Skip to content

Commit

Permalink
Fix scala#9613: Handle F-bounds with LazyRef
Browse files Browse the repository at this point in the history
  • Loading branch information
liufengyun committed Sep 16, 2020
1 parent 186fa57 commit 08c1b29
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -659,17 +659,28 @@ object TypeOps:
*/
private def instantiateToSubType(tp1: NamedType, tp2: Type)(using Context): Type = {
/** expose abstract type references to their bounds or tvars according to variance */
class ApproximateTypeParams(using Context) extends TypeMap {
val approximateTypeParams = new TypeMap {
val boundTypeParams = util.HashMap[TypeRef, TypeVar]()

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

case tp: TypeRef if !tp.symbol.isClass =>
def lo = apply(tp.underlying.loBound)
def hi = apply(tp.underlying.hiBound)
boundTypeParams.getOrElseUpdate(tp, newTypeVar(TypeBounds(lo, hi)))
def lo = LazyRef(apply(tp.underlying.loBound))
def hi = LazyRef(apply(tp.underlying.hiBound))
val lookup = boundTypeParams.lookup(tp)
if lookup != null then lookup
else
val tv = newTypeVar(TypeBounds(lo, hi))
boundTypeParams(tp) = tv
// Force lazy ref eagerly using current context
// Otherwise, the lazy ref will be forced with a unknown context,
// which causes a problem in tests/patmat/i3645e.scala
lo.ref
hi.ref
tv
end if

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

def approximateTypeParams(tp: Type)(using Context) = new ApproximateTypeParams().apply(tp)

// Prefix inference, replace `p.C.this.Child` with `X.Child` where `X <: p.C`
// Note: we need to strip ThisType in `p` recursively.
//
Expand Down

0 comments on commit 08c1b29

Please sign in to comment.