Skip to content

Fix #10905: Fix explicitSelf phase #10909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions compiler/src/dotty/tools/dotc/transform/ExplicitSelf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package dotty.tools.dotc
package transform

import core._
import Contexts._
import Types._
import MegaPhase._
import ast.Trees._
import Contexts._, Types._, MegaPhase._, ast.Trees._, Symbols._, Decorators._, Flags._

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

override def phaseName: String = "explicitSelf"

private def needsCast(tree: RefTree, cls: ClassSymbol)(using Context) =
!cls.is(Package) && cls.givenSelfType.exists && !cls.derivesFrom(tree.symbol.owner)

private def castQualifier(tree: RefTree, cls: ClassSymbol, thiz: Tree)(using Context) =
cpy.Select(tree)(thiz.cast(AndType(cls.classInfo.selfType, thiz.tpe)), tree.name)

override def transformIdent(tree: Ident)(using Context): Tree = tree.tpe match {
case tp: ThisType =>
report.debuglog(s"owner = ${ctx.owner}, context = ${ctx}")
This(tp.cls).withSpan(tree.span)
case _ => tree
case TermRef(thisTp: ThisType, _) =>
val cls = thisTp.cls
if needsCast(tree, cls) then castQualifier(tree, cls, This(cls))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if needsCast(tree, cls) then castQualifier(tree, cls, This(cls))
if needsCast(tree, cls) then castQualifier(tree, cls, This(cls).withSpan(tree.span))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will get the span anyway through the cpy.Select(tree) call.

else tree
case _ =>
tree
}

override def transformSelect(tree: Select)(using Context): Tree = tree match {
case Select(thiz: This, name) if name.isTermName =>
val cls = thiz.symbol.asClass
if (cls.givenSelfType.exists && !cls.derivesFrom(tree.symbol.owner))
cpy.Select(tree)(thiz.cast(AndType(cls.classInfo.selfType, thiz.tpe)), name)
if needsCast(tree, cls) then castQualifier(tree, cls, thiz)
else tree
case _ => tree
}
Expand Down
6 changes: 6 additions & 0 deletions tests/run/i10905.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class C(val x: Int) extends TypeHelpers
abstract class TypeHelpers:
self: C =>
def f = x
@main def Test =
C(0).f