Skip to content

Commit 25c720f

Browse files
committed
Mad baseType more context dependent
Roll the functionality of contextualBaseType into baseType itself.
1 parent 4d95963 commit 25c720f

File tree

3 files changed

+18
-34
lines changed

3 files changed

+18
-34
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,15 +1636,16 @@ object SymDenotations {
16361636
* cache ErasedValueType at all.
16371637
*/
16381638
def isCachable(tp: Type, btrCache: BaseTypeMap): Boolean = {
1639-
def inCache(tp: Type) = btrCache.containsKey(tp)
1639+
def inCache(tp: Type) = btrCache.containsKey(tp) && isCachable(tp, btrCache)
16401640
tp match {
1641-
case _: TypeErasure.ErasedValueType => false
16421641
case tp: TypeRef if tp.symbol.isClass => true
16431642
case tp: TypeVar => tp.inst.exists && inCache(tp.inst)
1643+
case tp: TypeParamRef if ctx.typerState.constraint.contains(tp) => false
16441644
//case tp: TypeProxy => inCache(tp.underlying) // disabled, can re-enable insyead of last two lines for performance testing
16451645
case tp: TypeProxy => isCachable(tp.underlying, btrCache)
16461646
case tp: AndType => isCachable(tp.tp1, btrCache) && isCachable(tp.tp2, btrCache)
16471647
case tp: OrType => isCachable(tp.tp1, btrCache) && isCachable(tp.tp2, btrCache)
1648+
case _: TypeErasure.ErasedValueType => false
16481649
case _ => true
16491650
}
16501651
}
@@ -1685,6 +1686,8 @@ object SymDenotations {
16851686
case tparams: List[Symbol @unchecked] =>
16861687
baseTypeOf(tycon).subst(tparams, args)
16871688
}
1689+
case tp: TypeParamRef =>
1690+
baseTypeOf(ctx.typeComparer.bounds(tp).hi)
16881691
case tp: TypeProxy =>
16891692
baseTypeOf(tp.superType)
16901693
case AndType(tp1, tp2) =>
@@ -1709,19 +1712,24 @@ object SymDenotations {
17091712
tp.stripTypeVar match {
17101713
case tp: CachedType =>
17111714
val btrCache = baseTypeCache
1715+
if (!isCachable(tp, btrCache))
1716+
computeBaseTypeOf(tp)
1717+
else
17121718
try {
1713-
var basetp = btrCache get tp
1719+
var basetp = btrCache.get(tp)
17141720
if (basetp == null) {
17151721
btrCache.put(tp, NoPrefix)
17161722
basetp = computeBaseTypeOf(tp)
1717-
if (!basetp.exists) Stats.record("base type miss")
1718-
if (isCachable(tp, btrCache)) {
1719-
if (basetp.exists) Stats.record("cached base type hit")
1720-
else Stats.record("cached base type miss")
1723+
if (basetp.exists) {
1724+
Stats.record("cached base type exists")
17211725
btrCache.put(tp, basetp)
17221726
}
1723-
else btrCache.remove(tp)
1724-
} else if (basetp `eq` NoPrefix)
1727+
else {
1728+
Stats.record("cached base type missing")
1729+
btrCache.remove(tp)
1730+
}
1731+
}
1732+
else if (basetp `eq` NoPrefix)
17251733
throw CyclicReference(this)
17261734
basetp
17271735
}

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -863,30 +863,6 @@ object Types {
863863
}
864864
}
865865

866-
/** Like basetype, but follow context bounds */
867-
final def contextualBaseType(cls: ClassSymbol)(implicit ctx: Context): Type = {
868-
def loop(tp: Type): Type = tp match {
869-
case tp: TypeRef =>
870-
val sym = tp.symbol
871-
if (sym.isClass) tp.baseType(cls) else loop(tp.superType): @tailrec
872-
case tp: AppliedType =>
873-
tp.baseType(cls)
874-
case tp: TypeParamRef =>
875-
loop(ctx.typeComparer.bounds(tp).hi): @tailrec
876-
case tp: TypeProxy =>
877-
loop(tp.superType): @tailrec
878-
case tp: AndType =>
879-
loop(tp.tp1) & loop(tp.tp2): @tailrec
880-
case tp: OrType =>
881-
loop(tp.tp1) | loop(tp.tp2): @tailrec
882-
case tp: JavaArrayType =>
883-
if (cls == defn.ObjectClass) cls.typeRef else NoType
884-
case _ =>
885-
NoType
886-
}
887-
loop(this)
888-
}
889-
890866
def & (that: Type)(implicit ctx: Context): Type = track("&") {
891867
ctx.typeComparer.glb(this, that)
892868
}

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ trait Implicits { self: Typer =>
652652
arg
653653
case fail @ SearchFailure(failed) =>
654654
def trySpecialCase(cls: ClassSymbol, handler: Type => Tree, ifNot: => Tree) = {
655-
val base = formalValue.contextualBaseType(cls)
655+
val base = formalValue.baseType(cls)
656656
if (base <:< formalValue) {
657657
// With the subtype test we enforce that the searched type `formalValue` is of the right form
658658
handler(base).orElse(ifNot)

0 commit comments

Comments
 (0)