Skip to content

Commit ab240f1

Browse files
committed
Only strip under unsafeNulls
1 parent 5b425ee commit ab240f1

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,15 @@ object SpaceEngine {
808808
doShow(s)
809809
}
810810

811+
extension (self: Type) private def stripUnsafeNulls()(using Context): Type =
812+
if Nullables.unsafeNullsEnabled then self.stripNull() else self
813+
811814
private def exhaustivityCheckable(sel: Tree)(using Context): Boolean = trace(i"exhaustivityCheckable($sel ${sel.className})") {
812815
val seen = collection.mutable.Set.empty[Symbol]
813816

814817
// Possible to check everything, but be compatible with scalac by default
815818
def isCheckable(tp: Type): Boolean = trace(i"isCheckable($tp ${tp.className})"):
816-
val tpw = tp.widen.dealias.stripNull()
819+
val tpw = tp.widen.dealias.stripUnsafeNulls()
817820
val classSym = tpw.classSymbol
818821
classSym.is(Sealed) && !tpw.isLargeGenericTuple || // exclude large generic tuples from exhaustivity
819822
// requires an unknown number of changes to make work
@@ -860,7 +863,7 @@ object SpaceEngine {
860863
})
861864

862865
def checkExhaustivity(m: Match)(using Context): Unit = trace(i"checkExhaustivity($m)") {
863-
val selTyp = toUnderlying(m.selector.tpe.stripNull()).dealias
866+
val selTyp = toUnderlying(m.selector.tpe.stripUnsafeNulls()).dealias
864867
val targetSpace = trace(i"targetSpace($selTyp)")(project(selTyp))
865868

866869
val patternSpace = Or(m.cases.foldLeft(List.empty[Space]) { (acc, x) =>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i20132.stream-Tuple2.safeNulls.scala:8:24 ---------------------
2+
8 | xs.asJava.forEach { case (a, b) => // warn
3+
| ^
4+
| match may not be exhaustive.
5+
|
6+
| It would fail on pattern case: _: Null
7+
|
8+
| longer explanation available when compiling with `-explain`
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//> using options -Yexplicit-nulls -Yno-flexible-types
2+
3+
import scala.jdk.CollectionConverters.*
4+
5+
class Test2:
6+
def t1: Unit = {
7+
val xs = List.empty[(String, String)]
8+
xs.asJava.forEach {
9+
case (a, b) => ()
10+
case null => ()
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//> using options -Yexplicit-nulls -Yno-flexible-types
2+
3+
import scala.jdk.CollectionConverters.*
4+
5+
class Test2:
6+
def t1: Unit = {
7+
val xs = List.empty[(String, String)]
8+
xs.asJava.forEach { case (a, b) => // warn
9+
()
10+
}
11+
}

0 commit comments

Comments
 (0)