-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Final variable type promotion is lost inside a closure if followed by try/catch #46532
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
Also, notice that this compiles fine: print(a.foo);
//print(['foo', 'bar'].firstWhere((i) => i == a.foo)); So type promotion seems to work when outside the closure only. |
The following reduced example will actually suffice to obtain the error: void main() {
final num a;
a = 1;
if (a is int) (i) => i == a.isEven; // fails to compile
} whereas the following compiles without errors: void main() {
final num a = 1;
if (a is int) (i) => i == a.isEven; // fails to compile
} The reason why there is no promotion of Note that an initializing expression does not count as an assignment, which is the reason why we can use I think we can close this issue as a duplicate, because the discussion about this particular part of the flow analysis is already the topic of that issue. |
The treatment of a final local variable for purpose of flow analysis should not depend on whether it's assigned immediately on declaration, or separately. There's no good reason to do it that way. As long as the variable has been definitely assigned (which the analyzer can easily recognize), the two cases are identical?! I agree that in the case where the variable is not final, the current behaviour is appropriate. But for final local variables, I believe it's simply a mistake to not promote the type. |
I think it's better to make that argument in dart-lang/language#1247! |
dart --version
)Description
The following code attempts to use type promotion of a final variable, which should be guaranteed to work in this case because the variable in question is final.
The two lines where it says
// fails to compile
fail because of the errors (from DartPad):I am almost certain this is a bug because changing the code that assigns
a
from:to:
... causes the problem to go away. But at the point where the error occurs, it shouldn't matter which of the two above was used, as
a
is guaranteed to be assigned and final after this.This issue might be related to issue 32285?
The text was updated successfully, but these errors were encountered: