Skip to content

Commit f69935b

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: stop creating union edges for inferred types.
Fixes #38341. Change-Id: I77e6bebc28ca917a13e5fd9958f5a7b3e0284834 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125448 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 6a8aae0 commit f69935b

4 files changed

Lines changed: 53 additions & 75 deletions

File tree

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

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -350,65 +350,60 @@ class C {
350350
}
351351

352352
test_listAndSetLiteralTypeArgument() async {
353-
// TODO(srawlins): Simplify this test with `var x` once #38341 is fixed.
354353
UnitInfo unit = await buildInfoForSingleTestFile('''
355354
void f() {
356355
String s = null;
357-
List<String> x = <String>["hello", s];
358-
Set<String> y = <String>{"hello", s};
356+
var x = <String>["hello", s];
357+
var y = <String>{"hello", s};
359358
}
360359
''', migratedContent: '''
361360
void f() {
362361
String? s = null;
363-
List<String?> x = <String?>["hello", s];
364-
Set<String?> y = <String?>{"hello", s};
362+
var x = <String?>["hello", s];
363+
var y = <String?>{"hello", s};
365364
}
366365
''');
367366
List<RegionInfo> regions = unit.fixRegions;
368-
expect(regions, hasLength(5));
367+
expect(regions, hasLength(3));
369368
// regions[0] is the `String? s` fix.
370-
// regions[1] is the `List<String?> x` fix.
371369
assertRegion(
372-
region: regions[2],
373-
offset: 58,
370+
region: regions[1],
371+
offset: 48,
374372
details: ["This list is initialized with a nullable value on line 3"]);
375-
assertDetail(detail: regions[2].details[0], offset: 67, length: 1);
376-
// regions[3] is the `Set<String?> y` fix.
373+
assertDetail(detail: regions[1].details[0], offset: 58, length: 1);
377374
assertRegion(
378-
region: regions[4],
379-
offset: 100,
375+
region: regions[2],
376+
offset: 81,
380377
details: ["This set is initialized with a nullable value on line 4"]);
381-
assertDetail(detail: regions[4].details[0], offset: 107, length: 1);
378+
assertDetail(detail: regions[2].details[0], offset: 90, length: 1);
382379
}
383380

384381
test_listLiteralTypeArgument_collectionIf() async {
385-
// TODO(srawlins): Simplify this test with `var x` once #38341 is fixed.
386382
UnitInfo unit = await buildInfoForSingleTestFile('''
387383
void f() {
388384
String s = null;
389-
List<String> x = <String>[
385+
var x = <String>[
390386
"hello",
391387
if (1 == 2) s
392388
];
393389
}
394390
''', migratedContent: '''
395391
void f() {
396392
String? s = null;
397-
List<String?> x = <String?>[
393+
var x = <String?>[
398394
"hello",
399395
if (1 == 2) s
400396
];
401397
}
402398
''');
403399
List<RegionInfo> regions = unit.fixRegions;
404-
expect(regions, hasLength(3));
400+
expect(regions, hasLength(2));
405401
// regions[0] is the `String? s` fix.
406-
// regions[1] is the `List<String?> x` fix.
407402
assertRegion(
408-
region: regions[2],
409-
offset: 58,
403+
region: regions[1],
404+
offset: 48,
410405
details: ["This list is initialized with a nullable value on line 5"]);
411-
assertDetail(detail: regions[2].details[0], offset: 88, length: 1);
406+
assertDetail(detail: regions[1].details[0], offset: 79, length: 1);
412407
}
413408

