Skip to content

Commit 40c60d8

Browse files
dwijnandWojciechMazur
authored andcommitted
Only strip under unsafeNulls
[Cherry-picked ab240f1]
1 parent 70ead5c commit 40c60d8

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

+5-2
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
@@ -859,7 +862,7 @@ object SpaceEngine {
859862
})
860863

861864
def checkExhaustivity(m: Match)(using Context): Unit = trace(i"checkExhaustivity($m)") {
862-
val selTyp = toUnderlying(m.selector.tpe.stripNull()).dealias
865+
val selTyp = toUnderlying(m.selector.tpe.stripUnsafeNulls()).dealias
863866
val targetSpace = trace(i"targetSpace($selTyp)")(project(selTyp))
864867

865868
val patternSpace = Or(m.cases.foldLeft(List.empty[Space]) { (acc, x) =>
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`
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+
}
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)