Skip to content

Commit 7a73682

Browse files
danrubelcommit-bot@chromium.org
authored andcommitted
update NodeBuilder to handle catch clause
This updates NodeBuilder to create a node for the exception parameter in a catch clause. Not doing so creates an exception downstream in the EdgeBuilder. Change-Id: Idaf0ab62e47018a634ff028af0e3af1d504c02ba Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107923 Reviewed-by: Paul Berry <[email protected]>
1 parent fc6cb0a commit 7a73682

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎pkg/nnbd_migration/lib/src/node_builder.dart‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ class NodeBuilder extends GeneralizingAstVisitor<DecoratedType> {
5757
: _nonNullableObjectType =
5858
DecoratedType(_typeProvider.objectType, _graph.never);
5959

60+
@override
61+
DecoratedType visitCatchClause(CatchClause node) {
62+
DecoratedType exceptionType = node.exceptionType?.accept(this);
63+
if (node.exceptionParameter != null) {
64+
exceptionType ??= DecoratedType(_typeProvider.objectType, _graph.never);
65+
_variables.recordDecoratedElementType(
66+
node.exceptionParameter.staticElement, exceptionType);
67+
}
68+
node.stackTraceParameter?.accept(this);
69+
node.body?.accept(this);
70+
return null;
71+
}
72+
6073
@override
6174
DecoratedType visitClassDeclaration(ClassDeclaration node) {
6275
node.metadata.accept(this);
@@ -263,7 +276,8 @@ $stackTrace''');
263276
parent is ImplementsClause ||
264277
parent is WithClause ||
265278
parent is OnClause ||
266-
parent is ClassTypeAlias) {
279+
parent is ClassTypeAlias ||
280+
parent is CatchClause) {
267281
nullabilityNode = _graph.never;
268282
} else {
269283
nullabilityNode = NullabilityNode.forTypeAnnotation(node.end);

‎pkg/nnbd_migration/test/edge_builder_test.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,26 @@ C f(C c, int i) => c..x = i;
541541
hard: false);
542542
}
543543

544+
test_catch_clause() async {
545+
await analyze('''
546+
foo() => 1;
547+
main() {
548+
try { foo(); } on Exception catch (e) { print(e); }
549+
}
550+
''');
551+
// No assertions; just checking that it doesn't crash.
552+
}
553+
554+
test_catch_clause_no_type() async {
555+
await analyze('''
556+
foo() => 1;
557+
main() {
558+
try { foo(); } catch (e) { print(e); }
559+
}
560+
''');
561+
// No assertions; just checking that it doesn't crash.
562+
}
563+
544564
test_class_alias_synthetic_constructor_with_parameters_complex() async {
545565
await analyze('''
546566
class MyList<T> {}

0 commit comments

Comments
 (0)