Skip to content

Confusing warning when improperly typed closure passed to map #26992

Closed
@stereotype441

Description

The following ill-typed code:

    void f(List<String> x, List<String> y) {
      x.addAll(y.map((String z) => 1.0));
    }

produces the strong mode warning Unsound implicit cast from Iterable<dynamic> to Iterable<String>.

This is confusing because there's nothing dynamic in the above code. I would expect one of the following behaviors:

  • (String z) => 1.0 is inferred to have type (String) -> double, therefore y.map((String z) => 1.0) is inferred to have type Iterable<double>, but x.addAll requires Iterable<String>, therefore the message is Unsound implicit cast from Iterable<double> to Iterable<String>.
  • Since x.addAll requires Iterable<String>, the type argument to y.map is inferred to be String, therefore y.map requires type (String) -> String. But the type of (String z) => 1.0 is (String) -> double, therefore the message is unsound implicit cast from (String) -> double to (String) -> String.

Activity

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

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work onanalyzer-uxarea-analyzerUse area-analyzer for Dart analyzer issues, including the analysis server and code completion.type-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions