Skip to content

Closures should not close over unnecessary variables. #47981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mmcdon20 opened this issue Dec 21, 2021 · 2 comments
Closed

Closures should not close over unnecessary variables. #47981

mmcdon20 opened this issue Dec 21, 2021 · 2 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.

Comments

@mmcdon20
Copy link

In #47972 @lrhn said there was probably already an issue for this, but I couldn't find it, so apologies if this is a duplicate.

If closures only closed over variables that are actually used inside the closure, it would make working with Isolates easier.

As of dart 2.15 Isolate.spawn can accept a closure, but will fail at runtime if one of the variables it closes over is one of the non supported message types like a ReceivePort, or UserTag. The program will fail even if the variable is never used within the closure itself.

Consider this example program.

import 'dart:isolate';

void main() async {
  final receivePort = ReceivePort();
  int three = 3;
  await Isolate.spawn(
    (SendPort port) => Isolate.exit(port, [1, 2, three]),
    receivePort.sendPort,
  );
  print(receivePort.first);
}

Which fails at runtime with the error message:

Unhandled exception:
Invalid argument(s): Illegal argument in isolate message: (object is aReceivePort)
@lrhn lrhn added the area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. label Dec 21, 2021
@mraleph
Copy link
Member

mraleph commented Dec 21, 2021

Duplicate of #36983

@mraleph mraleph marked this as a duplicate of #36983 Dec 21, 2021
@mraleph mraleph closed this as completed Dec 21, 2021
@mraleph
Copy link
Member

mraleph commented Dec 21, 2021

We use #36983 to track this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.
Projects
None yet
Development

No branches or pull requests

3 participants