Skip to content

CFE's visit order sometimes violates flow analysis assumptions #56887

@stereotype441

Description

@stereotype441

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:

  1. When visiting a variable declaration with an initializer expression, the CFE first visits the initializer expression, then it calls FlowAnalysis.declare and FlowAnalysis.initialize, then it calls ensureAssignableResult, which may rewrite the expression (leading to a call to FlowAnalysis.forwardExpression). This breaks the assumptions of flow analysis because flow analysis assumes forwardExpression will be called immediately after visiting the expression, and before continuing with the parent node; the call to initialize counts as continuing with the parent node.

  2. 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.

Metadata

Metadata

Assignees

Labels

area-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.cfe-dysfunctionalitiesIssues for the CFE not behaving as intendedmodel-flowImplementation of flow analysis in analyzer/cfe

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions