aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Martres <smarter@ubuntu.com>2015-05-16 22:43:53 +0200
committerGuillaume Martres <smarter@ubuntu.com>2015-05-21 16:29:28 +0200
commitc48badad54f90047f690d02e8af80153c6634412 (patch)
tree6a0de36f10b33f618203775254460d380982e486
parentbf81fb62084f9e04e43906396c3ac5e307caca63 (diff)
downloaddotty-c48badad54f90047f690d02e8af80153c6634412.tar.gz
dotty-c48badad54f90047f690d02e8af80153c6634412.tar.bz2
dotty-c48badad54f90047f690d02e8af80153c6634412.zip
Mixin: fix the initialization of traits
Before this commit, the following code: trait Hello { println("Hello") val x: Int = 1 println("World") } Became: <trait> trait Hello extends Object { def <init>(): Hello = { { () } this } <accessor> def x(): Int protected def initial$x(): Int = { println("Hello") 1 } } Notice that the initialization statements after the last getter were missing, this is now fixed: <trait> trait Hello extends Object { def <init>(): Hello = { { println("World") () } this } <accessor> def x(): Int protected def initial$x(): Int = { println("Hello") 1 } }
-rw-r--r--src/dotty/tools/dotc/transform/Mixin.scala4
-rw-r--r--tests/run/traitInit.check2
-rw-r--r--tests/run/traitInit.scala13
3 files changed, 17 insertions, 2 deletions
diff --git a/src/dotty/tools/dotc/transform/Mixin.scala b/src/dotty/tools/dotc/transform/Mixin.scala
index e6e2ad259..8470be2ef 100644
--- a/src/dotty/tools/dotc/transform/Mixin.scala
+++ b/src/dotty/tools/dotc/transform/Mixin.scala
@@ -96,7 +96,7 @@ class Mixin extends MiniPhaseTransform with SymTransformer { thisTransform =>
def traitDefs(stats: List[Tree]): List[Tree] = {
val initBuf = new mutable.ListBuffer[Tree]
- stats flatMap {
+ stats.flatMap({
case stat: DefDef if stat.symbol.isGetter && !stat.rhs.isEmpty && !stat.symbol.is(Flags.Lazy) =>
// make initializer that has all effects of previous getter,
// replace getter rhs with empty tree.
@@ -114,7 +114,7 @@ class Mixin extends MiniPhaseTransform with SymTransformer { thisTransform =>
case stat =>
initBuf += stat
Nil
- }
+ }) ++ initBuf
}
def transformSuper(tree: Tree): Tree = {
diff --git a/tests/run/traitInit.check b/tests/run/traitInit.check
new file mode 100644
index 000000000..f9264f7fb
--- /dev/null
+++ b/tests/run/traitInit.check
@@ -0,0 +1,2 @@
+Hello
+World
diff --git a/tests/run/traitInit.scala b/tests/run/traitInit.scala
new file mode 100644
index 000000000..664fe4ce0
--- /dev/null
+++ b/tests/run/traitInit.scala
@@ -0,0 +1,13 @@
+trait Hello {
+ println("Hello")
+ val x: Int = 1
+ println("World")
+}
+
+class A extends Hello
+
+object Test {
+ def main(args: Array[String]): Unit = {
+ new A
+ }
+}