Skip to content

Fix #1820: make sure outer of traits implemented #1821

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 4 commits into from
Dec 20, 2016
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
20 changes: 13 additions & 7 deletions compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,19 @@ class ExplicitOuter extends MiniPhaseTransform with InfoTransformer { thisTransf
!needsOuterAlways(cls) &&
impl.existsSubTree(referencesOuter(cls, _)))
ensureOuterAccessors(cls)
if (hasOuter(cls)) {

val clsHasOuter = hasOuter(cls)
if (clsHasOuter || cls.mixins.exists(needsOuterIfReferenced)) {
val newDefs = new mutable.ListBuffer[Tree]
if (isTrait)
newDefs += DefDef(outerAccessor(cls).asTerm, EmptyTree)
else {
val outerParamAcc = outerParamAccessor(cls)
newDefs += ValDef(outerParamAcc, EmptyTree)
newDefs += DefDef(outerAccessor(cls).asTerm, ref(outerParamAcc))

if (clsHasOuter) {
if (isTrait)
newDefs += DefDef(outerAccessor(cls).asTerm, EmptyTree)
else {
val outerParamAcc = outerParamAccessor(cls)
newDefs += ValDef(outerParamAcc, EmptyTree)
newDefs += DefDef(outerAccessor(cls).asTerm, ref(outerParamAcc))
}
}

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

Expand Down
1 change: 1 addition & 0 deletions tests/run/i1820.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
17 changes: 17 additions & 0 deletions tests/run/i1820.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class A {
val a = "a"
trait Inner {
def f = println(a)
def h = 3
}
}

class Inner extends Test.a.Inner

object Test {
val a = new A

def main(args: Array[String]): Unit = {
(new Inner).f
}
}
1 change: 1 addition & 0 deletions tests/run/i1820b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
19 changes: 19 additions & 0 deletions tests/run/i1820b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
trait A {
val a = "a"
trait Inner {
def f = println(a)
def h = 3
}
}

trait B extends A {
trait Inner2 extends Inner
def g = new Inner2 {}
}

object Test {
def main(args: Array[String]): Unit = {
val b = new B {}
b.g.f
}
}