From 9a4b5e7c306eb3f1c82ace10dd62576473b1dec1 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sat, 20 Jun 2015 00:44:47 +0200 Subject: Map outer fields to parameters in primary constructor Previously this was only done in secondary constructors; need to do it in primary constructor as well to avoid "reference to this before super" problems. --- src/dotty/tools/dotc/transform/Constructors.scala | 8 +- tests/pending/run/NestedClasses.check | 9 --- tests/pending/run/NestedClasses.scala | 97 ----------------------- tests/pending/run/t8611b.flags | 1 - tests/pending/run/t8611b.scala | 54 ------------- tests/run/NestedClasses.check | 9 +++ tests/run/NestedClasses.scala | 97 +++++++++++++++++++++++ tests/run/t8611b.flags | 1 + tests/run/t8611b.scala | 54 +++++++++++++ 9 files changed, 168 insertions(+), 162 deletions(-) delete mode 100644 tests/pending/run/NestedClasses.check delete mode 100644 tests/pending/run/NestedClasses.scala delete mode 100644 tests/pending/run/t8611b.flags delete mode 100644 tests/pending/run/t8611b.scala create mode 100644 tests/run/NestedClasses.check create mode 100644 tests/run/NestedClasses.scala create mode 100644 tests/run/t8611b.flags create mode 100644 tests/run/t8611b.scala diff --git a/src/dotty/tools/dotc/transform/Constructors.scala b/src/dotty/tools/dotc/transform/Constructors.scala index d078bab1d..3d7af6bf5 100644 --- a/src/dotty/tools/dotc/transform/Constructors.scala +++ b/src/dotty/tools/dotc/transform/Constructors.scala @@ -233,9 +233,15 @@ class Constructors extends MiniPhaseTransform with SymTransformer { thisTransfor case stats => (Nil, stats) } + val mappedSuperCalls = vparams match { + case (outerParam @ ValDef(nme.OUTER, _, _)) :: _ => + superCalls.map(mapOuter(outerParam.symbol).transform) + case _ => superCalls + } + cpy.Template(tree)( constr = cpy.DefDef(constr)( - rhs = Block(superCalls ::: copyParams ::: followConstrStats, unitLiteral)), + rhs = Block(mappedSuperCalls ::: copyParams ::: followConstrStats, unitLiteral)), body = clsStats.toList) } } diff --git a/tests/pending/run/NestedClasses.check b/tests/pending/run/NestedClasses.check deleted file mode 100644 index a7ebc386e..000000000 --- a/tests/pending/run/NestedClasses.check +++ /dev/null @@ -1,9 +0,0 @@ -e.e1 = 119 -cc.m = 3 -cc.am = 1 -cc.bm = 2 -aaa.f = 111 -bbb1.f = 42 -bbb2.f = 24 -bbb3.f = 24 -bbb4.f = 24 diff --git a/tests/pending/run/NestedClasses.scala b/tests/pending/run/NestedClasses.scala deleted file mode 100644 index 6db713e08..000000000 --- a/tests/pending/run/NestedClasses.scala +++ /dev/null @@ -1,97 +0,0 @@ -//############################################################################ -// Test nested classes -//############################################################################ - -// The following set of classes tests nasty references to "outer" -// values. - -class A(pa : Int) { - def a1 = pa; - class B(pb : Int) { - def b1 = pa+pb+a1; - class C(pc : Int) extends A(b1) { - def c1 = pc+pb+pa - } - val c1 = new C(13) - } -} - -trait M { - def m1 = 1 -} - -class A1(x : Int) extends A(x) with M { - class D extends B(14) { - val c2 = new C(15); - class E extends C(16) { - def e1 = c1+b1+a1+m1; - def e2 = new D(); - } - } -} - -// The following set of classes test qualified "this" and "super" -// references. - -class AA { - def m = 1; - class BB { - def m = 2; - class CC { - def m = 3; - def am = AA.this.m; - def bm = BB.this.m; - } - } -} - -class AAA { - def f = 42; -} - -class BBB extends AAA { - override def f = 24; -} - -class AAA1 extends AAA { - override def f = 111; - class BBB1 extends BBB { - override def f = AAA1.super.f; - } - class BBB2 extends BBB { - override def f = BBB2.super.f; - } - class BBB3 extends BBB { - override def f = super.f; - } - class BBB4 extends BBB { } -} - -object Test { - def main(args: Array[String]): Unit = { - val a = new A1(12); - val d = new a.D; - val e = new d.E; - Console.println("e.e1 = " + e.e1); - - val aa = new AA; - val bb = new aa.BB; - val cc = new bb.CC; - Console.println("cc.m = " + cc.m); - Console.println("cc.am = " + cc.am); - Console.println("cc.bm = " + cc.bm); - - val aaa = new AAA1; - val bbb1 = new aaa.BBB1; - val bbb2 = new aaa.BBB2; - val bbb3 = new aaa.BBB3; - val bbb4 = new aaa.BBB4; - Console.println("aaa.f = " + aaa.f); - Console.println("bbb1.f = " + bbb1.f); - Console.println("bbb2.f = " + bbb2.f); - Console.println("bbb3.f = " + bbb3.f); - Console.println("bbb4.f = " + bbb4.f); - } -} - -//############################################################################ diff --git a/tests/pending/run/t8611b.flags b/tests/pending/run/t8611b.flags deleted file mode 100644 index 85d8eb2ba..000000000 --- a/tests/pending/run/t8611b.flags +++ /dev/null @@ -1 +0,0 @@ --Xfatal-warnings diff --git a/tests/pending/run/t8611b.scala b/tests/pending/run/t8611b.scala deleted file mode 100644 index 75114c2ae..000000000 --- a/tests/pending/run/t8611b.scala +++ /dev/null @@ -1,54 +0,0 @@ -sealed trait KrafsDescription - -abstract class NotWorkingEnum extends Enumeration { - - type ExtendedValue = Value with KrafsDescription - - def Enum(inDescription: String): ExtendedValue = { - new Val(nextId) with KrafsDescription { - } - } -} - -abstract class WorkingEnum extends Enumeration { - - type ExtendedValue = Value - - def Enum(inDescription: String): ExtendedValue = { - new Val(nextId) { - } - } -} - -object NotWorkingTab extends NotWorkingEnum { - val a = Enum("A") - val b = Enum("B") -} - -object WorkingTab extends WorkingEnum { - val a = Enum("A") - val b = Enum("B") -} - -object Test extends dotty.runtime.LegacyApp { - testGris() - testWorking() - - def testGris(): Unit = { - val pipp = NotWorkingTab.b - pipp match { - case NotWorkingTab.a => ??? - case NotWorkingTab.b => - case _ => ??? - } - } - - def testWorking(): Unit = { - val stuff = WorkingTab.a - stuff match { - case WorkingTab.a => - case WorkingTab.b => ??? - case _ => ??? - } - } -} diff --git a/tests/run/NestedClasses.check b/tests/run/NestedClasses.check new file mode 100644 index 000000000..a7ebc386e --- /dev/null +++ b/tests/run/NestedClasses.check @@ -0,0 +1,9 @@ +e.e1 = 119 +cc.m = 3 +cc.am = 1 +cc.bm = 2 +aaa.f = 111 +bbb1.f = 42 +bbb2.f = 24 +bbb3.f = 24 +bbb4.f = 24 diff --git a/tests/run/NestedClasses.scala b/tests/run/NestedClasses.scala new file mode 100644 index 000000000..6db713e08 --- /dev/null +++ b/tests/run/NestedClasses.scala @@ -0,0 +1,97 @@ +//############################################################################ +// Test nested classes +//############################################################################ + +// The following set of classes tests nasty references to "outer" +// values. + +class A(pa : Int) { + def a1 = pa; + class B(pb : Int) { + def b1 = pa+pb+a1; + class C(pc : Int) extends A(b1) { + def c1 = pc+pb+pa + } + val c1 = new C(13) + } +} + +trait M { + def m1 = 1 +} + +class A1(x : Int) extends A(x) with M { + class D extends B(14) { + val c2 = new C(15); + class E extends C(16) { + def e1 = c1+b1+a1+m1; + def e2 = new D(); + } + } +} + +// The following set of classes test qualified "this" and "super" +// references. + +class AA { + def m = 1; + class BB { + def m = 2; + class CC { + def m = 3; + def am = AA.this.m; + def bm = BB.this.m; + } + } +} + +class AAA { + def f = 42; +} + +class BBB extends AAA { + override def f = 24; +} + +class AAA1 extends AAA { + override def f = 111; + class BBB1 extends BBB { + override def f = AAA1.super.f; + } + class BBB2 extends BBB { + override def f = BBB2.super.f; + } + class BBB3 extends BBB { + override def f = super.f; + } + class BBB4 extends BBB { } +} + +object Test { + def main(args: Array[String]): Unit = { + val a = new A1(12); + val d = new a.D; + val e = new d.E; + Console.println("e.e1 = " + e.e1); + + val aa = new AA; + val bb = new aa.BB; + val cc = new bb.CC; + Console.println("cc.m = " + cc.m); + Console.println("cc.am = " + cc.am); + Console.println("cc.bm = " + cc.bm); + + val aaa = new AAA1; + val bbb1 = new aaa.BBB1; + val bbb2 = new aaa.BBB2; + val bbb3 = new aaa.BBB3; + val bbb4 = new aaa.BBB4; + Console.println("aaa.f = " + aaa.f); + Console.println("bbb1.f = " + bbb1.f); + Console.println("bbb2.f = " + bbb2.f); + Console.println("bbb3.f = " + bbb3.f); + Console.println("bbb4.f = " + bbb4.f); + } +} + +//############################################################################ diff --git a/tests/run/t8611b.flags b/tests/run/t8611b.flags new file mode 100644 index 000000000..85d8eb2ba --- /dev/null +++ b/tests/run/t8611b.flags @@ -0,0 +1 @@ +-Xfatal-warnings diff --git a/tests/run/t8611b.scala b/tests/run/t8611b.scala new file mode 100644 index 000000000..75114c2ae --- /dev/null +++ b/tests/run/t8611b.scala @@ -0,0 +1,54 @@ +sealed trait KrafsDescription + +abstract class NotWorkingEnum extends Enumeration { + + type ExtendedValue = Value with KrafsDescription + + def Enum(inDescription: String): ExtendedValue = { + new Val(nextId) with KrafsDescription { + } + } +} + +abstract class WorkingEnum extends Enumeration { + + type ExtendedValue = Value + + def Enum(inDescription: String): ExtendedValue = { + new Val(nextId) { + } + } +} + +object NotWorkingTab extends NotWorkingEnum { + val a = Enum("A") + val b = Enum("B") +} + +object WorkingTab extends WorkingEnum { + val a = Enum("A") + val b = Enum("B") +} + +object Test extends dotty.runtime.LegacyApp { + testGris() + testWorking() + + def testGris(): Unit = { + val pipp = NotWorkingTab.b + pipp match { + case NotWorkingTab.a => ??? + case NotWorkingTab.b => + case _ => ??? + } + } + + def testWorking(): Unit = { + val stuff = WorkingTab.a + stuff match { + case WorkingTab.a => + case WorkingTab.b => ??? + case _ => ??? + } + } +} -- cgit v1.2.3