Skip to content

NNBD. No compile time error when read of local late definitely unassigned variable #41981

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
sgrekhov opened this issue May 20, 2020 · 4 comments
Assignees
Labels
legacy-area-front-end Legacy: Use area-dart-model instead.

Comments

@sgrekhov
Copy link
Contributor

According to the NNBD specification

It is a compile time error to read a local variable marked late when the variable is definitely unassigned

But in the following code it's not true

main() {
  late int i;
  Null n = null;
  if (n != null) {
    i = 42; // Variable is initialized in a dead code. This leaves it definitely unassigned
  }
  i;  // It is an error to read a local late variable when it is definitely unassigned.
//^
// [analyzer] unspecified
// [cfe] unspecified
}

This code produces no Issues in analyzer and LateInitializationError in CFE. There must be a compile time error here

Dart VM version: 2.9.0-10.0.dev (dev) (Tue May 19 15:16:48 2020 +0200) on "windows_x64"

@mkustermann
Copy link
Member

In this case it's easy to see the code is dead, but in general, it is undecidable whether certain code is dead or not.

/cc @lrhn @eernstg

@mkustermann mkustermann added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label May 20, 2020
@eernstg
Copy link
Member

eernstg commented May 20, 2020

I believe the rules in flow-analysis.md about if statements and the notNull continuation of n and the true continuation of n != null should make the unreachable bit for the "then" branch of the if true, so the flow analysis should conclude that i is definitely unassigned after the if.

The treatment of == / != is a bit underspecified (the implementation certainly doesn't do what the spec says, and it is known that the spec shouldn't say exactly what it currently says), but it would surprise me if it is unable to see that n != null will evaluate to false.

@eernstg
Copy link
Member

eernstg commented May 20, 2020

I think we should use area-front-end for this, because it is about the implementation of flow-analysis.md.

@eernstg eernstg added legacy-area-front-end Legacy: Use area-dart-model instead. and removed area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels May 20, 2020
@stereotype441
Copy link
Member

Finally getting a chance to look at this. I believe what's happening is that if (n != null) is promoting n to type Never, but we don't have any logic that says "if we promote a variable to type Never, then consider the code to be unreachable", so we don't recognize that the body of the if is dead code. (We have logic that says "if an expression has type Never, then consider the code to be unreachable", which is very similar, but not exactly the same).

I think this corner case is worth fixing. I'll try to get it fixed soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-front-end Legacy: Use area-dart-model instead.
Projects
None yet
Development

No branches or pull requests

4 participants