-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Analyzer infers promoted type on locals #40413
Comments
Interesting corner case! Even though it's not strictly speaking a null safety issue, I'm adding it to the analyzer team's null safety epic, since null safety causes type promotion to happen more frequently, so it's likely to crop up more often in opted in libraries. |
Extra info which may be helpful for others: @johnniwinther just mentioned that the difficulty is demotion of the variable, and also that a promising approach is the following: Allow the local variable |
Just wanted to point to code that started breaking because of this change: #40485 The local variable with the promoted type seems to be final and only assigned in that initialization. Should that be allowed? (I believe it would follow from the alternative approach suggested by @eernstg in the comment above) |
I think the alternative approach would work just as well for a final variable. It might seem slightly convoluted in that case, and maybe we could get away with a rule that says that the inferred type of a final variable can be an intersection type. But as long as it's consistent with the treatment of non-final variables the complexity of the approach in general doesn't grow, so we might as well use this approach for final variables as well. PS: The alternative approach came from @johnniwinter, but I think it's a good idea. ;) |
Note: since this is a breaking change, it needs to be pre-announced and pre-tested internally before it's landed. |
Also note: the change has already been landed by the CFE seeing only one problem internally. |
Internal presubmit looks green. https://test.corp.google.com/ui#id=OCL:295832949:BASE:295984425:1582132722046:8eaca57b Bug: #40413 Change-Id: I8514f6e1a99c4b02098cc649607cf31c5daf350f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/136360 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
The analyzer wrongfully infers promoted types on locals.
Consider this example:
The inferred type for
o
isT & String
which leads to a missing error in theo.length
access.This issue was only recently fixed in the CFE: https://dart-review.googlesource.com/c/sdk/+/133588
cc @stereotype441, @lrhn, @eernstg
The text was updated successfully, but these errors were encountered: