@@ -667,44 +667,34 @@ object TypeOps:
667667 def maxTypeMap (implicit ctx : Context ) = new AbstractTypeMap (maximize = true )
668668
669669 // Prefix inference, replace `p.C.this.Child` with `X.Child` where `X <: p.C`
670- // Note: we need to handle `p` recursively.
670+ // Note: we need to strip ThisType in `p` recursively.
671671 //
672672 // See tests/patmat/i3938.scala
673673 class InferPrefixMap extends TypeMap {
674- var tvars = Set .empty[ TypeVar ]
674+ var prefixTVar : Type = null
675675 def apply (tp : Type ): Type = tp match {
676676 case ThisType (tref : TypeRef ) if ! tref.symbol.isStaticOwner =>
677677 if (tref.symbol.is(Module ))
678678 TermRef (this (tref.prefix), tref.symbol.sourceModule)
679+ else if (prefixTVar != null )
680+ this (tref)
679681 else {
680- val bound = TypeRef (this (tref.prefix), tref.symbol)
681- val tvar = newTypeVar(TypeBounds .upper(bound))
682- tvars = tvars + tvar
683- tvar
682+ prefixTVar = WildcardType // prevent recursive call from assigning it
683+ prefixTVar = newTypeVar(TypeBounds .upper(this (tref)))
684+ prefixTVar
684685 }
685686 case tp => mapOver(tp)
686687 }
687688 }
688689
689- // replace uninstantiated type vars with WildcardType, check tests/patmat/3333.scala
690- def instUndetMap (implicit ctx : Context ) = new TypeMap {
691- def apply (t : Type ): Type = t match {
692- case tvar : TypeVar if ! tvar.isInstantiated =>
693- WildcardType (tvar.origin.underlying.bounds)
694- case tref : TypeRef if tref.typeSymbol.isPatternBound =>
695- WildcardType (tref.underlying.bounds)
696- case _ => mapOver(t)
697- }
698- }
699-
700690 val inferThisMap = new InferPrefixMap
701691 val tvars = tp1.typeParams.map { tparam => newTypeVar(tparam.paramInfo.bounds) }
702692 val protoTp1 = inferThisMap.apply(tp1).appliedTo(tvars)
703693
704694 val force = new ForceDegree .Value (
705695 tvar =>
706696 ! (ctx.typerState.constraint.entry(tvar.origin) `eq` tvar.origin.underlying) ||
707- inferThisMap.tvars.contains (tvar), // always instantiate prefix
697+ (tvar `eq` inferThisMap.prefixTVar ), // always instantiate prefix
708698 IfBottom .flip
709699 )
710700
@@ -721,13 +711,13 @@ object TypeOps:
721711
722712 if (protoTp1 <:< tp2) {
723713 maximizeType(protoTp1, NoSpan , fromScala2x = false )
724- instUndetMap.apply (protoTp1)
714+ wildApprox (protoTp1)
725715 }
726716 else {
727717 val protoTp2 = maxTypeMap.apply(tp2)
728718 if (protoTp1 <:< protoTp2 || parentQualify)
729719 if (isFullyDefined(AndType (protoTp1, protoTp2), force)) protoTp1
730- else instUndetMap.apply (protoTp1)
720+ else wildApprox (protoTp1)
731721 else {
732722 typr.println(s " $protoTp1 <:< $protoTp2 = false " )
733723 NoType
0 commit comments