@@ -778,7 +778,10 @@ class Typer extends Namer
778
778
val matched = ascription(tpt1, isWildcard = true )
779
779
// special case for an abstract type that comes with a class tag
780
780
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
782
785
// no check for matchability if TestTest was applied
783
786
checkMatchable(pt, tree.srcPos, pattern = true )
784
787
result
@@ -802,13 +805,15 @@ class Typer extends Namer
802
805
inferImplicit(tpe, EmptyTree , tree.tpt.span)
803
806
) match
804
807
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
+ }
806
811
case _ =>
807
812
None
808
813
}
809
814
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)
812
817
if tag.symbol.owner == defn.ClassTagClass && config.Feature .sourceVersion.isAtLeast(config.SourceVersion .`3.1`) then
813
818
report.warning(" Use of `scala.reflect.ClassTag` for type testing may be unsound. Consider using `scala.reflect.TypeTest` instead." , tree.srcPos)
814
819
tag
0 commit comments