Skip to content

Commit

Permalink
Only strip under unsafeNulls
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Aug 5, 2024
1 parent 2cc0e45 commit 4306063
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -808,12 +808,15 @@ object SpaceEngine {
doShow(s)
}

extension (self: Type) private def stripUnsafeNulls()(using Context): Type =
if Nullables.unsafeNullsEnabled then self.stripNull() else self

private def exhaustivityCheckable(sel: Tree)(using Context): Boolean = trace(i"exhaustivityCheckable($sel ${sel.className})") {
val seen = collection.mutable.Set.empty[Symbol]

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

def checkExhaustivity(m: Match)(using Context): Unit = trace(i"checkExhaustivity($m)") {
val selTyp = toUnderlying(m.selector.tpe.stripNull()).dealias
val selTyp = toUnderlying(m.selector.tpe.stripUnsafeNulls()).dealias
val targetSpace = trace(i"targetSpace($selTyp)")(project(selTyp))

val patternSpace = Or(m.cases.foldLeft(List.empty[Space]) { (acc, x) =>
Expand Down
8 changes: 8 additions & 0 deletions tests/warn/i20132.stream-Tuple2.safeNulls.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- [E029] Pattern Match Exhaustivity Warning: tests/warn/i20132.stream-Tuple2.safeNulls.scala:8:24 ---------------------
8 | xs.asJava.forEach { case (a, b) => // warn
| ^
| match may not be exhaustive.
|
| It would fail on pattern case: _: Null
|
| longer explanation available when compiling with `-explain`
12 changes: 12 additions & 0 deletions tests/warn/i20132.stream-Tuple2.safeNulls.fixed.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//> using options -Yexplicit-nulls -Yno-flexible-types

import scala.jdk.CollectionConverters.*

class Test2:
def t1: Unit = {
val xs = List.empty[(String, String)]
xs.asJava.forEach {
case (a, b) => ()
case null => ()
}
}
11 changes: 11 additions & 0 deletions tests/warn/i20132.stream-Tuple2.safeNulls.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//> using options -Yexplicit-nulls -Yno-flexible-types

import scala.jdk.CollectionConverters.*

class Test2:
def t1: Unit = {
val xs = List.empty[(String, String)]
xs.asJava.forEach { case (a, b) => // warn
()
}
}

0 comments on commit 4306063

Please sign in to comment.