Skip to content

Commit 9fbfe31

Browse files
[nnbd_migration] Add tests for important real-world exact nullable case
Add tests verifying #40590 so that we don't regress it. Change-Id: I74105cc1e6a33b75d7ff67dc07b7cc913040dc8b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/135840 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Mike Fairhurst <[email protected]>
1 parent f825e7a commit 9fbfe31

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pkg/analyzer/lib/src/test_utilities/mock_sdk.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ abstract class Set<E> implements Iterable<E> {
436436
factory Set.of(Iterable<E> elements) => null;
437437
438438
Set<R> cast<R>();
439+
440+
void add(E element) {}
439441
}
440442
441443
class StackTrace {}

pkg/nnbd_migration/test/api_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,6 +2948,33 @@ class C {
29482948
{path2: file2, path1: file1}, {path1: expected1, path2: expected2});
29492949
}
29502950

2951+
Future<void> test_literals_maintain_nullability() async {
2952+
// See #40590. Without exact nullability, this would migrate to
2953+
// `List<int?> list = <int>[1, 2]`. While the function of exact nullability
2954+
// may change, this case should continue to work.
2955+
var content = r'''
2956+
void f() {
2957+
List<int> list = [1, 2];
2958+
list.add(null);
2959+
Set<int> set_ = {1, 2};
2960+
set_.add(null);
2961+
Map<int, int> map = {1: 2};
2962+
map[null] = null;
2963+
}
2964+
''';
2965+
var expected = r'''
2966+
void f() {
2967+
List<int?> list = [1, 2];
2968+
list.add(null);
2969+
Set<int?> set_ = {1, 2};
2970+
set_.add(null);
2971+
Map<int?, int?> map = {1: 2};
2972+
map[null] = null;
2973+
}
2974+
''';
2975+
await _checkSingleFileChanges(content, expected);
2976+
}
2977+
29512978
Future<void> test_local_function() async {
29522979
var content = '''
29532980
int f(int i) {

0 commit comments

Comments
 (0)