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

local variable looses promoted type in closure, in the presence of a re-assignment #1675

Closed
srawlins opened this issue Jun 9, 2021 · 5 comments
Labels
request Requests to resolve a particular developer problem

Comments

@srawlins
Copy link
Member

srawlins commented Jun 9, 2021

(Note I have a very amateur understanding of flow analysis)

Take this code:

void f(String? s, List<int> lengths) {
  for (;;) {
    if (s == null) return;
    
    if (1==2) break;
    s = "not null";
  }
  s.length; // OK
  
  // The property 'length' can't be unconditionally accessed because the receiver can be null.
  lengths.any((e) => e == s.length);
}

So after the for loop, s is promoted to String. s.length is then OK. But later on, in a closure, s.length is not OK; analyzer and CFE say that s is a String?.

I know that there is not a lot of flow analysis or promotion going on within closures, but there is some. In this simpler example, which contains zero re-assignments, we get no errors:

void f2(String? s, List<int> lengths) {
  if (s == null) return;
  
  lengths.any((e) => e == s.length);
}

So I'm guess something about the re-assignment (with a String value, note) throws a lot of flow analysis or promotion out the window.

Bug? Feature? I perused for other promotion bugs but didn't find anything like this.

@srawlins srawlins added the request Requests to resolve a particular developer problem label Jun 9, 2021
@srawlins srawlins changed the title local variable losses promoted type in closure, even when closure is followed by zero assignments local variable looses promoted type in closure, in the presence of a re-assignment Jun 9, 2021
@srawlins
Copy link
Member Author

srawlins commented Jun 9, 2021

cc @jeffkwoh @stereotype441

@srawlins
Copy link
Member Author

srawlins commented Jun 9, 2021

(I'm looking at this in DartPad, by the way, using Dart 2.13.1)

@jeffkwoh
Copy link

Thank you for reproducing and filing this here :)

@leafpetersen
Copy link
Member

Duplicate of #1536 .

@srawlins
Copy link
Member Author

Thanks for finding that, Leaf!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

3 participants