Skip to content

Strange inference with a ternary operator and Lists #24202

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

Closed
nex3 opened this issue Aug 25, 2015 · 2 comments
Closed

Strange inference with a ternary operator and Lists #24202

nex3 opened this issue Aug 25, 2015 · 2 comments
Labels
closed-duplicate Closed in favor of an existing report legacy-area-analyzer Use area-devexp instead.

Comments

@nex3
Copy link
Member

nex3 commented Aug 25, 2015

On the following code:

void fn(Iterable iter) {}

void main() {
  bool condition = true;
  Iterable<String> iter = [];
  fn(condition ? [] : iter.toList());
}

The analyzer reports the following warning:

[warning] The argument type 'EfficientLength' cannot be assigned to the parameter type 'Iterable'

This seems wrong. The first branch of the ternary operator is a List, which is assignable to Iterable.

Note that if <String> is removed from the type annotation for iter, or if <String> is added to the list literal, the warning goes away.

I'm using 1.13.0-edge.9e18a7519d429f87c70bb3b8871c6cc666ee3fa6.

@nex3 nex3 added Type-Defect legacy-area-analyzer Use area-devexp instead. labels Aug 25, 2015
@lrhn lrhn added the closed-duplicate Closed in favor of an existing report label Aug 26, 2015
@lrhn
Copy link
Member

lrhn commented Aug 26, 2015

This looks like an instance of issue #19425.

@bwilkerson
Copy link
Member

Yes, sadly, unless we get a different definition of least upper bounds we're stuck. The current specification of LUB says that LUB(List, List) is EfficientLength. The answer that everyone expects is List, so it surprises lots of people fairly frequently.

To work around this, you either have to write "[]" for the literal, or stop using the conditional operator (the only place where LUB is used in the spec.).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-duplicate Closed in favor of an existing report legacy-area-analyzer Use area-devexp instead.
Projects
None yet
Development

No branches or pull requests

3 participants