414409
test_localVariable() async {
@@ -436,35 +431,32 @@ void f() {
436431
}
437432

438433
test_mapLiteralTypeArgument() async {
439-
// TODO(srawlins): Simplify this test with `var x` once #38341 is fixed.
440434
UnitInfo unit = await buildInfoForSingleTestFile('''
441435
void f() {
442436
String s = null;
443-
Map<String, bool> x = <String, bool>{"hello": false, s: true};
444-
Map<bool, String> y = <bool, String>{false: "hello", true: s};
437+
var x = <String, bool>{"hello": false, s: true};
438+
var y = <bool, String>{false: "hello", true: s};
445439
}
446440
''', migratedContent: '''
447441
void f() {
448442
String? s = null;
449-
Map<String?, bool> x = <String?, bool>{"hello": false, s: true};
450-
Map<bool, String?> y = <bool, String?>{false: "hello", true: s};
443+
var x = <String?, bool>{"hello": false, s: true};
444+
var y = <bool, String?>{false: "hello", true: s};
451445
}
452446
''');
453447
List<RegionInfo> regions = unit.fixRegions;
454-
expect(regions, hasLength(5));
448+
expect(regions, hasLength(3));
455449
// regions[0] is the `String? s` fix.
456-
// regions[1] is the `Map<String?, bool> x` fix.
457450
assertRegion(
458-
region: regions[2],
459-
offset: 63,
451+
region: regions[1],
452+
offset: 48,
460453
details: ["This map is initialized with a nullable value on line 3"]);
461-
assertDetail(detail: regions[2].details[0], offset: 85, length: 1);
462-
// regions[3] is the `Map<bool, String?> y` fix.
454+
assertDetail(detail: regions[1].details[0], offset: 71, length: 1);
463455
assertRegion(
464-
region: regions[4],
465-
offset: 136,
456+
region: regions[2],
457+
offset: 106,
466458
details: ["This map is initialized with a nullable value on line 4"]);
467-
assertDetail(detail: regions[4].details[0], offset: 156, length: 1);
459+
assertDetail(detail: regions[2].details[0], offset: 128, length: 1);
468460
}
469461

470462
test_nonNullableType_assert() async {
@@ -872,35 +864,33 @@ class A {
872864
}
873865

874866
test_setLiteralTypeArgument_nestedList() async {
875-
// TODO(srawlins): Simplify this test with `var x` once #38341 is fixed.
876867
UnitInfo unit = await buildInfoForSingleTestFile('''
877868
void f() {
878869
String s = null;
879-
Set<List<String>> x = <List<String>>{
870+
var x = <List<String>>{
880871
["hello"],
881872
if (1 == 2) [s]
882873
};
883874
}
884875
''', migratedContent: '''
885876
void f() {
886877
String? s = null;
887-
Set<List<String?>> x = <List<String?>>{
878+
var x = <List<String?>>{
888879
["hello"],
889880
if (1 == 2) [s]
890881
};
891882
}
892883
''');
893884
List<RegionInfo> regions = unit.fixRegions;
894-
expect(regions, hasLength(3));
885+
expect(regions, hasLength(2));
895886
// regions[0] is the `String? s` fix.
896-
// regions[1] is the `Set<List<String?>> x` fix.
897887
assertRegion(
898-
region: regions[2],
899-
offset: 68,
888+
region: regions[1],
889+
offset: 53,
900890
details: ["This set is initialized with a nullable value on line 5"]);
901891
// TODO(srawlins): Actually, this is marking the `[s]`, but I think only
902892
// `s` should be marked. Minor bug for now.
903-
assertDetail(detail: regions[2].details[0], offset: 101, length: 3);
893+
assertDetail(detail: regions[1].details[0], offset: 87, length: 3);
904894
}
905895

906896
test_topLevelVariable() async {

pkg/nnbd_migration/lib/src/edge_builder.dart

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,18 +1406,7 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
14061406
_flowAnalysis.initialize(declaredElement);
14071407
}
14081408
var destinationType = getOrComputeElementType(declaredElement);
1409-
if (typeAnnotation == null) {
1410-
var initializerType = initializer.accept(this);
1411-
if (initializerType == null) {
1412-
throw StateError(
1413-
'No type computed for ${initializer.runtimeType} '
1414-
'(${initializer.toSource()}) offset=${initializer.offset}');
1415-
}
1416-
_unionDecoratedTypes(initializerType, destinationType,
1417-
InitializerInferenceOrigin(source, variable));
1418-
} else {
1419-
_handleAssignment(initializer, destinationType: destinationType);
1420-
}
1409+
_handleAssignment(initializer, destinationType: destinationType);
14211410
}
14221411
} finally {
14231412
if (isTopLevel) {

pkg/nnbd_migration/test/api_test.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ main() {
100100
await _checkSingleFileChanges(content, expected);
101101
}
102102

103-
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/38341')
104103
test_back_propagation_stops_at_implicitly_typed_variables() async {
105104
var content = '''
106105
class C {
@@ -1399,6 +1398,7 @@ class C {
13991398
await _checkSingleFileChanges(content, expected);
14001399
}
14011400

1401+
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/39404')
14021402
test_field_type_inferred() async {
14031403
var content = '''
14041404
int f() => null;
@@ -1409,15 +1409,14 @@ class C {
14091409
}
14101410
}
14111411
''';
1412-
// The type of x is inferred from its initializer, so it is non-nullable,
1413-
// even though we try to assign a nullable value to it. So a null check
1414-
// must be added.
1412+
// The type of x is inferred as non-nullable from its initializer, but we
1413+
// try to assign a nullable value to it. So an explicit type must be added.
14151414
var expected = '''
14161415
int? f() => null;
14171416
class C {
1418-
var x = 1;
1417+
int? x = 1;
14191418
void g() {
1420-
x = f()!;
1419+
x = f();
14211420
}
14221421
}
14231422
''';
@@ -2028,6 +2027,7 @@ main() {
20282027
await _checkSingleFileChanges(content, expected);
20292028
}
20302029

2030+
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/39404')
20312031
test_localVariable_type_inferred() async {
20322032
var content = '''
20332033
int f() => null;
@@ -2036,14 +2036,13 @@ void main() {
20362036
x = f();
20372037
}
20382038
''';
2039-
// The type of x is inferred from its initializer, so it is non-nullable,
2040-
// even though we try to assign a nullable value to it. So a null check
2041-
// must be added.
2039+
// The type of x is inferred as non-nullable from its initializer, but we
2040+
// try to assign a nullable value to it. So an explicit type must be added.
20422041
var expected = '''
20432042
int? f() => null;
20442043
void main() {
2045-
var x = 1;
2046-
x = f()!;
2044+
int? x = 1;
2045+
x = f();
20472046
}
20482047
''';
20492048
await _checkSingleFileChanges(content, expected);
@@ -2983,6 +2982,7 @@ Object? g() => f();
29832982
await _checkSingleFileChanges(content, expected);
29842983
}
29852984

2985+
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/39404')
29862986
test_topLevelVariable_type_inferred() async {
29872987
var content = '''
29882988
int f() => null;
@@ -2991,14 +2991,13 @@ void main() {
29912991
x = f();
29922992
}
29932993
''';
2994-
// The type of x is inferred from its initializer, so it is non-nullable,
2995-
// even though we try to assign a nullable value to it. So a null check
2996-
// must be added.
2994+
// The type of x is inferred as non-nullable from its initializer, but we
2995+
// try to assign a nullable value to it. So an explicit type must be added.
29972996
var expected = '''
29982997
int? f() => null;
2999-
var x = 1;
2998+
int? x = 1;
30002999
void main() {
3001-
x = f()!;
3000+
x = f();
30023001
}
30033002
''';
30043003
await _checkSingleFileChanges(content, expected);

pkg/nnbd_migration/test/edge_builder_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,7 @@ class C {
17731773
''');
17741774
var xType =
17751775
variables.decoratedElementType(findNode.simple('x').staticElement);
1776-
assertUnion(xType.node, decoratedTypeAnnotation('int').node);
1776+
assertEdge(decoratedTypeAnnotation('int').node, xType.node, hard: false);
17771777
}
17781778

17791779
test_fieldFormalParameter_function_typed() async {
@@ -2979,7 +2979,7 @@ main() {
29792979
''');
29802980
var xType =
29812981
variables.decoratedElementType(findNode.simple('x').staticElement);
2982-
assertUnion(xType.node, decoratedTypeAnnotation('int').node);
2982+
assertEdge(decoratedTypeAnnotation('int').node, xType.node, hard: false);
29832983
}
29842984

29852985
test_method_parameterType_inferred() async {
@@ -5310,7 +5310,7 @@ var x = f();
53105310
''');
53115311
var xType =
53125312
variables.decoratedElementType(findNode.simple('x').staticElement);
5313-
assertUnion(xType.node, decoratedTypeAnnotation('int').node);
5313+
assertEdge(decoratedTypeAnnotation('int').node, xType.node, hard: false);
53145314
}
53155315

53165316
test_type_argument_explicit_bound() async {

0 commit comments

Comments
 (0)