diff --git a/compiler/src/dotty/tools/dotc/ast/tpd.scala b/compiler/src/dotty/tools/dotc/ast/tpd.scala index ec0884e6ee99..8bcdd9203cfe 100644 --- a/compiler/src/dotty/tools/dotc/ast/tpd.scala +++ b/compiler/src/dotty/tools/dotc/ast/tpd.scala @@ -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 diff --git a/compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala b/compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala index fead31d29dd8..878f75a9df56 100644 --- a/compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala +++ b/compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala @@ -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)))) } diff --git a/tests/pos/i5083.scala b/tests/pos/i5083.scala new file mode 100644 index 000000000000..6843fa104e2f --- /dev/null +++ b/tests/pos/i5083.scala @@ -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)) +} \ No newline at end of file