Skip to content

Commit 364a114

Browse files
committed
Fix scala#9631: Handle F-bounds with LazyRef
1 parent ee5b3ac commit 364a114

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

compiler/src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -659,17 +659,28 @@ object TypeOps:
659659
*/
660660
private def instantiateToSubType(tp1: NamedType, tp2: Type)(using Context): Type = {
661661
/** expose abstract type references to their bounds or tvars according to variance */
662-
class ApproximateTypeParams(using Context) extends TypeMap {
662+
val approximateTypeParams = new TypeMap {
663663
val boundTypeParams = util.HashMap[TypeRef, TypeVar]()
664664

665665
def apply(tp: Type): Type = tp.dealias match {
666666
case _: MatchType =>
667667
tp // break cycles
668668

669669
case tp: TypeRef if !tp.symbol.isClass =>
670-
def lo = apply(tp.underlying.loBound)
671-
def hi = apply(tp.underlying.hiBound)
672-
boundTypeParams.getOrElseUpdate(tp, newTypeVar(TypeBounds(lo, hi)))
670+
def lo = LazyRef(apply(tp.underlying.loBound))
671+
def hi = LazyRef(apply(tp.underlying.hiBound))
672+
val lookup = boundTypeParams.lookup(tp)
673+
if lookup != null then lookup
674+
else
675+
val tv = newTypeVar(TypeBounds(lo, hi))
676+
boundTypeParams(tp) = tv
677+
// Force lazy ref eagerly using current context
678+
// Otherwise, the lazy ref will be forced with a unknown context,
679+
// which causes a problem in tests/patmat/i3645e.scala
680+
lo.ref
681+
hi.ref
682+
tv
683+
end if
673684

674685
case AppliedType(tycon: TypeRef, _) if !tycon.dealias.typeSymbol.isClass =>
675686
// Type inference cannot handle X[Y] <:< Int
@@ -690,8 +701,6 @@ object TypeOps:
690701
}
691702
}
692703

693-
def approximateTypeParams(tp: Type)(using Context) = new ApproximateTypeParams().apply(tp)
694-
695704
// Prefix inference, replace `p.C.this.Child` with `X.Child` where `X <: p.C`
696705
// Note: we need to strip ThisType in `p` recursively.
697706
//

0 commit comments

Comments
 (0)