-
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
Using a late final
variable as loop variable for for
/in
gives incorrect late errors
#60359
Comments
The specification isn't entirely complete on this topic, but I do think we can find most of the rules that are needed to clarify this situation. The for-in loop
The rule about the flow analysis of a for-in statement is found later in the same document, but it doesn't spell out exactly how the assignment mentioned above is taken into account. There's a TODO about it:
This means that we can't see precisely how the for-in statement updates the variable model of In particular, it would then be 'not definitely unassigned' before the synthetic assignment, which would imply that the synthetic assignment to |
It's not sound to consider the loop variable as definitely assigned after the loop, because the loop might execute zero times. I think the last bullet should be replaced with something like:
Agreed. Accordingly, I'm classifying this as P3. |
…igned_test.dart`. Some of the tests in `assignment_to_final_local_test.dart` were actually testing `late final` locals, so they make more sense in `late_final_local_already_assigned_test.dart`. This paves the way for fixing #60359 (Using a `late final` variable as loop variable for `for`/`in` gives incorrect late errors). Change-Id: I58da6ae6791adb1b124129b30b0d8c62bfa2d519 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417000 Auto-Submit: Paul Berry <paulberry@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Example:
This code should be able to run without invalidating any
late
variable constraints.The analyzer reports:
The front end (DDC, Dartpad, main branch) reports:
The analysis should not consider
c
definitely assigned in the loop header (since it isn't definitely assigned, the example code above would only assign to it once).The error message suggests that the logic for
for
/in
(which is not well specified to begin with) has some clause saying that the variable must not befinal
. That would predatelate
variables, and shouldn't be take verbatim any more.The variable must be assignable, which means it must:
const
,final
unless:late
and potentially unassigned.Assignment analysis of a
for
/in
(andwhile
) loop should treat the loop variable as:Basically, it should work just as if the loop was written as:
which both CFE and analyzer accepts.
(I don't think a
late final
loop variable is something anyone would ever write, so not a high priority to fix.)@stereotype441
The text was updated successfully, but these errors were encountered: