@@ -692,15 +692,24 @@ object TypeOps:
692
692
*/
693
693
private def instantiateToSubType (tp1 : NamedType , tp2 : Type )(using Context ): Type = {
694
694
// In order for a child type S to qualify as a valid subtype of the parent
695
- // T, we need to test whether it is possible S <: T. Therefore, we replace
696
- // type parameters in T with tvars, and see if the subtyping is true.
697
- val approximateTypeParams = new TypeMap {
695
+ // T, we need to test whether it is possible S <: T.
696
+ //
697
+ // The check is different from subtype checking due to type parameters and
698
+ // `this`. We perform the following operations to approximate the parameters:
699
+ //
700
+ // 1. Replace type parameters in T with tvars
701
+ // 2. Replace `A.this.C` with `A#C` (see tests/patmat/i12681.scala)
702
+ //
703
+ val approximateParent = new TypeMap {
698
704
val boundTypeParams = util.HashMap [TypeRef , TypeVar ]()
699
705
700
706
def apply (tp : Type ): Type = tp.dealias match {
701
707
case _ : MatchType =>
702
708
tp // break cycles
703
709
710
+ case ThisType (tref : TypeRef ) if ! tref.symbol.isStaticOwner =>
711
+ tref
712
+
704
713
case tp : TypeRef if ! tp.symbol.isClass =>
705
714
def lo = LazyRef .of(apply(tp.underlying.loBound))
706
715
def hi = LazyRef .of(apply(tp.underlying.hiBound))
@@ -787,7 +796,7 @@ object TypeOps:
787
796
// we manually patch subtyping check instead of changing TypeComparer.
788
797
// See tests/patmat/i3645b.scala
789
798
def parentQualify (tp1 : Type , tp2 : Type ) = tp1.classSymbol.info.parents.exists { parent =>
790
- parent.argInfos.nonEmpty && approximateTypeParams (parent) <:< tp2
799
+ parent.argInfos.nonEmpty && approximateParent (parent) <:< tp2
791
800
}
792
801
793
802
def instantiate (): Type = {
@@ -797,8 +806,8 @@ object TypeOps:
797
806
798
807
if (protoTp1 <:< tp2) instantiate()
799
808
else {
800
- val protoTp2 = approximateTypeParams (tp2)
801
- if (protoTp1 <:< protoTp2 || parentQualify(protoTp1, protoTp2 )) instantiate()
809
+ val approxTp2 = approximateParent (tp2)
810
+ if (protoTp1 <:< approxTp2 || parentQualify(protoTp1, approxTp2 )) instantiate()
802
811
else NoType
803
812
}
804
813
}
0 commit comments