Skip to content

for-in inference broken in dev.17.0 #31884

Closed
@natebosch

Description

@natebosch

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    legacy-area-analyzerUse area-devexp instead.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions