-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Non-nullable analysis flow fails at recognizing nullcheck #44331
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
Comments
The compiler would have to prove that But you could even get rid of the whole null-check with |
Well, that's a null-check too. I was not aware of this syntax, it works like a charm. I have another case where no other type of null-check works, I'm assuming because the field is static.
I understand nnbd is still in beta and if this is the expected behavior, hopefully it gets documented in the future. Other than these few edge cases migrating my code was fairly straightforward. |
Again, the compiler would have to prove that |
Reading the announcement article, flow analysis sounded like a cool feature, but its usefulness seems to be limited to local variables, which I barely have any. I gave up on trying to use it, and I migrated my code to ?. syntax. |
@Rudiksz wrote:
Flow analysis is never applied to any other entity than a local variable. Note that 'local variable' here covers formal parameters of enclosing functions as well as "regular" local variables (that is, the ones whose declaration is derived from In general, any action that could run unknown code (that is, any instance method invocation and any invocation of a first-class function object) could potentially call code that changes the value of a static or top-level variable. If the variable under scrutiny is an instance variable then the same thing could happen, but also: its getter could be overridden in a subclass of the statically known type of In both cases it is unsound to rely on a test in the past to conclude that the value still isn't null (or that it still has any particular property that we may have tested). @MarvinHannott wrote:
There have been many proposals for enhancements to type promotions, e.g., to have a dynamically checked variant (cf. dart-lang/language#1188). Dynamically checked promotions would allow essentially all of these use cases, but they would of course be unsafe in the sense that they could throw at run time. Specifically allowing promotions to take place with a static or top-level However, you could argue that this solution is still half-baked, because each local context (each function body, initializing expression, etc.) would have to re-discover that said static/top-level variable value has a specific property (e.g., it is non-null; or it has a specific type which is not guaranteed because it isn't a supertype of the declared type). Hence, it would provide exactly the same level of convenience as the technique where the static/top-level variable is stored in a local variable. PS: @MarvinHannott, thanks for giving such precise explanations! |
On Dart 2.12.0-76.0.dev (dev) and windows I have the following code that gives a compile error.
The workaround below works, but it's a needless complication and something I bump into more and more.
The text was updated successfully, but these errors were encountered: