Skip to content

Commit 3d240ad

Browse files
committed
Fix ElimStaticThis#transformIdent
- Only transform static methods which are inside module classes. - Make sure that the prefix of the underlying type of the Ident is a ThisType of the current module class. For example in "scala.Int.box(42)", "box" is an Ident whose underlying type is "TermRef(ThisType(TypeRef(ThisType(TypeRef(NoPrefix,scala)),Int$)),box)", but we should not trigger an assertion in this case.
1 parent cafd71a commit 3d240ad

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ class Compiler {
6767
new Constructors,
6868
new FunctionalInterfaces),
6969
List(new LambdaLift, // in this mini-phase block scopes are incorrect. No phases that rely on scopes should be here
70-
new ElimStaticThis,
7170
new Flatten,
71+
new ElimStaticThis,
7272
new RestoreScopes),
7373
List(/*new PrivateToStatic,*/
7474
new ExpandPrivate,

src/dotty/tools/dotc/transform/ElimStaticThis.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import dotty.tools.dotc.core.StdNames._
99
import dotty.tools.dotc.core.SymDenotations.SymDenotation
1010
import TreeTransforms.{MiniPhaseTransform, TransformerInfo}
1111
import dotty.tools.dotc.core.Types.{ThisType, TermRef}
12+
import Phases.Phase
1213

1314
/** Replace This references to module classes in static methods by global identifiers to the
1415
* corresponding modules.
@@ -17,6 +18,8 @@ class ElimStaticThis extends MiniPhaseTransform {
1718
import ast.tpd._
1819
def phaseName: String = "elimStaticThis"
1920

21+
override def runsAfter: Set[Class[_ <: Phase]] = Set(classOf[Flatten])
22+
2023
override def transformThis(tree: This)(implicit ctx: Context, info: TransformerInfo): Tree =
2124
if (!tree.symbol.is(Package) && ctx.owner.enclosingMethod.is(JavaStatic)) {
2225
assert(tree.symbol.is(ModuleClass))
@@ -25,10 +28,12 @@ class ElimStaticThis extends MiniPhaseTransform {
2528
else tree
2629

2730
override def transformIdent(tree: tpd.Ident)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
28-
if (ctx.owner.enclosingMethod.is(JavaStatic)) {
31+
val meth = ctx.owner.enclosingMethod
32+
// We cannot use meth.enclosingClass because it skips other static classes,
33+
// so instead we require this phase to run after Flatten and use meth.owner
34+
if (meth.is(JavaStatic) && meth.owner.is(ModuleClass)) {
2935
tree.tpe match {
30-
case TermRef(thiz: ThisType, _) =>
31-
assert(thiz.underlying.typeSymbol.is(ModuleClass))
36+
case TermRef(thiz: ThisType, _) if (thiz.underlying.typeSymbol == meth.owner) =>
3237
ref(thiz.underlying.typeSymbol.sourceModule).select(tree.symbol)
3338
case _ => tree
3439
}

0 commit comments

Comments
 (0)