@@ -901,28 +901,32 @@ class Typer extends Namer
901
901
* exists, rewrite to `tt(e)`.
902
902
* @pre We are in pattern-matching mode (Mode.Pattern)
903
903
*/
904
- def tryWithTypeTest (tree : Typed , pt : Type )(using Context ): Tree = tree.tpt.tpe.dealias match {
905
- case tref : TypeRef if ! tref.symbol.isClass && ! ctx.isAfterTyper && ! (tref =:= pt) =>
906
- def withTag ( tpe : Type ) : Option [ Tree ] = {
907
- require(ctx.mode.is( Mode .Pattern ))
908
- withoutMode( Mode . Pattern )(
909
- inferImplicit(tpe, EmptyTree , tree.tpt.span)
910
- ) match
911
- case SearchSuccess (clsTag, _, _, _) =>
912
- withMode( Mode . InTypeTest ) {
913
- Some (typed(untpd. Apply (untpd. TypedSplice (clsTag), untpd. TypedSplice (tree.expr)), pt))
914
- }
915
- case _ =>
916
- None
917
- }
918
- val tag = withTag(defn.TypeTestClass .typeRef.appliedTo(pt, tref ))
919
- .orElse(withTag(defn.ClassTagClass .typeRef.appliedTo(tref )))
904
+ def tryWithTypeTest (tree : Typed , pt : Type )(using Context ): Tree =
905
+ def withTag ( tpe : Type ) : Option [ Tree ] = {
906
+ require(ctx.mode.is( Mode . Pattern ))
907
+ withoutMode( Mode .Pattern )(
908
+ inferImplicit(tpe, EmptyTree , tree.tpt.span)
909
+ ) match
910
+ case SearchSuccess (clsTag, _, _, _) =>
911
+ withMode( Mode . InTypeTest ) {
912
+ Some (typed(untpd. Apply (untpd. TypedSplice (clsTag), untpd. TypedSplice (tree.expr)), pt))
913
+ }
914
+ case _ =>
915
+ None
916
+ }
917
+ def tagged ( tpe : Type ) = {
918
+ val tag = withTag(defn.TypeTestClass .typeRef.appliedTo(pt, tpe ))
919
+ .orElse(withTag(defn.ClassTagClass .typeRef.appliedTo(tpe )))
920
920
.getOrElse(tree)
921
- if tag.symbol.owner == defn.ClassTagClass && config.Feature .sourceVersion.isAtLeast(config.SourceVersion .future) then
921
+ if tag.symbol.maybeOwner == defn.ClassTagClass && config.Feature .sourceVersion.isAtLeast(config.SourceVersion .future) then
922
922
report.warning(" Use of `scala.reflect.ClassTag` for type testing may be unsound. Consider using `scala.reflect.TypeTest` instead." , tree.srcPos)
923
923
tag
924
- case _ => tree
925
- }
924
+ }
925
+ tree.tpt.tpe.dealias match {
926
+ case tpe @ AppliedType (tref : TypeRef , _) if ! tref.symbol.isClass && ! ctx.isAfterTyper && ! (tpe =:= pt) => tagged(tpe)
927
+ case tref : TypeRef if ! tref.symbol.isClass && ! ctx.isAfterTyper && ! (tref =:= pt) => tagged(tref)
928
+ case _ => tree
929
+ }
926
930
927
931
928
932
def typedNamedArg (tree : untpd.NamedArg , pt : Type )(using Context ): NamedArg = {
0 commit comments