Skip to content

Commit a33b958

Browse files
committed
Move givenSelfType to ClassSymbol
Simplifies usage and removes some obscure code in Types
1 parent 5e6b666 commit a33b958

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
@@ -1213,14 +1213,6 @@ object Types {
12131213
case _ => defn.AnyType
12141214
}
12151215

1216-
/** the self type of the underlying classtype */
1217-
def givenSelfType(implicit ctx: Context): Type = this match {
1218-
case tp: RefinedType => tp.wrapIfMember(tp.parent.givenSelfType)
1219-
case tp: ThisType => tp.tref.givenSelfType
1220-
case tp: TypeProxy => tp.superType.givenSelfType
1221-
case _ => NoType
1222-
}
1223-
12241216
/** The parameter types of a PolyType or MethodType, Empty list for others */
12251217
final def paramInfoss(implicit ctx: Context): List[List[Type]] = stripPoly match {
12261218
case mt: MethodType => mt.paramInfos :: mt.resultType.paramInfoss
@@ -1649,10 +1641,8 @@ object Types {
16491641
}
16501642

16511643
private def checkSymAssign(sym: Symbol)(implicit ctx: Context) = {
1652-
def selfTypeOf(sym: Symbol) = sym.owner.info match {
1653-
case info: ClassInfo => info.givenSelfType
1654-
case _ => NoType
1655-
}
1644+
def selfTypeOf(sym: Symbol) =
1645+
if (sym.isClass) sym.asClass.givenSelfType else NoType
16561646
assert(
16571647
(lastSymbol eq sym)
16581648
||
@@ -2251,7 +2241,7 @@ object Types {
22512241
if ((parent eq this.parent) && (refinedName eq this.refinedName) && (refinedInfo eq this.refinedInfo)) this
22522242
else RefinedType(parent, refinedName, refinedInfo)
22532243

2254-
/** Add this refinement to `parent`, provided If `refinedName` is a member of `parent`. */
2244+
/** Add this refinement to `parent`, provided `refinedName` is a member of `parent`. */
22552245
def wrapIfMember(parent: Type)(implicit ctx: Context): Type =
22562246
if (parent.member(refinedName).exists) derivedRefinedType(parent, refinedName, refinedInfo)
22572247
else parent
@@ -3425,7 +3415,7 @@ object Types {
34253415
if (selfTypeCache == null)
34263416
selfTypeCache = {
34273417
def fullRef = fullyAppliedRef
3428-
val given = givenSelfType
3418+
val given = cls.givenSelfType
34293419
val raw =
34303420
if (!given.exists) fullRef
34313421
else if (cls is Module) given
@@ -3436,14 +3426,6 @@ object Types {
34363426
selfTypeCache
34373427
}
34383428

3439-
/** The explicitly given self type (self types of modules are assumed to be
3440-
* explcitly given here).
3441-
*/
3442-
override def givenSelfType(implicit ctx: Context): Type = selfInfo match {
3443-
case tp: Type => tp
3444-
case self: Symbol => self.info
3445-
}
3446-
34473429
private var selfTypeCache: Type = null
34483430

34493431
private def fullyAppliedRef(base: Type, tparams: List[TypeSymbol])(implicit ctx: Context): Type = tparams match {
@@ -4282,7 +4264,7 @@ object Types {
42824264
}
42834265

42844266
private def otherReason(pre: Type)(implicit ctx: Context): String = pre match {
4285-
case pre: ThisType if pre.givenSelfType.exists =>
4267+
case pre: ThisType if pre.cls.givenSelfType.exists =>
42864268
i"\nor the self type of $pre might not contain all transitive dependencies"
42874269
case _ => ""
42884270
}

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)