-
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
Flow analysis is overly pessimistic about closures capturing for-in loop variables #43136
Comments
Is this still planned? |
I would still like to do this, especially because it means that code that used to work (prior to null safety) will stop working when null safety is enabled. However, time is drawing short and I might not get to it in time. |
When dart-lang/sdk#43136 is fixed, two non-null assertions in the source_span package will become unnecessary, causing warnings which will break the build of the SDK. This change adds "// ignore" comments to avoid the breakage. Once the fix is landed, I'll remove both the "// ignore" comments and the unnecessary non-null assertions.
Flow analysis currently has a bug preventing for-each loop variables from being properly promoted in the presence of closures (dart-lang/sdk#43136); as a result of this bug the source_span package had two non-null assertions that ought to have been unnecessary. I'm preparing a fix for that bug, however if I land it as is, it will cause the front end to emit errors saying "Operand of null-aware operation '!' has type '_Highlight' which excludes null"; this in turn will cause SDK bot breakages (since source_span is imported into the SDK repo). So, in order to land the fix, we need to first update the source_span package to work around the bug; this change does that by allocating a temporary variable (which *is* promoted correctly). Once the fix for dart-lang/sdk#43136 lands, I will make a follow-up change that deletes the temporary variable.
This change, together with the corresponding implementation fix at https://dart-review.googlesource.com/c/sdk/+/162581, fixes dart-lang/sdk#43136.
This change, together with the corresponding implementation fix at https://dart-review.googlesource.com/c/sdk/+/162581, fixes dart-lang/sdk#43136.
This change, together with the corresponding implementation fix at https://dart-review.googlesource.com/c/sdk/+/162581, fixes dart-lang/sdk#43136.
This change, together with the corresponding implementation fix at https://dart-review.googlesource.com/c/sdk/+/162581, fixes dart-lang/sdk#43136.
Flow analysis currently has a bug preventing for-each loop variables from being properly promoted in the presence of closures (dart-lang/sdk#43136); as a result of this bug the source_span package had two non-null assertions that ought to have been unnecessary. I'm preparing a fix for that bug, however if I land it as is, it will cause the front end to emit errors saying "Operand of null-aware operation '!' has type '_Highlight' which excludes null"; this in turn will cause SDK bot breakages (since source_span is imported into the SDK repo). So, in order to land the fix, we need to first update the source_span package to work around the bug; this change does that by allocating a temporary variable (which *is* promoted correctly). Once the fix for dart-lang/sdk#43136 lands, I will make a follow-up change that deletes the temporary variable.
Whoops, fix has not landed yet. |
…en to. When performing flow analysis for a "for each" loop such as `for (var x in ...) { ... }`, we don't need to consider the iterated value to be "written to" the variable `x`, since the actual runtime semantics are to create a fresh instance of the variable `x` each time through the loop, initialized to the iterated value. Fixes #43136. Bug: #43136 Change-Id: I497c408c3efc26e93502de8ba0530bb5278e74c6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/162581 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Paul Berry <[email protected]>
@stereotype441 – I don't think you meant to have this closed by 27fc687 – right? I think GitHubs auto-close logic caught you by mistake |
Now that dart-lang/sdk#43136 is fixed, the workaround is no longer needed.
This cast was working around dart-lang/sdk#43136. Now that it is fixed, the cast is no longer needed.
Now that dart-lang/sdk#43136 is fixed, the workaround is no longer needed.
This cast was working around dart-lang/sdk#43136. Now that it is fixed, the cast is no longer needed.
This cast was working around dart-lang/sdk#43136. Now that it is fixed, the cast is no longer needed.
Flow analysis currently has a bug preventing for-each loop variables from being properly promoted in the presence of closures (dart-lang/sdk#43136); as a result of this bug the source_span package had two non-null assertions that ought to have been unnecessary. I'm preparing a fix for that bug, however if I land it as is, it will cause the front end to emit errors saying "Operand of null-aware operation '!' has type '_Highlight' which excludes null"; this in turn will cause SDK bot breakages (since source_span is imported into the SDK repo). So, in order to land the fix, we need to first update the source_span package to work around the bug; this change does that by allocating a temporary variable (which *is* promoted correctly). Once the fix for dart-lang/sdk#43136 lands, I will make a follow-up change that deletes the temporary variable.
Now that dart-lang/sdk#43136 is fixed, the workaround is no longer needed.
The following code works in legacy mode:
It prints:
It ought to work with the null safety experiment enabled as well, but instead it gives a compile error:
I believe what's happening is that flow analysis considers the variable
n
to be assigned to within the body of the method, so it cancels promotions on entry to the function expression. However, according to the spec, the for-in loop is treated as equivalent to:...which is accepted by flow analysis (because each initialization of
n
is in effect initializing a fresh variable, so there is no assignment that could clobber promotion).Flow analysis should be changed so that the
for-in
form behaves the same as its desugared equivalent.The text was updated successfully, but these errors were encountered: