Skip to content

Commit 7905e78

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
NNBD preview: Better messaging for non-late uninitialized variable
Change-Id: Ica0433154f0f72d6f79159a77e0b784bdb1d6f40 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125561 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent b5b45ff commit 7905e78

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

pkg/analysis_server/lib/src/edit/nnbd_migration/info_builder.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ class InfoBuilder {
138138
} else {
139139
nullableValue = "a nullable value";
140140
}
141+
142+
CompilationUnit unit = node.thisOrAncestorOfType<CompilationUnit>();
143+
int lineNumber = unit.lineInfo.getLocation(node.offset).lineNumber;
144+
145+
if (origin.kind == EdgeOriginKind.uninitializedRead) {
146+
return "This variable could not be made 'late' because it is used on "
147+
"line $lineNumber, when it is possibly uninitialized";
148+
}
149+
141150
if (parent is ArgumentList) {
142151
return capitalize("$nullableValue is passed as an argument");
143152
}
@@ -167,8 +176,6 @@ class InfoBuilder {
167176
return (ancestor is TypedLiteral) ? ancestor : null;
168177
}
169178

170-
CompilationUnit unit = node.thisOrAncestorOfType<CompilationUnit>();
171-
int lineNumber = unit.lineInfo.getLocation(node.offset).lineNumber;
172179
FunctionBody functionBody = findFunctionBody();
173180
if (functionBody != null) {
174181
AstNode function = functionBody.parent;

pkg/analysis_server/test/src/edit/nnbd_migration/info_builder_test.dart

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,24 +409,24 @@ void f() {
409409
test_localVariable() async {
410410
UnitInfo unit = await buildInfoForSingleTestFile('''
411411
void f() {
412-
int _v1 = null;
413-
int _v2 = _v1;
412+
int v1 = null;
413+
int v2 = v1;
414414
}
415415
''', migratedContent: '''
416416
void f() {
417-
int? _v1 = null;
418-
int? _v2 = _v1;
417+
int? v1 = null;
418+
int? v2 = v1;
419419
}
420420
''');
421-
List<RegionInfo> regions = unit.regions;
421+
List<RegionInfo> regions = unit.fixRegions;
422422
expect(regions, hasLength(2));
423423
assertRegion(
424424
region: regions[0],
425425
offset: 16,
426426
details: ["This variable is initialized to an explicit 'null'"]);
427427
assertRegion(
428428
region: regions[1],
429-
offset: 35,
429+
offset: 34,
430430
details: ["This variable is initialized to a nullable value"]);
431431
}
432432

@@ -949,4 +949,29 @@ class C {
949949
assertDetail(detail: region.details[1], offset: 34, length: 3);
950950
assertDetail(detail: region.details[2], offset: 70, length: 3);
951951
}
952+
953+
test_uninitializedVariable_notLate_uninitializedUse() async {
954+
UnitInfo unit = await buildInfoForSingleTestFile('''
955+
void f() {
956+
int v1;
957+
if (1 == 2) v1 = 7;
958+
g(v1);
959+
}
960+
void g(int i) => print(i.isEven);
961+
''', migratedContent: '''
962+
void f() {
963+
int? v1;
964+
if (1 == 2) v1 = 7;
965+
g(v1!);
966+
}
967+
void g(int i) => print(i.isEven);
968+
''');
969+
List<RegionInfo> regions = unit.fixRegions;
970+
expect(regions, hasLength(2));
971+
assertRegion(region: regions[0], offset: 16, details: [
972+
"This variable could not be made \'late\' because it is used on line 4, "
973+
"when it is possibly uninitialized"
974+
]);
975+
// regions[1] is the `v1!` fix.
976+
}
952977
}

0 commit comments

Comments
 (0)