Skip to content

Commit 7b541da

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[cfe] Use exhaustiveness in computing nullability of intersection
This is a follow-up for https://dart-review.googlesource.com/c/sdk/+/436620/comment/cddbbd38_1d23184c/ Change-Id: Ib473db14c6088ceb99034a6762ec75edccb4b5c1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/438723 Commit-Queue: Johnni Winther <[email protected]> Auto-Submit: Chloe Stefantsova <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent fb2d117 commit 7b541da

File tree

1 file changed

+30
-46
lines changed

1 file changed

+30
-46
lines changed

pkg/kernel/lib/src/ast/types.dart

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,53 +1888,37 @@ class IntersectionType extends DartType {
18881888
// | ? | (!) | (?) | (%) |
18891889
// | % | ! | % | % |
18901890

1891-
if (lhsNullability == Nullability.nullable &&
1892-
rhsNullability == Nullability.nonNullable) {
1893-
return Nullability.nonNullable;
1891+
switch ((lhsNullability, rhsNullability)) {
1892+
case (Nullability.nullable, _):
1893+
return rhsNullability;
1894+
1895+
// Intersection with a non-nullable type always yields a non-nullable
1896+
// type, as it's the most restrictive kind of types.
1897+
case (Nullability.nonNullable, _):
1898+
case (_, Nullability.nonNullable):
1899+
return Nullability.nonNullable;
1900+
1901+
// If the nullability of LHS is 'undetermined', the nullability of the
1902+
// intersection is also 'undetermined' if RHS is 'undetermined' or
1903+
// nullable.
1904+
//
1905+
// Consider the following example:
1906+
//
1907+
// class A<X extends Object?, Y extends X> {
1908+
// foo(X x) {
1909+
// if (x is Y) {
1910+
// x = null; // Compile-time error. Consider X = Y = int.
1911+
// Object a = x; // Compile-time error. Consider X = Y = int?.
1912+
// }
1913+
// if (x is int?) {
1914+
// x = null; // Compile-time error. Consider X = int.
1915+
// Object b = x; // Compile-time error. Consider X = int?.
1916+
// }
1917+
// }
1918+
// }
1919+
case (Nullability.undetermined, _):
1920+
return Nullability.undetermined;
18941921
}
1895-
1896-
if (lhsNullability == Nullability.nullable &&
1897-
rhsNullability == Nullability.nullable) {
1898-
return Nullability.nullable;
1899-
}
1900-
1901-
if (lhsNullability == Nullability.nullable &&
1902-
rhsNullability == Nullability.undetermined) {
1903-
return Nullability.undetermined;
1904-
}
1905-
1906-
// Intersection with a non-nullable type always yields a non-nullable type,
1907-
// as it's the most restrictive kind of types.
1908-
if (lhsNullability == Nullability.nonNullable ||
1909-
rhsNullability == Nullability.nonNullable) {
1910-
return Nullability.nonNullable;
1911-
}
1912-
1913-
// If the nullability of LHS is 'undetermined', the nullability of the
1914-
// intersection is also 'undetermined' if RHS is 'undetermined' or
1915-
// nullable.
1916-
//
1917-
// Consider the following example:
1918-
//
1919-
// class A<X extends Object?, Y extends X> {
1920-
// foo(X x) {
1921-
// if (x is Y) {
1922-
// x = null; // Compile-time error. Consider X = Y = int.
1923-
// Object a = x; // Compile-time error. Consider X = Y = int?.
1924-
// }
1925-
// if (x is int?) {
1926-
// x = null; // Compile-time error. Consider X = int.
1927-
// Object b = x; // Compile-time error. Consider X = int?.
1928-
// }
1929-
// }
1930-
// }
1931-
if (lhsNullability == Nullability.undetermined ||
1932-
rhsNullability == Nullability.undetermined) {
1933-
return Nullability.undetermined;
1934-
}
1935-
1936-
throw new StateError("Unsupported combination '${lhsNullability}' (LHS) "
1937-
"and '${rhsNullability}' (RHS).");
19381922
}
19391923

19401924
@override

0 commit comments

Comments
 (0)