Skip to content

Commit 08c6eac

Browse files
committed
thisType of a module class is a term ref to the source module.
Module classes now always get the sourceModule term ref as their this type. We would like to eliminate ThisType() of a module class completely, as this hangs on to a symbol which might become stale for globally accessible modules. This commit is the first step. It contains the change to thisType and the necessary fixes to make the test suite pass.
1 parent 97d89af commit 08c6eac

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,15 @@ object Trees {
469469
case class This[-T >: Untyped] private[ast] (qual: TypeName)
470470
extends DenotingTree[T] with TermTree[T] {
471471
type ThisTree[-T >: Untyped] = This[T]
472+
// Denotation of a This tree is always the udnerlying class; needs correction for modules.
473+
override def denot(implicit ctx: Context): Denotation = {
474+
tpe match {
475+
case tpe @ TermRef(pre, _) if tpe.symbol is Module =>
476+
tpe.symbol.moduleClass.denot.asSeenFrom(pre)
477+
case _ =>
478+
super.denot
479+
}
480+
}
472481
}
473482

474483
/** C.super[mix], where qual = C.this */

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,14 +467,17 @@ object SymDenotations {
467467
(linked ne NoSymbol) && accessWithin(linked)
468468
}
469469

470-
/** Is `pre` of the form C.this, where C is exactly the owner of this symbol,
470+
/** Is `pre` the same as C.thisThis, where C is exactly the owner of this symbol,
471471
* or, if this symbol is protected, a subclass of the owner?
472472
*/
473473
def isCorrectThisType(pre: Type): Boolean = pre match {
474474
case ThisType(pclazz) =>
475475
(pclazz eq owner) ||
476476
(this is Protected) && pclazz.derivesFrom(owner)
477-
case _ => false
477+
case pre: TermRef =>
478+
pre.symbol.moduleClass == owner
479+
case _ =>
480+
false
478481
}
479482

480483
/** Is protected access to target symbol permitted? */
@@ -1002,11 +1005,13 @@ object SymDenotations {
10021005
myThisType
10031006
}
10041007

1005-
private def computeThisType(implicit ctx: Context): Type = ThisType(classSymbol) /*
1006-
if ((this is PackageClass) && !isRoot)
1007-
TermRef(owner.thisType, name.toTermName)
1008-
else
1009-
ThisType(classSymbol) */
1008+
private def computeThisType(implicit ctx: Context): Type =
1009+
if (this.is(Module, butNot = Package)) {
1010+
val pre = owner.thisType
1011+
if ((pre eq NoPrefix) || ctx.erasedTypes) pre select sourceModule
1012+
else TermRef.withSig(pre, name.sourceModuleName, Signature.NotAMethod)
1013+
}
1014+
else ThisType(classSymbol)
10101015

10111016
private[this] var myTypeRef: TypeRef = null
10121017

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,13 +1353,13 @@ object Types {
13531353
}
13541354

13551355
override def newLikeThis(prefix: Type)(implicit ctx: Context): TermRef = {
1356-
if (sig != Signature.NotAMethod &&
1357-
sig != Signature.OverloadedSignature &&
1358-
symbol.exists) {
1356+
val candidate = TermRef.withSig(prefix, name, sig)
1357+
if (symbol.exists && !candidate.symbol.exists) { // recompute from previous symbol
13591358
val ownSym = symbol
1360-
TermRef.all(prefix, name).withDenot(asMemberOf(prefix).disambiguate(_ eq ownSym))
1359+
val newd = asMemberOf(prefix)
1360+
candidate.withDenot(asMemberOf(prefix).suchThat(_ eq ownSym))
13611361
}
1362-
else TermRef.withSig(prefix, name, sig)
1362+
else candidate
13631363
}
13641364

13651365
override def equals(that: Any) = that match {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ trait TypeAssigner {
4949
case TypeAlias(ref) =>
5050
apply(ref)
5151
case info: ClassInfo =>
52-
mapOver(info.instantiatedParents.reduceLeft(AndType(_, _)))
52+
mapOver(info.instantiatedParents.reduceLeft(ctx.typeComparer.andType(_, _)))
5353
case _ =>
5454
mapOver(tp)
5555
}

0 commit comments

Comments
 (0)