Skip to content

Commit 1f1d895

Browse files
committed
Move givenSelfType to ClassSymbol
Simplifies usage and removes some obscure code in Types
1 parent 140fac3 commit 1f1d895

File tree

8 files changed

+31
-44
lines changed

8 files changed

+31
-44
lines changed

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
889889
// to inner symbols of DefDef
890890
// todo: somehow handle.
891891

892-
def parents: List[Type] = tp.parents
892+
def parents: List[Type] = tp.parentsNEW
893893
}
894894

895895

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,14 @@ object SymDenotations {
13791379
NoSymbol
13801380
}
13811381

1382+
/** The explicitly given self type (self types of modules are assumed to be
1383+
* explcitly given here).
1384+
*/
1385+
def givenSelfType(implicit ctx: Context) = classInfo.selfInfo match {
1386+
case tp: Type => tp
1387+
case self: Symbol => self.info
1388+
}
1389+
13821390
// ------ class-specific operations -----------------------------------
13831391

13841392
private[this] var myThisType: Type = null

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,14 +1206,6 @@ object Types {
12061206
case _ => defn.AnyType
12071207
}
12081208

1209-
/** the self type of the underlying classtype */
1210-
def givenSelfType(implicit ctx: Context): Type = this match {
1211-
case tp: RefinedType => tp.wrapIfMember(tp.parent.givenSelfType)
1212-
case tp: ThisType => tp.tref.givenSelfType
1213-
case tp: TypeProxy => tp.superType.givenSelfType
1214-
case _ => NoType
1215-
}
1216-
12171209
/** The parameter types of a PolyType or MethodType, Empty list for others */
12181210
final def paramInfoss(implicit ctx: Context): List[List[Type]] = stripPoly match {
12191211
case mt: MethodType => mt.paramInfos :: mt.resultType.paramInfoss
@@ -1642,10 +1634,8 @@ object Types {
16421634
}
16431635

16441636
private def checkSymAssign(sym: Symbol)(implicit ctx: Context) = {
1645-
def selfTypeOf(sym: Symbol) = sym.owner.info match {
1646-
case info: ClassInfo => info.givenSelfType
1647-
case _ => NoType
1648-
}
1637+
def selfTypeOf(sym: Symbol) =
1638+
if (sym.isClass) sym.asClass.givenSelfType else NoType
16491639
assert(
16501640
(lastSymbol eq sym)
16511641
||
@@ -2244,7 +2234,7 @@ object Types {
22442234
if ((parent eq this.parent) && (refinedName eq this.refinedName) && (refinedInfo eq this.refinedInfo)) this
22452235
else RefinedType(parent, refinedName, refinedInfo)
22462236

2247-
/** Add this refinement to `parent`, provided If `refinedName` is a member of `parent`. */
2237+
/** Add this refinement to `parent`, provided `refinedName` is a member of `parent`. */
22482238
def wrapIfMember(parent: Type)(implicit ctx: Context): Type =
22492239
if (parent.member(refinedName).exists) derivedRefinedType(parent, refinedName, refinedInfo)
22502240
else parent
@@ -3418,7 +3408,7 @@ object Types {
34183408
if (selfTypeCache == null)
34193409
selfTypeCache = {
34203410
def fullRef = fullyAppliedRef
3421-
val given = givenSelfType
3411+
val given = cls.givenSelfType
34223412
val raw =
34233413
if (!given.exists) fullRef
34243414
else if (cls is Module) given
@@ -3429,14 +3419,6 @@ object Types {
34293419
selfTypeCache
34303420
}
34313421

3432-
/** The explicitly given self type (self types of modules are assumed to be
3433-
* explcitly given here).
3434-
*/
3435-
override def givenSelfType(implicit ctx: Context): Type = selfInfo match {
3436-
case tp: Type => tp
3437-
case self: Symbol => self.info
3438-
}
3439-
34403422
private var selfTypeCache: Type = null
34413423

34423424
private def fullyAppliedRef(base: Type, tparams: List[TypeSymbol])(implicit ctx: Context): Type = tparams match {
@@ -4275,7 +4257,7 @@ object Types {
42754257
}
42764258

42774259
private def otherReason(pre: Type)(implicit ctx: Context): String = pre match {
4278-
case pre: ThisType if pre.givenSelfType.exists =>
4260+
case pre: ThisType if pre.cls.givenSelfType.exists =>
42794261
i"\nor the self type of $pre might not contain all transitive dependencies"
42804262
case _ => ""
42814263
}

compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
179179
else dt.Module
180180
} else dt.ClassDef
181181

182-
val selfType = apiType(sym.classInfo.givenSelfType)
182+
val selfType = apiType(sym.givenSelfType)
183183

184184
val name = if (sym.is(ModuleClass)) sym.fullName.sourceModuleName else sym.fullName
185185

compiler/src/dotty/tools/dotc/transform/ExplicitSelf.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ class ExplicitSelf extends MiniPhaseTransform { thisTransform =>
3838
override def transformSelect(tree: Select)(implicit ctx: Context, info: TransformerInfo): Tree = tree match {
3939
case Select(thiz: This, name) if name.isTermName =>
4040
val cls = thiz.symbol.asClass
41-
val cinfo = cls.classInfo
42-
if (cinfo.givenSelfType.exists && !cls.derivesFrom(tree.symbol.owner))
43-
cpy.Select(tree)(thiz.asInstance(AndType(cinfo.selfType, thiz.tpe)), name)
41+
if (cls.givenSelfType.exists && !cls.derivesFrom(tree.symbol.owner))
42+
cpy.Select(tree)(thiz.asInstance(AndType(cls.classInfo.selfType, thiz.tpe)), name)
4443
else tree
4544
case _ => tree
4645
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ object Checking {
133133
// Create a synthetic singleton type instance, and check whether
134134
// it conforms to the self type of the class as seen from that instance.
135135
val stp = SkolemType(tp)
136-
val selfType = tref.givenSelfType.asSeenFrom(stp, cls)
136+
val selfType = cls.asClass.givenSelfType.asSeenFrom(stp, cls)
137137
if (selfType.exists && !(stp <:< selfType))
138138
ctx.error(DoesNotConformToSelfTypeCantBeInstantiated(tp, selfType), pos)
139139
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ object RefChecks {
9494
*/
9595
private def checkParents(cls: Symbol)(implicit ctx: Context): Unit = cls.info match {
9696
case cinfo: ClassInfo =>
97-
def checkSelfConforms(other: TypeRef, category: String, relation: String) = {
97+
def checkSelfConforms(other: ClassSymbol, category: String, relation: String) = {
9898
val otherSelf = other.givenSelfType.asSeenFrom(cls.thisType, other.classSymbol)
9999
if (otherSelf.exists && !(cinfo.selfType <:< otherSelf))
100100
ctx.error(DoesNotConformToSelfType(category, cinfo.selfType, cls, otherSelf, relation, other.classSymbol),
101101
cls.pos)
102102
}
103103
for (parent <- cinfo.classParents)
104-
checkSelfConforms(parent, "illegal inheritance", "parent")
105-
for (reqd <- cinfo.givenSelfType.classSymbols)
106-
checkSelfConforms(reqd.typeRef, "missing requirement", "required")
104+
checkSelfConforms(parent.typeSymbol.asClass, "illegal inheritance", "parent")
105+
for (reqd <- cinfo.cls.givenSelfType.classSymbols)
106+
checkSelfConforms(reqd, "missing requirement", "required")
107107
case _ =>
108108
}
109109

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -513,17 +513,15 @@ trait TypeAssigner {
513513

514514
private def symbolicIfNeeded(sym: Symbol)(implicit ctx: Context) = {
515515
val owner = sym.owner
516-
owner.infoOrCompleter match {
517-
case info: ClassInfo if info.givenSelfType.exists =>
518-
// In that case a simple typeRef/termWithWithSig could return a member of
519-
// the self type, not the symbol itself. To avoid this, we make the reference
520-
// symbolic. In general it seems to be faster to keep the non-symblic
521-
// reference, since there is less pressure on the uniqueness tables that way
522-
// and less work to update all the different references. That's why symbolic references
523-
// are only used if necessary.
524-
NamedType.withFixedSym(owner.thisType, sym)
525-
case _ => NoType
526-
}
516+
if (owner.isClass && owner.isCompleted && owner.asClass.givenSelfType.exists)
517+
// In that case a simple typeRef/termWithWithSig could return a member of
518+
// the self type, not the symbol itself. To avoid this, we make the reference
519+
// symbolic. In general it seems to be faster to keep the non-symblic
520+
// reference, since there is less pressure on the uniqueness tables that way
521+
// and less work to update all the different references. That's why symbolic references
522+
// are only used if necessary.
523+
NamedType.withFixedSym(owner.thisType, sym)
524+
else NoType
527525
}
528526

529527
def assertExists(tp: Type) = { assert(tp != NoType); tp }

0 commit comments

Comments
 (0)