@@ -692,15 +692,24 @@ object TypeOps:
692692 */
693693 private def instantiateToSubType (tp1 : NamedType , tp2 : Type )(using Context ): Type = {
694694 // 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 {
698704 val boundTypeParams = util.HashMap [TypeRef , TypeVar ]()
699705
700706 def apply (tp : Type ): Type = tp.dealias match {
701707 case _ : MatchType =>
702708 tp // break cycles
703709
710+ case ThisType (tref : TypeRef ) if ! tref.symbol.isStaticOwner =>
711+ tref
712+
704713 case tp : TypeRef if ! tp.symbol.isClass =>
705714 def lo = LazyRef .of(apply(tp.underlying.loBound))
706715 def hi = LazyRef .of(apply(tp.underlying.hiBound))
@@ -787,7 +796,7 @@ object TypeOps:
787796 // we manually patch subtyping check instead of changing TypeComparer.
788797 // See tests/patmat/i3645b.scala
789798 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
791800 }
792801
793802 def instantiate (): Type = {
@@ -797,8 +806,8 @@ object TypeOps:
797806
798807 if (protoTp1 <:< tp2) instantiate()
799808 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()
802811 else NoType
803812 }
804813 }
0 commit comments