Skip to content

Commit 2a32d6a

Browse files
authored
Merge pull request #10909 from dotty-staging/fix-#10905
Fix #10905: Fix explicitSelf phase
2 parents 8c0c461 + 71e7924 commit 2a32d6a

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package dotty.tools.dotc
22
package transform
33

44
import core._
5-
import Contexts._
6-
import Types._
7-
import MegaPhase._
8-
import ast.Trees._
5+
import Contexts._, Types._, MegaPhase._, ast.Trees._, Symbols._, Decorators._, Flags._
96

107
/** Transform references of the form
118
*
@@ -26,18 +23,28 @@ class ExplicitSelf extends MiniPhase {
2623

2724
override def phaseName: String = "explicitSelf"
2825

26+
private def needsCast(tree: RefTree, cls: ClassSymbol)(using Context) =
27+
!cls.is(Package) && cls.givenSelfType.exists && !cls.derivesFrom(tree.symbol.owner)
28+
29+
private def castQualifier(tree: RefTree, cls: ClassSymbol, thiz: Tree)(using Context) =
30+
cpy.Select(tree)(thiz.cast(AndType(cls.classInfo.selfType, thiz.tpe)), tree.name)
31+
2932
override def transformIdent(tree: Ident)(using Context): Tree = tree.tpe match {
3033
case tp: ThisType =>
3134
report.debuglog(s"owner = ${ctx.owner}, context = ${ctx}")
3235
This(tp.cls).withSpan(tree.span)
33-
case _ => tree
36+
case TermRef(thisTp: ThisType, _) =>
37+
val cls = thisTp.cls
38+
if needsCast(tree, cls) then castQualifier(tree, cls, This(cls))
39+
else tree
40+
case _ =>
41+
tree
3442
}
3543

3644
override def transformSelect(tree: Select)(using Context): Tree = tree match {
3745
case Select(thiz: This, name) if name.isTermName =>
3846
val cls = thiz.symbol.asClass
39-
if (cls.givenSelfType.exists && !cls.derivesFrom(tree.symbol.owner))
40-
cpy.Select(tree)(thiz.cast(AndType(cls.classInfo.selfType, thiz.tpe)), name)
47+
if needsCast(tree, cls) then castQualifier(tree, cls, thiz)
4148
else tree
4249
case _ => tree
4350
}

tests/run/i10905.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class C(val x: Int) extends TypeHelpers
2+
abstract class TypeHelpers:
3+
self: C =>
4+
def f = x
5+
@main def Test =
6+
C(0).f

0 commit comments

Comments
 (0)