Skip to content

Commit

Permalink
Strip null on the scrutinee
Browse files Browse the repository at this point in the history
Without that, we end up with either a flexible type or a `| Null`.
  • Loading branch information
dwijnand committed Jul 5, 2024
1 parent eb90deb commit 2ed2826
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ object SpaceEngine {
/** Return the underlying type of non-module, non-constant, non-enum case singleton types.
* Also widen ExprType to its result type, and rewrap any annotation wrappers.
* For example, with `val opt = None`, widen `opt.type` to `None.type`. */
def toUnderlying(tp: Type)(using Context): Type = trace(i"toUnderlying($tp)")(tp match {
def toUnderlying(tp: Type)(using Context): Type = trace(i"toUnderlying($tp ${tp.className})")(tp match {
case _: ConstantType => tp
case tp: TermRef if tp.symbol.is(Module) => tp
case tp: TermRef if tp.symbol.isAllOf(EnumCase) => tp
Expand All @@ -846,7 +846,7 @@ object SpaceEngine {
})

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

val patternSpace = Or(m.cases.foldLeft(List.empty[Space]) { (acc, x) =>
Expand Down Expand Up @@ -879,7 +879,7 @@ object SpaceEngine {
def checkReachability(m: Match)(using Context): Unit = trace(i"checkReachability($m)") {
val cases = m.cases.toIndexedSeq

val selTyp = toUnderlying(m.selector.tpe).dealias
val selTyp = toUnderlying(m.selector.tpe.stripNull()).dealias

val isNullable = selTyp.classSymbol.isNullableClass
val targetSpace = trace(i"targetSpace($selTyp)")(if isNullable
Expand Down
13 changes: 13 additions & 0 deletions tests/warn/i20132.future-Left.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//> using options -Yexplicit-nulls -Yno-flexible-types

import scala.language.unsafeNulls

import java.util.concurrent.CompletableFuture
import scala.jdk.CollectionConverters._

class Test1:
def m1 =
val fut: CompletableFuture[Either[String, List[String]]] = ???
fut.thenApply:
case Right(edits: List[String]) => edits.asJava
case Left(error: String) => throw new Exception(error)

0 comments on commit 2ed2826

Please sign in to comment.