@@ -681,14 +681,15 @@ class Typer extends Namer
681681 */
682682 def tryWithClassTag (tree : Typed , pt : Type )(implicit ctx : Context ): Tree = tree.tpt.tpe.dealias match {
683683 case tref : TypeRef if ! tref.symbol.isClass && ! ctx.isAfterTyper && ! (tref =:= pt) =>
684- require(ctx.mode.is(Mode .Pattern ))
685- inferImplicit(defn.ClassTagClass .typeRef.appliedTo(tref),
686- EmptyTree , tree.tpt.span)(ctx.retractMode(Mode .Pattern )) match {
687- case SearchSuccess (clsTag, _, _) =>
688- typed(untpd.Apply (untpd.TypedSplice (clsTag), untpd.TypedSplice (tree.expr)), pt)
689- case _ =>
690- tree
684+ def withTag (tpe : Type ): Option [Tree ] = {
685+ inferImplicit(tpe, EmptyTree , tree.tpt.span)(ctx.retractMode(Mode .Pattern )) match
686+ case SearchSuccess (typeTest, _, _) =>
687+ Some (typed(untpd.Apply (untpd.TypedSplice (typeTest), untpd.TypedSplice (tree.expr)), pt))
688+ case _ =>
689+ None
691690 }
691+ withTag(defn.TypeTestClass .typeRef.appliedTo(pt, tref)).orElse(
692+ withTag(defn.ClassTagClass .typeRef.appliedTo(tref))).getOrElse(tree)
692693 case _ => tree
693694 }
694695
@@ -1491,8 +1492,8 @@ class Typer extends Namer
14911492 val body1 = typed(tree.body, pt1)
14921493 body1 match {
14931494 case UnApply (fn, Nil , arg :: Nil )
1494- if fn.symbol.exists && fn.symbol.owner == defn.ClassTagClass && ! body1.tpe.isError =>
1495- // A typed pattern `x @ (e: T)` with an implicit `ctag: ClassTag[T]`
1495+ if fn.symbol.exists && ( fn.symbol.owner == defn.ClassTagClass || fn.symbol.owner.derivesFrom(defn. TypeTestClass )) && ! body1.tpe.isError =>
1496+ // A typed pattern `x @ (e: T)` with an implicit `ctag: ClassTag[T]` or `ctag: TypeTest[T]`
14961497 // was rewritten to `x @ ctag(e)` by `tryWithClassTag`.
14971498 // Rewrite further to `ctag(x @ e)`
14981499 tpd.cpy.UnApply (body1)(fn, Nil ,
0 commit comments