Skip to content

Commit fa2df01

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Null Safety preview: Fix description for uninitialized field
Fixes flutter#40773 Change-Id: I7639364820785f5a877917afbbb62ab9c09eff00 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138781 Reviewed-by: Mike Fairhurst <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 24c8021 commit fa2df01

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,6 @@ class InfoBuilder {
173173
return 'The value of the expression is nullable';
174174
}
175175

176-
// Text indicating the type of nullable value found.
177-
String nullableValue;
178-
if (node is NullLiteral) {
179-
nullableValue = "an explicit 'null'";
180-
} else if (origin.kind == EdgeOriginKind.dynamicAssignment) {
181-
nullableValue = 'a dynamic value, which is nullable';
182-
} else {
183-
nullableValue = 'a nullable value';
184-
}
185-
186176
if (origin.kind == EdgeOriginKind.listLengthConstructor) {
187177
return 'A length is specified in the "List()" constructor and the list '
188178
'items are initialized to null';
@@ -205,6 +195,16 @@ class InfoBuilder {
205195
'$lineNumber, which implicitly returns null.';
206196
}
207197

198+
// Text indicating the type of nullable value found.
199+
String nullableValue;
200+
if (node is NullLiteral) {
201+
nullableValue = "an explicit 'null'";
202+
} else if (origin.kind == EdgeOriginKind.dynamicAssignment) {
203+
nullableValue = 'a dynamic value, which is nullable';
204+
} else {
205+
nullableValue = 'a nullable value';
206+
}
207+
208208
/// If the [node] is inside the return expression for a function body,
209209
/// return the function body. Otherwise return `null`.
210210
FunctionBody findFunctionBody() {
@@ -263,16 +263,19 @@ class InfoBuilder {
263263
return 'This field is initialized to $nullableValue';
264264
}
265265
return 'This variable is initialized to $nullableValue';
266-
} else if (node is ConstructorDeclaration &&
267-
origin.kind == EdgeOriginKind.fieldNotInitialized) {
268-
String constructorName =
269-
node.declaredElement.enclosingElement.displayName;
270-
if (node.declaredElement.displayName.isNotEmpty) {
271-
constructorName =
272-
'$constructorName.${node.declaredElement.displayName}';
266+
} else if (origin.kind == EdgeOriginKind.fieldNotInitialized) {
267+
if (node is ConstructorDeclaration) {
268+
String constructorName =
269+
node.declaredElement.enclosingElement.displayName;
270+
if (node.declaredElement.displayName.isNotEmpty) {
271+
constructorName =
272+
'$constructorName.${node.declaredElement.displayName}';
273+
}
274+
return "The constructor '$constructorName' does not initialize this "
275+
'field in its initializer list';
276+
} else {
277+
return 'This field is not initialized';
273278
}
274-
return "The constructor '$constructorName' does not initialize this "
275-
'field in its initializer list';
276279
}
277280

278281
String enclosingMemberDescription = buildEnclosingMemberDescription(node);

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,6 @@ class B extends A {
13441344
details: ['A nullable value is assigned']);
13451345
}
13461346

1347-
@FailingTest(issue: 'https://dartbug.com/40773')
13481347
Future<void> test_parameter_fromOverriddenField_explicit() async {
13491348
UnitInfo unit = await buildInfoForSingleTestFile('''
13501349
class A {
@@ -1366,8 +1365,7 @@ void f(A a) => a.m = null;
13661365
List<RegionInfo> regions = unit.fixRegions;
13671366
expect(regions, hasLength(2));
13681367
assertRegion(region: regions[0], offset: 15, details: [
1369-
// TODO(mfairhurst): Implement something similar to this error message
1370-
'No initializer is given',
1368+
'This field is not initialized',
13711369
"An explicit 'null' is assigned in the function 'f'",
13721370
]);
13731371
assertRegion(region: regions[1], offset: 61, details: [
@@ -1823,7 +1821,6 @@ class C {
18231821
assertDetail(detail: region.details[2], offset: 70, length: 3);
18241822
}
18251823

1826-
@FailingTest(issue: 'https://dartbug.com/40773')
18271824
Future<void> test_uninitializedMember() async {
18281825
UnitInfo unit = await buildInfoForSingleTestFile('''
18291826
class C {
@@ -1837,10 +1834,11 @@ class C {
18371834
List<RegionInfo> regions = unit.fixRegions;
18381835
expect(regions, hasLength(1));
18391836
expect(regions[0].details, isNotEmpty);
1840-
// disabled so that it won't interfere with @FailingTest annotation.
1841-
//assertRegion(region: regions[0], offset: 15, length: 1, details: [
1842-
// 'This field is not initialized and is therefore made nullable'
1843-
//]);
1837+
assertRegion(
1838+
region: regions[0],
1839+
offset: 15,
1840+
length: 1,
1841+
details: ['This field is not initialized']);
18441842
}
18451843

18461844
Future<void> test_uninitializedVariable_notLate_uninitializedUse() async {

0 commit comments

Comments
 (0)