@@ -718,6 +718,36 @@ trait Implicits { self: Typer =>
718718 EmptyTree
719719 }
720720
721+ lazy val synthesizedTypeTest : SpecialHandler =
722+ (formal, span) => implicit ctx => formal.argInfos match {
723+ case arg1 :: arg2 :: Nil if ! defn.isBottomClass(arg2.typeSymbol) =>
724+ val tp1 = fullyDefinedType(arg1, " TypeTest argument" , span)
725+ val tp2 = fullyDefinedType(arg2, " TypeTest argument" , span)
726+ val sym2 = tp2.typeSymbol
727+ if tp1 <:< tp2 then
728+ ref(defn.TypeTestModule_identity ).appliedToType(tp2).withSpan(span)
729+ else if sym2 == defn.AnyValClass || sym2 == defn.AnyRefAlias || sym2 == defn.ObjectClass then
730+ EmptyTree
731+ else
732+ // Generate SAM: (s: <tp1>) => if s.isInstanceOf[s.type & <tp2>] then Some(s.asInstanceOf[s.type & <tp2>]) else None
733+ def body (args : List [Tree ]): Tree = {
734+ val arg :: Nil = args
735+ val t = arg.tpe & tp2
736+ If (
737+ arg.select(defn.Any_isInstanceOf ).appliedToType(t),
738+ ref(defn.SomeClass .companionModule.termRef).select(nme.apply)
739+ .appliedToType(t)
740+ .appliedTo(arg.select(nme.asInstanceOf_).appliedToType(t)),
741+ ref(defn.NoneModule ))
742+ }
743+ val tpe = MethodType (List (nme.s))(_ => List (tp1), mth => defn.OptionClass .typeRef.appliedTo(mth.newParamRef(0 ) & tp2))
744+ val meth = ctx.newSymbol(ctx.owner, nme.ANON_FUN , Synthetic | Method , tpe, coord = span)
745+ val typeTestType = defn.TypeTestClass .typeRef.appliedTo(List (tp1, tp2))
746+ Closure (meth, tss => body(tss.head).changeOwner(ctx.owner, meth), targetType = typeTestType).withSpan(span)
747+ case _ =>
748+ EmptyTree
749+ }
750+
721751 /** Synthesize the tree for `'[T]` for an implicit `scala.quoted.Type[T]`.
722752 * `T` is deeply dealiased to avoid references to local type aliases.
723753 */
@@ -1094,6 +1124,7 @@ trait Implicits { self: Typer =>
10941124 if (mySpecialHandlers == null )
10951125 mySpecialHandlers = List (
10961126 defn.ClassTagClass -> synthesizedClassTag,
1127+ defn.TypeTestClass -> synthesizedTypeTest,
10971128 defn.QuotedTypeClass -> synthesizedTypeTag,
10981129 defn.EqlClass -> synthesizedEql,
10991130 defn.TupledFunctionClass -> synthesizedTupleFunction,
0 commit comments