File tree Expand file tree Collapse file tree 3 files changed +15
-5
lines changed
compiler/src/dotty/tools/dotc Expand file tree Collapse file tree 3 files changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -116,4 +116,7 @@ object Mode {
116
116
117
117
/** Are we typechecking the rhs of an extension method? */
118
118
val InExtensionMethod : Mode = newMode(26 , " InExtensionMethod" )
119
+
120
+ /** Are we resolving a TypeTest node? */
121
+ val InTypeTest : Mode = newMode(27 , " InTypeTest" )
119
122
}
Original file line number Diff line number Diff line change @@ -1158,7 +1158,8 @@ trait Applications extends Compatibility {
1158
1158
def typedUnApply (tree : untpd.Apply , selType : Type )(using Context ): Tree = {
1159
1159
record(" typedUnApply" )
1160
1160
val Apply (qual, args) = tree
1161
- checkMatchable(selType, tree.srcPos, pattern = true )
1161
+ if ctx.mode.is(Mode .InTypeTest ) then
1162
+ checkMatchable(selType, tree.srcPos, pattern = true )
1162
1163
1163
1164
def notAnExtractor (tree : Tree ): Tree =
1164
1165
// prefer inner errors
Original file line number Diff line number Diff line change @@ -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
@@ -806,9 +809,12 @@ class Typer extends Namer
806
809
case _ =>
807
810
None
808
811
}
809
- val tag = withTag(defn.TypeTestClass .typeRef.appliedTo(pt, tref))
810
- .orElse(withTag(defn.ClassTagClass .typeRef.appliedTo(tref)))
811
- .getOrElse(tree)
812
+ val tag =
813
+ withMode(Mode .InTypeTest ) {
814
+ withTag(defn.TypeTestClass .typeRef.appliedTo(pt, tref))
815
+ .orElse(withTag(defn.ClassTagClass .typeRef.appliedTo(tref)))
816
+ .getOrElse(tree)
817
+ }
812
818
if tag.symbol.owner == defn.ClassTagClass && config.Feature .sourceVersion.isAtLeast(config.SourceVersion .`3.1`) then
813
819
report.warning(" Use of `scala.reflect.ClassTag` for type testing may be unsound. Consider using `scala.reflect.TypeTest` instead." , tree.srcPos)
814
820
tag
You can’t perform that action at this time.
0 commit comments