-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
In https://dart-review.googlesource.com/c/sdk/+/389590, I've added documentation to the FlowAnalysis class explaining the assumptions flow analysis makes about the order in which its client visits AST nodes during type inference.
I've found at least two instances in which the CFE's order of visiting AST nodes breaks these assumptions:
-
When visiting a variable declaration with an initializer expression, the CFE first visits the initializer expression, then it calls
FlowAnalysis.declareandFlowAnalysis.initialize, then it callsensureAssignableResult, which may rewrite the expression (leading to a call toFlowAnalysis.forwardExpression). This breaks the assumptions of flow analysis because flow analysis assumesforwardExpressionwill be called immediately after visiting the expression, and before continuing with the parent node; the call toinitializecounts as continuing with the parent node. -
When visiting a list, map, or set literal, the CFE first visits all the elements of the literal to determine their types; then it infers the type parameter(s) of the literal (if necessary); then it makes a second pass through the literal to do assignability checks, making sure that all the elements have the proper types (and performing implicit conversions if necessary). This breaks the assumptions of flow analysis because flow analysis assumes that the AST nodes are visited in a single-pass fashion.
I haven't been able to find any bugs caused by (1); it seems to be benign. As for (2), it leads to some "why not promoted" context messages getting dropped; fortunately this isn't a serious problem, since these "why not promoted" context messages exist solely for the purpose of helping the user figure out why they have assignability errors; they don't affect execution semantics in any way.
However, I'm concerned that these assumptions might be broken in other ways that I haven't discovered yet, so I'm filing this issue to track the situation and collect ideas about how we might be able to improve it.