Skip to content

Commit e0ad2fe

Browse files
committed
Introduce AppliedType
AppliedType will be used for all type applications, higher-kinded or not. That is, eventually, it will replace HKApply and also the encodings of type applications as refined types. This commit introduces AppliedType and adapts baseType computations to take it into account. AppliedType is not yet constructed anywhere, however.
1 parent 4f60767 commit e0ad2fe

13 files changed

+167
-64
lines changed

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

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ object SymDenotations {
699699
| is not a subclass of ${owner.showLocated} where target is defined""")
700700
else if (
701701
!( isType // allow accesses to types from arbitrary subclasses fixes #4737
702-
|| pre.baseTypeRef(cls).exists // ??? why not use derivesFrom ???
702+
|| pre.derivesFrom(cls)
703703
|| isConstructor
704704
|| (owner is ModuleClass) // don't perform this check for static members
705705
))
@@ -1266,10 +1266,10 @@ object SymDenotations {
12661266
private[this] var myMemberCache: LRUCache[Name, PreDenotation] = null
12671267
private[this] var myMemberCachePeriod: Period = Nowhere
12681268

1269-
/** A cache from types T to baseTypeRef(T, C) */
1270-
type BaseTypeRefMap = java.util.HashMap[CachedType, Type]
1271-
private[this] var myBaseTypeRefCache: BaseTypeRefMap = null
1272-
private[this] var myBaseTypeRefCachePeriod: Period = Nowhere
1269+
/** A cache from types T to baseType(T, C) */
1270+
type BaseTypeMap = java.util.HashMap[CachedType, Type]
1271+
private[this] var myBaseTypeCache: BaseTypeMap = null
1272+
private[this] var myBaseTypeCachePeriod: Period = Nowhere
12731273

12741274
private var baseDataCache: BaseData = BaseData.None
12751275
private var memberNamesCache: MemberNames = MemberNames.None
@@ -1282,14 +1282,14 @@ object SymDenotations {
12821282
myMemberCache
12831283
}
12841284

1285-
private def baseTypeRefCache(implicit ctx: Context): BaseTypeRefMap = {
1286-
if (myBaseTypeRefCachePeriod != ctx.period &&
1287-
(myBaseTypeRefCachePeriod.runId != ctx.runId ||
1288-
ctx.phases(myBaseTypeRefCachePeriod.phaseId).sameParentsStartId != ctx.phase.sameParentsStartId)) {
1289-
myBaseTypeRefCache = new BaseTypeRefMap
1290-
myBaseTypeRefCachePeriod = ctx.period
1285+
private def baseTypeCache(implicit ctx: Context): BaseTypeMap = {
1286+
if (myBaseTypeCachePeriod != ctx.period &&
1287+
(myBaseTypeCachePeriod.runId != ctx.runId ||
1288+
ctx.phases(myBaseTypeCachePeriod.phaseId).sameParentsStartId != ctx.phase.sameParentsStartId)) {
1289+
myBaseTypeCache = new BaseTypeMap
1290+
myBaseTypeCachePeriod = ctx.period
12911291
}
1292-
myBaseTypeRefCache
1292+
myBaseTypeCache
12931293
}
12941294

12951295
private def invalidateBaseDataCache() = {
@@ -1302,9 +1302,9 @@ object SymDenotations {
13021302
memberNamesCache = MemberNames.None
13031303
}
13041304

1305-
def invalidateBaseTypeRefCache() = {
1306-
myBaseTypeRefCache = null
1307-
myBaseTypeRefCachePeriod = Nowhere
1305+
def invalidateBaseTypeCache() = {
1306+
myBaseTypeCache = null
1307+
myBaseTypeCachePeriod = Nowhere
13081308
}
13091309

13101310
override def copyCaches(from: SymDenotation, phase: Phase)(implicit ctx: Context): this.type = {
@@ -1313,7 +1313,7 @@ object SymDenotations {
13131313
if (from.memberNamesCache.isValidAt(phase)) memberNamesCache = from.memberNamesCache
13141314
if (from.baseDataCache.isValidAt(phase)) {
13151315
baseDataCache = from.baseDataCache
1316-
myBaseTypeRefCache = from.baseTypeRefCache
1316+
myBaseTypeCache = from.baseTypeCache
13171317
}
13181318
case _ =>
13191319
}
@@ -1581,11 +1581,11 @@ object SymDenotations {
15811581
raw.filterExcluded(excluded).asSeenFrom(pre).toDenot(pre)
15821582
}
15831583

1584-
/** Compute tp.baseTypeRef(this) */
1585-
final def baseTypeRefOf(tp: Type)(implicit ctx: Context): Type = {
1584+
/** Compute tp.baseType(this) */
1585+
final def baseTypeOf(tp: Type)(implicit ctx: Context): Type = {
15861586

15871587
def foldGlb(bt: Type, ps: List[Type]): Type = ps match {
1588-
case p :: ps1 => foldGlb(bt & baseTypeRefOf(p), ps1)
1588+
case p :: ps1 => foldGlb(bt & baseTypeOf(p), ps1)
15891589
case _ => bt
15901590
}
15911591

@@ -1597,7 +1597,7 @@ object SymDenotations {
15971597
* and this changes subtyping relations. As a shortcut, we do not
15981598
* cache ErasedValueType at all.
15991599
*/
1600-
def isCachable(tp: Type, btrCache: BaseTypeRefMap): Boolean = {
1600+
def isCachable(tp: Type, btrCache: BaseTypeMap): Boolean = {
16011601
def inCache(tp: Type) = btrCache.containsKey(tp)
16021602
tp match {
16031603
case _: TypeErasure.ErasedValueType => false
@@ -1609,12 +1609,12 @@ object SymDenotations {
16091609
}
16101610
}
16111611

1612-
def computeBaseTypeRefOf(tp: Type): Type = {
1612+
def computeBaseTypeOf(tp: Type): Type = {
16131613
Stats.record("computeBaseTypeOf")
1614-
if (symbol.isStatic && tp.derivesFrom(symbol))
1614+
if (symbol.isStatic && tp.derivesFrom(symbol) && symbol.typeParams.isEmpty)
16151615
symbol.typeRef
16161616
else tp match {
1617-
case tp: TypeRef =>
1617+
case tp: RefType =>
16181618
val subcls = tp.symbol
16191619
if (subcls eq symbol)
16201620
tp
@@ -1623,31 +1623,31 @@ object SymDenotations {
16231623
if (cdenot.baseClassSet contains symbol) foldGlb(NoType, tp.parents)
16241624
else NoType
16251625
case _ =>
1626-
baseTypeRefOf(tp.superType)
1626+
baseTypeOf(tp.superType)
16271627
}
16281628
case tp: TypeProxy =>
1629-
baseTypeRefOf(tp.superType)
1629+
baseTypeOf(tp.superType)
16301630
case AndType(tp1, tp2) =>
1631-
baseTypeRefOf(tp1) & baseTypeRefOf(tp2)
1631+
baseTypeOf(tp1) & baseTypeOf(tp2)
16321632
case OrType(tp1, tp2) =>
1633-
baseTypeRefOf(tp1) | baseTypeRefOf(tp2)
1633+
baseTypeOf(tp1) | baseTypeOf(tp2)
16341634
case JavaArrayType(_) if symbol == defn.ObjectClass =>
16351635
this.typeRef
16361636
case _ =>
16371637
NoType
16381638
}
16391639
}
16401640

1641-
/*>|>*/ ctx.debugTraceIndented(s"$tp.baseTypeRef($this)") /*<|<*/ {
1641+
/*>|>*/ ctx.debugTraceIndented(s"$tp.baseType($this)") /*<|<*/ {
16421642
tp match {
16431643
case tp: CachedType =>
1644-
val btrCache = baseTypeRefCache
1644+
val btrCache = baseTypeCache
16451645
try {
16461646
var basetp = btrCache get tp
16471647
if (basetp == null) {
16481648
btrCache.put(tp, NoPrefix)
1649-
basetp = computeBaseTypeRefOf(tp)
1650-
if (isCachable(tp, baseTypeRefCache)) btrCache.put(tp, basetp)
1649+
basetp = computeBaseTypeOf(tp)
1650+
if (isCachable(tp, baseTypeCache)) btrCache.put(tp, basetp)
16511651
else btrCache.remove(tp)
16521652
} else if (basetp == NoPrefix)
16531653
throw CyclicReference(this)
@@ -1659,7 +1659,7 @@ object SymDenotations {
16591659
throw ex
16601660
}
16611661
case _ =>
1662-
computeBaseTypeRefOf(tp)
1662+
computeBaseTypeOf(tp)
16631663
}
16641664
}
16651665
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ object TypeApplications {
7676
}
7777

7878
def unapply(tp: Type)(implicit ctx: Context): Option[TypeRef] = tp match {
79-
case tp @ HKTypeLambda(tparams, AppliedType(fn: TypeRef, args)) if (args == tparams.map(_.toArg)) => Some(fn)
79+
case tp @ HKTypeLambda(tparams, AnyAppliedType(fn: TypeRef, args)) if (args == tparams.map(_.toArg)) => Some(fn)
8080
case _ => None
8181
}
8282
}
@@ -87,7 +87,7 @@ object TypeApplications {
8787
*
8888
* where v_i, p_i are the variances and names of the type parameters of T.
8989
*/
90-
object AppliedType {
90+
object AnyAppliedType {
9191
def apply(tp: Type, args: List[Type])(implicit ctx: Context): Type = tp.appliedTo(args)
9292

9393
def unapply(tp: Type)(implicit ctx: Context): Option[(Type, List[Type])] = tp match {
@@ -111,6 +111,8 @@ object TypeApplications {
111111
None
112112
}
113113
collectArgs(tycon.typeParams, refinements, new mutable.ListBuffer[Type])
114+
case AppliedType(tycon, args) =>
115+
Some((tycon, args))
114116
case HKApply(tycon, args) =>
115117
Some((tycon, args))
116118
case _ =>
@@ -408,7 +410,7 @@ class TypeApplications(val self: Type) extends AnyVal {
408410
if (!args.exists(_.isInstanceOf[TypeBounds])) {
409411
val followAlias = Config.simplifyApplications && {
410412
dealiased.resType match {
411-
case AppliedType(tyconBody, dealiasedArgs) =>
413+
case AnyAppliedType(tyconBody, dealiasedArgs) =>
412414
// Reduction should not affect type inference when it's
413415
// just eta-reduction (ignoring variance annotations).
414416
// See i2201*.scala for examples where more aggressive
@@ -421,7 +423,7 @@ class TypeApplications(val self: Type) extends AnyVal {
421423
else HKApply(self, args)
422424
}
423425
else dealiased.resType match {
424-
case AppliedType(tycon, args1) if tycon.safeDealias ne tycon =>
426+
case AnyAppliedType(tycon, args1) if tycon.safeDealias ne tycon =>
425427
// In this case we should always dealias since we cannot handle
426428
// higher-kinded applications to wildcard arguments.
427429
dealiased
@@ -521,7 +523,7 @@ class TypeApplications(val self: Type) extends AnyVal {
521523
* Existential types in arguments are returned as TypeBounds instances.
522524
*/
523525
final def baseTypeWithArgs(base: Symbol)(implicit ctx: Context): Type = ctx.traceIndented(s"btwa ${self.show} wrt $base", core, show = true) {
524-
def default = self.baseTypeRef(base).appliedTo(baseArgInfos(base))
526+
def default = self.baseTypeTycon(base).appliedTo(baseArgInfos(base))
525527
def isExpandedTypeParam(sym: Symbol) = sym.is(TypeParam) && sym.name.is(ExpandedName)
526528
self match {
527529
case tp: TypeRef =>
@@ -573,7 +575,7 @@ class TypeApplications(val self: Type) extends AnyVal {
573575
* Existential types in arguments are returned as TypeBounds instances.
574576
*/
575577
final def argInfos(implicit ctx: Context): List[Type] = self match {
576-
case AppliedType(tycon, args) => args
578+
case AnyAppliedType(tycon, args) => args
577579
case _ => Nil
578580
}
579581

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
368368
case _ =>
369369
val cls2 = tp2.symbol
370370
if (cls2.isClass) {
371-
val base = tp1.baseTypeRef(cls2)
371+
val base = tp1.baseType(cls2)
372372
if (base.exists && (base ne tp1)) return isSubType(base, tp2)
373373
if (cls2 == defn.SingletonClass && tp1.isStable) return true
374374
}
@@ -713,7 +713,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
713713
def liftToBase(bcs: List[ClassSymbol]): Boolean = bcs match {
714714
case bc :: bcs1 =>
715715
classBounds.exists(bc.derivesFrom) &&
716-
tyconOK(tp1w.baseTypeRef(bc), tp1w.baseArgInfos(bc)) ||
716+
tyconOK(tp1w.baseTypeTycon(bc), tp1w.baseArgInfos(bc)) ||
717717
liftToBase(bcs1)
718718
case _ =>
719719
false
@@ -771,7 +771,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
771771
tycon1 match {
772772
case param1: TypeParamRef =>
773773
def canInstantiate = tp2 match {
774-
case AppliedType(tycon2, args2) =>
774+
case AnyAppliedType(tycon2, args2) =>
775775
tryInstantiate(param1, tycon2.ensureHK) && isSubArgs(args1, args2, tycon2.typeParams)
776776
case _ =>
777777
false
@@ -810,7 +810,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
810810
val classBounds = tp2.classSymbols
811811
def recur(bcs: List[ClassSymbol]): Boolean = bcs match {
812812
case bc :: bcs1 =>
813-
val baseRef = tp1.baseTypeRef(bc)
813+
val baseRef = tp1.baseTypeTycon(bc)
814814
(classBounds.exists(bc.derivesFrom) &&
815815
variancesConform(baseRef.typeParams, tparams) &&
816816
p(baseRef.appliedTo(tp1.baseArgInfos(bc)))

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
6666
else pre match {
6767
case pre: SuperType => toPrefix(pre.thistpe, cls, thiscls)
6868
case _ =>
69-
if (thiscls.derivesFrom(cls) && pre.baseTypeRef(thiscls).exists) {
69+
if (thiscls.derivesFrom(cls) && pre.baseType(thiscls).exists) { // ??? why not derivesFrom ???
7070
if (theMap != null && theMap.currentVariance <= 0 && !isLegalPrefix(pre)) {
7171
ctx.base.unsafeNonvariant = ctx.runId
7272
pre match {
@@ -79,7 +79,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
7979
else if ((pre.termSymbol is Package) && !(thiscls is Package))
8080
toPrefix(pre.select(nme.PACKAGE), cls, thiscls)
8181
else
82-
toPrefix(pre.baseTypeRef(cls).normalizedPrefix, cls.owner, thiscls)
82+
toPrefix(pre.baseType(cls).normalizedPrefix, cls.owner, thiscls)
8383
}
8484
}
8585

@@ -256,7 +256,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
256256
val doms = dominators(commonBaseClasses, Nil)
257257
def baseTp(cls: ClassSymbol): Type = {
258258
val base =
259-
if (tp1.typeParams.nonEmpty) tp.baseTypeRef(cls)
259+
if (tp1.typeParams.nonEmpty) tp.baseTypeTycon(cls)
260260
else tp.baseTypeWithArgs(cls)
261261
base.mapReduceOr(identity)(mergeRefined)
262262
}

0 commit comments

Comments
 (0)