Skip to content

Commit afa8309

Browse files
authored
Merge pull request #1821 from dotty-staging/fix-i1820
Fix #1820: make sure outer of traits implemented
2 parents e23c278 + c2bc637 commit afa8309

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,19 @@ class ExplicitOuter extends MiniPhaseTransform with InfoTransformer { thisTransf
8181
!needsOuterAlways(cls) &&
8282
impl.existsSubTree(referencesOuter(cls, _)))
8383
ensureOuterAccessors(cls)
84-
if (hasOuter(cls)) {
84+
85+
val clsHasOuter = hasOuter(cls)
86+
if (clsHasOuter || cls.mixins.exists(needsOuterIfReferenced)) {
8587
val newDefs = new mutable.ListBuffer[Tree]
86-
if (isTrait)
87-
newDefs += DefDef(outerAccessor(cls).asTerm, EmptyTree)
88-
else {
89-
val outerParamAcc = outerParamAccessor(cls)
90-
newDefs += ValDef(outerParamAcc, EmptyTree)
91-
newDefs += DefDef(outerAccessor(cls).asTerm, ref(outerParamAcc))
88+
89+
if (clsHasOuter) {
90+
if (isTrait)
91+
newDefs += DefDef(outerAccessor(cls).asTerm, EmptyTree)
92+
else {
93+
val outerParamAcc = outerParamAccessor(cls)
94+
newDefs += ValDef(outerParamAcc, EmptyTree)
95+
newDefs += DefDef(outerAccessor(cls).asTerm, ref(outerParamAcc))
96+
}
9297
}
9398

9499
for (parentTrait <- cls.mixins) {
@@ -181,6 +186,7 @@ object ExplicitOuter {
181186
private def needsOuterAlways(cls: ClassSymbol)(implicit ctx: Context): Boolean =
182187
needsOuterIfReferenced(cls) &&
183188
(!hasLocalInstantiation(cls) || // needs outer because we might not know whether outer is referenced or not
189+
cls.mixins.exists(needsOuterIfReferenced) || // needs outer for parent traits
184190
cls.classInfo.parents.exists(parent => // needs outer to potentially pass along to parent
185191
needsOuterIfReferenced(parent.classSymbol.asClass)))
186192

tests/run/i1820.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a

tests/run/i1820.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class A {
2+
val a = "a"
3+
trait Inner {
4+
def f = println(a)
5+
def h = 3
6+
}
7+
}
8+
9+
class Inner extends Test.a.Inner
10+
11+
object Test {
12+
val a = new A
13+
14+
def main(args: Array[String]): Unit = {
15+
(new Inner).f
16+
}
17+
}

tests/run/i1820b.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a

tests/run/i1820b.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
trait A {
2+
val a = "a"
3+
trait Inner {
4+
def f = println(a)
5+
def h = 3
6+
}
7+
}
8+
9+
trait B extends A {
10+
trait Inner2 extends Inner
11+
def g = new Inner2 {}
12+
}
13+
14+
object Test {
15+
def main(args: Array[String]): Unit = {
16+
val b = new B {}
17+
b.g.f
18+
}
19+
}

0 commit comments

Comments
 (0)