Skip to content

Commit 4bde1f1

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: test to make sure exact nullability isn't applied too much.
This is a regression test that would have caught the mistake in https://dart-review.googlesource.com/c/sdk/+/135351 (see that CL for additional details). Change-Id: Ideaf83df6deae3f6688fc89cfd8b3568344c5d4d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/135660 Reviewed-by: Mike Fairhurst <mfairhurst@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
1 parent ed705e8 commit 4bde1f1

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

pkg/nnbd_migration/test/api_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,37 @@ void h() {
15441544
await _checkSingleFileChanges(content, expected);
15451545
}
15461546

1547+
Future<void> test_exact_nullability_counterexample() async {
1548+
var content = '''
1549+
void f(List<int> x) {
1550+
x.add(1);
1551+
}
1552+
void g() {
1553+
f([null]);
1554+
}
1555+
void h(List<int> x) {
1556+
f(x);
1557+
}
1558+
''';
1559+
// The `null` in `g` causes `f`'s `x` argument to have type `List<int?>`.
1560+
// Even though `f` calls a method that uses `List`'s type parameter
1561+
// contravariantly (the `add` method), that is not sufficient to cause exact
1562+
// nullability propagation, since value passed to `add` has a
1563+
// non-nullable type. So nullability is *not* propagated back to `h`.
1564+
var expected = '''
1565+
void f(List<int?> x) {
1566+
x.add(1);
1567+
}
1568+
void g() {
1569+
f([null]);
1570+
}
1571+
void h(List<int> x) {
1572+
f(x);
1573+
}
1574+
''';
1575+
await _checkSingleFileChanges(content, expected);
1576+
}
1577+
15471578
Future<void> test_explicit_nullable_overrides_hard_edge() async {
15481579
var content = '''
15491580
int f(int/*?*/ i) => i + 1;

0 commit comments

Comments
 (0)