Skip to content

Commit 86ce74c

Browse files
danrubelcommit-bot@chromium.org
authored andcommitted
update NodeBuilder for default value type arguments
This updates NodeBuilder to build nodes for type arguments in default parameter values. Change-Id: I8999bdfca9a60d9e1d3feff5cdbd1cdc62cdb4fd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108120 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Dan Rubel <[email protected]>
1 parent 439692c commit 86ce74c

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ class NodeBuilder extends GeneralizingAstVisitor<DecoratedType> {
146146
@override
147147
DecoratedType visitDefaultFormalParameter(DefaultFormalParameter node) {
148148
var decoratedType = node.parameter.accept(this);
149-
if (node.declaredElement.hasRequired || node.defaultValue != null) {
149+
if (node.defaultValue != null) {
150+
node.defaultValue.accept(this);
151+
return null;
152+
} else if (node.declaredElement.hasRequired) {
150153
return null;
151154
}
152155
if (decoratedType == null) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ class Variables implements VariableRecorder, VariableRepository {
4848
throw StateError('No declarated type annotations in ${source.fullName}; '
4949
'expected one for ${typeAnnotation.toSource()}');
5050
}
51-
return annotationsInSource[_uniqueOffsetForTypeAnnotation(typeAnnotation)];
51+
DecoratedTypeAnnotation decoratedTypeAnnotation =
52+
annotationsInSource[_uniqueOffsetForTypeAnnotation(typeAnnotation)];
53+
if (decoratedTypeAnnotation == null) {
54+
throw StateError('Missing declarated type annotation'
55+
' in ${source.fullName}; for ${typeAnnotation.toSource()}');
56+
}
57+
return decoratedTypeAnnotation;
5258
}
5359

5460
Map<Source, List<PotentialModification>> getPotentialModifications() =>

‎pkg/nnbd_migration/test/edge_builder_test.dart‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,17 @@ int/*1*/ f(int/*2*/ i) => i/*3*/;
10421042
hard: true));
10431043
}
10441044

1045+
test_functionDeclaration_parameter_named_default_listConst() async {
1046+
await analyze('''
1047+
void f({List<int/*1*/> i = const <int/*2*/>[]}) {}
1048+
''');
1049+
1050+
assertNoUpstreamNullability(decoratedTypeAnnotation('List<int/*1*/>').node);
1051+
assertEdge(decoratedTypeAnnotation('int/*2*/').node,
1052+
decoratedTypeAnnotation('int/*1*/').node,
1053+
hard: false);
1054+
}
1055+
10451056
test_functionDeclaration_parameter_named_default_notNull() async {
10461057
await analyze('''
10471058
void f({int i = 1}) {}

0 commit comments

Comments
 (0)