[CP] Flow analysis: fix first phase handling of pattern assignments. #52767
Labels
area-analyzer
Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
cherry-pick-approved
Label for approved cherrypick request
cherry-pick-merged
Cherry-pick has been merged to the stable or beta branch.
cherry-pick-review
Issue that need cherry pick triage to approve
merge-to-stable
Commit(s) to merge
2ca7380
Target
Stable
Prepared changelist for beta/stable
https://dart-review.googlesource.com/c/sdk/+/310702
Issue Description
During the first phase of flow analysis, in which flow analysis "looks ahead" to see which variables are potentially assigned in each code block, the analyzer and front end fail to properly account for variables that are assigned by pattern assignment expressions. This "look ahead" information is used by flow analysis to determine the effects of nonlinear control flow (e.g. to figure out which variables to demote at the top of a loop, or to figure out which variables are potentially assigned inside a closure). As a result, some correct code is rejected by the analyzer and compiler, e.g.:
Also, some incorrect code is (unsoundly) accepted, e.g.:
What is the fix
The fix is to call
AssignedVariables.write
from the analyzer'sFlowAnalysisVisitor
, and from the CFE'sBodyBuilder
, to let flow analysis know about variable assignments that occur inside pattern assignment expressions.Why cherry-pick
If the fix is not cherry-picked, it's possible that an incorrect (and therefore buggy) program might be accepted by the compiler, leading to a crash. The risk of this happening is fairly low today, because the new "patterns" feature hasn't been used very much yet, but will increase over time as the feature becomes more widely used.
The risk is low, since the only code path in the analyzer and front end that's affected by the fix is the code path for dealing with assignments to variables inside pattern assignments, and this is precisely the situation in which the bug occurs.
Risk
low
Issue link(s)
#52745
Extra Info
No response
The text was updated successfully, but these errors were encountered: