Skip to content

Type promotion failure in for loop with type parameters #39171

@stereotype441

Description

@stereotype441

The following code:

void f<T>(Iterable<T> Function() g, void Function(T) h) {
  for (var x in g()) {
    if (x is String) {
      h(x);
    }
  }
}

Produces the analyzer error:

error • The argument type 'String' can't be assigned to the parameter type 'T'. • .../test.dart:4:9 • argument_type_not_assignable

There should be no error; the type of x should be inferred to be T, and the if test should promote it to T & String, which is a valid subtype of h's parameter type, T.

The problem bisects to 7c1fa84.
This bug is blocking an internal roll, so we're considering it blocking for D27.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions