Skip to content

Commit 2e77d34

Browse files
committed
Don't check for matchability if TypeTest is used
1 parent 5186a48 commit 2e77d34

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,17 @@ class Typer extends Namer
771771
def typedTpt = checkSimpleKinded(typedType(tree.tpt))
772772
def handlePattern: Tree = {
773773
val tpt1 = typedTpt
774-
if (!ctx.isAfterTyper && pt != defn.ImplicitScrutineeTypeRef)
775-
checkMatchable(pt, tree.srcPos, pattern = true)
774+
if !ctx.isAfterTyper && pt != defn.ImplicitScrutineeTypeRef then
776775
withMode(Mode.GadtConstraintInference) {
777776
TypeComparer.constrainPatternType(tpt1.tpe, pt)
778777
}
778+
val matched = ascription(tpt1, isWildcard = true)
779779
// special case for an abstract type that comes with a class tag
780-
tryWithTypeTest(ascription(tpt1, isWildcard = true), pt)
780+
val result = tryWithTypeTest(ascribed, pt)
781+
if (result eq matched) && pt != defn.ImplicitScrutineeTypeRef then
782+
// no check for matchability if TestTest was applied
783+
checkMatchable(pt, tree.srcPos, pattern = true)
784+
result
781785
}
782786
cases(
783787
ifPat = handlePattern,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.language.`3.1-migration`
2+
import scala.reflect.TypeTest
3+
4+
trait Foo:
5+
type X
6+
type Y <: X
7+
def x: X
8+
given TypeTest[X, Y] = ???
9+
10+
object Test:
11+
def test(foo: Foo): Unit =
12+
foo.x match
13+
case x: foo.Y =>

0 commit comments

Comments
 (0)