Skip to content

Commit 2b96453

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: better handle a variable declaration statement with multiple variables.
This fix addresses the case where a variable declaration statement declares multiple variables with different types; previously, we were applying the type of the first variable to all the variables, leading to possible crashes. There are still some other issues with variable declaration statements that declare multiple variables; I'll address them in follow-up CLs. Bug: #47669 Change-Id: Iba82ef5ccc0b22382f30bf5a6ed391f4874f7443 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/220042 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 8809779 commit 2b96453

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

pkg/nnbd_migration/lib/src/node_builder.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ class NodeBuilder extends GeneralizingAstVisitor<DecoratedType>
660660
DecoratedType? visitVariableDeclarationList(VariableDeclarationList node) {
661661
node.metadata.accept(this);
662662
var typeAnnotation = node.type;
663-
var type = _pushNullabilityNodeTarget(
663+
var declaredType = _pushNullabilityNodeTarget(
664664
NullabilityNodeTarget.element(
665665
node.variables.first.declaredElement!, _getLineInfo),
666666
() => typeAnnotation?.accept(this));
@@ -671,9 +671,11 @@ class NodeBuilder extends GeneralizingAstVisitor<DecoratedType>
671671
if (hint != null && hint.kind == HintCommentKind.lateFinal) {
672672
_variables!.recordLateHint(source, node, hint);
673673
}
674+
var parent = node.parent;
674675
for (var variable in node.variables) {
675676
variable.metadata.accept(this);
676677
var declaredElement = variable.declaredElement;
678+
var type = declaredType;
677679
if (type == null) {
678680
var target =
679681
NullabilityNodeTarget.element(declaredElement!, _getLineInfo);
@@ -683,11 +685,11 @@ class NodeBuilder extends GeneralizingAstVisitor<DecoratedType>
683685
}
684686
_variables!.recordDecoratedElementType(declaredElement, type);
685687
variable.initializer?.accept(this);
686-
}
687-
var parent = node.parent;
688-
if (parent is FieldDeclaration) {
689-
if (_hasAngularChildAnnotation(parent.metadata)) {
690-
_graph.makeNullable(type!.node!, AngularAnnotationOrigin(source, node));
688+
if (parent is FieldDeclaration) {
689+
if (_hasAngularChildAnnotation(parent.metadata)) {
690+
_graph.makeNullable(
691+
type.node!, AngularAnnotationOrigin(source, node));
692+
}
691693
}
692694
}
693695
return null;

pkg/nnbd_migration/test/api_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8927,6 +8927,31 @@ http.BaseClient downcast(http.Client x) => x as http.BaseClient;
89278927
await _checkSingleFileChanges(content, expected);
89288928
}
89298929

8930+
Future<void> test_var_with_different_types() async {
8931+
// Based on https://github.com/dart-lang/sdk/issues/47669
8932+
var content = '''
8933+
class C<T> {
8934+
T m() => throw 'foo';
8935+
}
8936+
f(bool b, List<C<int>> cs) {
8937+
var x = !b,
8938+
y = cs.first,
8939+
z = y.m();
8940+
}
8941+
''';
8942+
var expected = '''
8943+
class C<T> {
8944+
T m() => throw 'foo';
8945+
}
8946+
f(bool b, List<C<int>> cs) {
8947+
var x = !b,
8948+
y = cs.first,
8949+
z = y.m();
8950+
}
8951+
''';
8952+
await _checkSingleFileChanges(content, expected);
8953+
}
8954+
89308955
Future<void> test_weak_if_visit_weak_subexpression() async {
89318956
var content = '''
89328957
int f(int x, int/*?*/ y) {

0 commit comments

Comments
 (0)