Skip to content

Fix #5083: Fix field references in outer accessors #5099

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
case pre: ThisType =>
tp.isType ||
pre.cls.isStaticOwner ||
tp.symbol.isParamOrAccessor && !pre.cls.is(Trait) && ctx.owner.enclosingClass == pre.cls
tp.symbol.isParamOrAccessor && !tp.symbol.owner.is(Trait) && ctx.owner.enclosingClass == pre.cls
// was ctx.owner.enclosingClass.derivesFrom(pre.cls) which was not tight enough
// and was spuriously triggered in case inner class would inherit from outer one
// eg anonymous TypeMap inside TypeMap.andThen
Expand Down
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,19 @@ class ExplicitOuter extends MiniPhase with InfoTransformer { thisPhase =>
}
}

/** Replace `NoPrefix` prefixes of references to symbols in superclasses by `cls.thisType` */
def fixPrefix(tp: Type): Type = tp match {
case tp @ TermRef(NoPrefix, _) if cls.derivesFrom(tp.symbol.owner) =>
tp.derivedSelect(cls.thisType)
case tp: NamedType =>
tp.derivedSelect(fixPrefix(tp.prefix))
case tp =>
tp
}

for (parentTrait <- cls.mixins) {
if (needsOuterIfReferenced(parentTrait)) {
val parentTp = cls.denot.thisType.baseType(parentTrait)
val parentTp = fixPrefix(cls.denot.thisType.baseType(parentTrait))
val outerAccImpl = newOuterAccessor(cls, parentTrait).enteredAfter(thisPhase)
newDefs += DefDef(outerAccImpl, singleton(fixThis(outerPrefix(parentTp))))
}
Expand Down
15 changes: 15 additions & 0 deletions tests/pos/i5083.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class A(a: Int) {
class X {
val x = a
}

trait Y {
val y = a
}

trait Z(val o: A) extends o.Y {
val z = a
}

class B extends X with Z(new A(4))
}