Skip to content
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

List.fold generic type parameters not inferred #25491

Closed
leafpetersen opened this issue Jan 14, 2016 · 3 comments
Closed

List.fold generic type parameters not inferred #25491

leafpetersen opened this issue Jan 14, 2016 · 3 comments
Assignees
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on

Comments

@leafpetersen
Copy link
Member

The code below produces the following error:

[warning] x (dynamic) will need runtime check to cast to type List<String> (/Users/leafp/tmp/scratch.dart, line 4, col 20)

Suggesting that we are not successfully inferring the generic type parameters for fold. I would have thought we would be catching this. I don't think this is an instance of #25490, but maybe an interaction between the upwards inference on the arguments and the downwards inference on the closure parameter?

void main() {
  List<int> o;
  var x = o.fold(0, (x, y) => x + y);
  List<String> y = x;
}
@leafpetersen leafpetersen added Priority-Medium area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Jan 14, 2016
@jmesserly
Copy link

funny enough, we have a test specifically for "fold" ... let me dig in

@jmesserly
Copy link

@leafpetersen here's what I see in terms of hints:

  info: [INFERRED_TYPE_CLOSURE] line 12, column 29 of /main.dart: (x, y) => x + y has inferred type (dynamic, int) → dynamic
            var x = o.fold(0, (x, y) => x + y);
                              ^^^^^^^^^^^^^^^
  info: [DYNAMIC_INVOKE] line 12, column 39 of /main.dart: x + y requires dynamic invoke
            var x = o.fold(0, (x, y) => x + y);
                                        ^^^^^

I believe what happens is:

  1. We instantiate with T as dynamic.
  2. That pushes down (dynamic, int) → dynamic to the closure.
  3. When we come back up, we see that type as the 2nd arg to fold. LUB of "int" and "dynamic" will be "dynamic"

So I think we need #25490, unless I'm missing something.

If there's some way we could infer from only the first argument, we'd get T:int, but we'd still need to push that type down to the lambda, right? At least for checking. Otherwise I don't see how we know that the '+' is a valid operation.

@leafpetersen
Copy link
Member Author

Ah right. Annoying. I guess for now you just have to put types on the arguments.

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on and removed Priority-Medium labels Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

3 participants