@@ -778,7 +778,10 @@ class Typer extends Namer
778778 val matched = ascription(tpt1, isWildcard = true )
779779 // special case for an abstract type that comes with a class tag
780780 val result = tryWithTypeTest(matched, pt)
781- if (result eq matched) && pt != defn.ImplicitScrutineeTypeRef then
781+ if (result eq matched)
782+ && pt != defn.ImplicitScrutineeTypeRef
783+ && ! (pt <:< tpt1.tpe)
784+ then
782785 // no check for matchability if TestTest was applied
783786 checkMatchable(pt, tree.srcPos, pattern = true )
784787 result
@@ -802,13 +805,15 @@ class Typer extends Namer
802805 inferImplicit(tpe, EmptyTree , tree.tpt.span)
803806 ) match
804807 case SearchSuccess (clsTag, _, _) =>
805- Some (typed(untpd.Apply (untpd.TypedSplice (clsTag), untpd.TypedSplice (tree.expr)), pt))
808+ withMode(Mode .InTypeTest ) {
809+ Some (typed(untpd.Apply (untpd.TypedSplice (clsTag), untpd.TypedSplice (tree.expr)), pt))
810+ }
806811 case _ =>
807812 None
808813 }
809814 val tag = withTag(defn.TypeTestClass .typeRef.appliedTo(pt, tref))
810- .orElse(withTag(defn.ClassTagClass .typeRef.appliedTo(tref)))
811- .getOrElse(tree)
815+ .orElse(withTag(defn.ClassTagClass .typeRef.appliedTo(tref)))
816+ .getOrElse(tree)
812817 if tag.symbol.owner == defn.ClassTagClass && config.Feature .sourceVersion.isAtLeast(config.SourceVersion .`3.1`) then
813818 report.warning(" Use of `scala.reflect.ClassTag` for type testing may be unsound. Consider using `scala.reflect.TypeTest` instead." , tree.srcPos)
814819 tag
0 commit comments