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

for-in inference broken in dev.17.0 #31884

Closed
natebosch opened this Issue Jan 12, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@natebosch
Member

natebosch commented Jan 12, 2018

When the iterable in a for-in loop needs inference the variable doesn't get the right inferred type. Examples shown with implicit-casts: false, but the broken inference can cause problems in other ways without that option - such as no good feedback in the IDE

An example:

Broken:

void doStuff(int x) {}

void main() {
  var ints = [1];
  for (var x in ints.map((i) => i)) {
    doStuff(x); // The argument type 'dynamic' can't be assigned to the parameter type 'int'.
  }
}

Working if you add a variable so the type is already inferred:

void doStuff(int x) {}

void main() {
  var ints = [1];
  var intermediate = ints.map((i) => i);
  for (var x in intermediate) {
    doStuff(x); // No static error
  }
}

Also works if you force the type of the loop variable

void doStuff(int x) {}

void main() {
  var ints = [1];
  for (int x in ints.map((i) => i)) { // No static error
    doStuff(x);
  }
}

cc @munificent

natebosch added a commit to dart-lang/build that referenced this issue Jan 12, 2018

Add explicity types to for-in loops
Work around for dart-lang/sdk#31884 in the
dev.17.0 version of the SDK

natebosch added a commit to dart-lang/build that referenced this issue Jan 12, 2018

Add explicit types to for-in loops (#839)
Work around for dart-lang/sdk#31884 in the
dev.17.0 version of the SDK
@lrhn

This comment has been minimized.

Show comment
Hide comment
@lrhn

lrhn Jan 14, 2018

Member

Also, shouldn't the dynamic typed expression be down-cast'able to any type?
(Or is this code analyzed with "no implicit downcasts"?)

Member

lrhn commented Jan 14, 2018

Also, shouldn't the dynamic typed expression be down-cast'able to any type?
(Or is this code analyzed with "no implicit downcasts"?)

@natebosch

This comment has been minimized.

Show comment
Hide comment
@natebosch

natebosch Jan 14, 2018

Member

Or is this code analyzed with "no implicit downcasts"?

Yes, we're using implicit-casts: false here

Member

natebosch commented Jan 14, 2018

Or is this code analyzed with "no implicit downcasts"?

Yes, we're using implicit-casts: false here

natebosch added a commit to dart-lang/graphs that referenced this issue Jan 16, 2018

Add explicit type to for-in loop
Workaround for dart-lang/sdk#31884 in the
dev.17.0 version of the SDK.

natebosch added a commit to dart-lang/graphs that referenced this issue Jan 17, 2018

Add explicit type to for-in loop (#16)
Workaround for dart-lang/sdk#31884 in the
dev.17.0 version of the SDK.
@leafpetersen

This comment has been minimized.

Show comment
Hide comment
@leafpetersen

leafpetersen Jan 18, 2018

Member

Seems to be fixed in dev.18

Member

leafpetersen commented Jan 18, 2018

Seems to be fixed in dev.18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment