Skip to content

Commit b907f99

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Migrator: Begin flow analysis on top-level annotations
Fixes #42295 Change-Id: I9d224b5c8a3fa5d57f17e434a9e6dfbcebc14781 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151026 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent 89fc3e1 commit b907f99

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

pkg/nnbd_migration/lib/src/edge_builder.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
127127
_flowAnalysis;
128128

129129
/// If we are visiting a function body or initializer, assigned variable
130-
/// information used in flow analysis. Otherwise `null`.
130+
/// information used in flow analysis. Otherwise `null`.
131131
AssignedVariables<AstNode, PromotableElement> _assignedVariables;
132132

133133
/// The [DecoratedType] of the innermost function or method being visited, or
@@ -281,6 +281,32 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
281281
}
282282
}
283283

284+
@override
285+
// TODO(srawlins): Theoretically, edges should be connected between arguments
286+
// and parameters, as in an instance creation. It is quite rare though, to
287+
// declare a class and use it as an annotation in the same package.
288+
DecoratedType visitAnnotation(Annotation node) {
289+
var previousFlowAnalysis = _flowAnalysis;
290+
var previousAssignedVariables = _assignedVariables;
291+
if (_flowAnalysis == null) {
292+
_assignedVariables = AssignedVariables();
293+
_flowAnalysis = FlowAnalysis<AstNode, Statement, Expression,
294+
PromotableElement, DecoratedType>(
295+
DecoratedTypeOperations(_typeSystem, _variables, _graph),
296+
_assignedVariables);
297+
}
298+
try {
299+
_dispatch(node.name);
300+
_dispatch(node.constructorName);
301+
_dispatchList(node.arguments?.arguments);
302+
} finally {
303+
_flowAnalysis = previousFlowAnalysis;
304+
_assignedVariables = previousAssignedVariables;
305+
}
306+
annotationVisited(node);
307+
return null;
308+
}
309+
284310
@override
285311
DecoratedType visitAsExpression(AsExpression node) {
286312
if (BestPracticesVerifier.isUnnecessaryCast(

pkg/nnbd_migration/lib/src/utilities/completeness_tracker.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ mixin CompletenessTracker<T> on AstVisitor<T>, PermissiveModeVisitor<T> {
1919

2020
@override
2121
T visitAnnotation(Annotation node) {
22+
annotationVisited(node);
23+
return super.visitAnnotation(node);
24+
}
25+
26+
void annotationVisited(Annotation node) {
2227
assert(() {
2328
_annotationTracker.nodeVisited(node);
2429
return true;
2530
}());
26-
return super.visitAnnotation(node);
2731
}
2832

2933
void typeNameVisited(TypeName node) {

pkg/nnbd_migration/test/edge_builder_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7508,6 +7508,16 @@ int f() {
75087508
expect(edge.sourceNode.displayName, 'throw expression (test.dart:2:10)');
75097509
}
75107510

7511+
Future<void> test_top_level_annotation_begins_flow_analysis() async {
7512+
await analyze('''
7513+
class C {
7514+
const C(bool x);
7515+
}
7516+
@C(true)
7517+
int x;
7518+
''');
7519+
}
7520+
75117521
Future<void> test_topLevelSetter() async {
75127522
await analyze('''
75137523
void set x(int value) {}

0 commit comments

Comments
 (0)