Skip to content

Commit

Permalink
Fix #11541: Specialize ClassTag[T] in exhaustivity check (#17385)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand authored May 4, 2023
2 parents 8a055d6 + 4d32116 commit be32eb9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ class Definitions {

@tu lazy val ReflectPackageClass: Symbol = requiredPackage("scala.reflect.package").moduleClass
@tu lazy val ClassTagClass: ClassSymbol = requiredClass("scala.reflect.ClassTag")
@tu lazy val ClassTagClass_unapply: Symbol = ClassTagClass.requiredMethod("unapply")
@tu lazy val ClassTagModule: Symbol = ClassTagClass.companionModule
@tu lazy val ClassTagModule_apply: Symbol = ClassTagModule.requiredMethod(nme.apply)

Expand Down
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,15 @@ object SpaceEngine {

/** Whether the extractor covers the given type */
def covers(unapp: TermRef, scrutineeTp: Type, argLen: Int)(using Context): Boolean =
SpaceEngine.isIrrefutable(unapp, argLen) || unapp.symbol == defn.TypeTest_unapply && {
SpaceEngine.isIrrefutable(unapp, argLen)
|| unapp.symbol == defn.TypeTest_unapply && {
val AppliedType(_, _ :: tp :: Nil) = unapp.prefix.widen.dealias: @unchecked
scrutineeTp <:< tp
}
|| unapp.symbol == defn.ClassTagClass_unapply && {
val AppliedType(_, tp :: Nil) = unapp.prefix.widen.dealias: @unchecked
scrutineeTp <:< tp
}

/** Decompose a type into subspaces -- assume the type can be decomposed */
def decompose(tp: Type)(using Context): List[Type] = trace(i"decompose($tp)", debug) {
Expand Down
13 changes: 13 additions & 0 deletions tests/patmat/i11541.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.reflect.ClassTag

class Test:
type A

given ClassTag[A] = ???

var a: A | Null = null

a match { //WARNING: match may not be exhaustive. It would fail on pattern case: _: A
case null =>
case a: A =>
}

0 comments on commit be32eb9

Please sign in to comment.