@@ -2,10 +2,7 @@ package dotty.tools.dotc
2
2
package transform
3
3
4
4
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 ._
9
6
10
7
/** Transform references of the form
11
8
*
@@ -26,18 +23,28 @@ class ExplicitSelf extends MiniPhase {
26
23
27
24
override def phaseName : String = " explicitSelf"
28
25
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
+
29
32
override def transformIdent (tree : Ident )(using Context ): Tree = tree.tpe match {
30
33
case tp : ThisType =>
31
34
report.debuglog(s " owner = ${ctx.owner}, context = ${ctx}" )
32
35
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
34
42
}
35
43
36
44
override def transformSelect (tree : Select )(using Context ): Tree = tree match {
37
45
case Select (thiz : This , name) if name.isTermName =>
38
46
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)
41
48
else tree
42
49
case _ => tree
43
50
}
0 commit comments