From ac71812170547acdce74fc224bfa9f3a776b4cd1 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Tue, 26 Jun 2012 23:21:38 +0200 Subject: SI-2796 Warn if early definitions are used with a trait. For which they (currently) have no special meaning. --- .../scala/tools/nsc/typechecker/Typers.scala | 6 +++++ test/files/neg/t2796.check | 4 ++++ test/files/neg/t2796.flags | 1 + test/files/neg/t2796.scala | 28 ++++++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 test/files/neg/t2796.check create mode 100644 test/files/neg/t2796.flags create mode 100644 test/files/neg/t2796.scala diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index acf1b3dc59..4d6e1c71a1 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -1753,6 +1753,12 @@ trait Typers extends Modes with Adaptations with Tags { if (clazz.info.firstParent.typeSymbol == AnyValClass) validateDerivedValueClass(clazz, body1) + if (clazz.isTrait) { + for (decl <- clazz.info.decls if decl.isTerm && decl.isEarlyInitialized) { + unit.warning(decl.pos, "Implementation restriction: early definitions in traits are not initialized before the super class is initialized.") + } + } + treeCopy.Template(templ, parents1, self1, body1) setType clazz.tpe } diff --git a/test/files/neg/t2796.check b/test/files/neg/t2796.check new file mode 100644 index 0000000000..aeb18497ed --- /dev/null +++ b/test/files/neg/t2796.check @@ -0,0 +1,4 @@ +t2796.scala:7: error: Implementation restriction: early definitions in traits are not initialized before the super class is initialized. + val abstractVal = "T1.abstractVal" // warn + ^ +one error found diff --git a/test/files/neg/t2796.flags b/test/files/neg/t2796.flags new file mode 100644 index 0000000000..e8fb65d50c --- /dev/null +++ b/test/files/neg/t2796.flags @@ -0,0 +1 @@ +-Xfatal-warnings \ No newline at end of file diff --git a/test/files/neg/t2796.scala b/test/files/neg/t2796.scala new file mode 100644 index 0000000000..3bcc9df562 --- /dev/null +++ b/test/files/neg/t2796.scala @@ -0,0 +1,28 @@ +trait Base { + val abstractVal: String + final val useAbstractVal = abstractVal +} + +trait T1 extends { + val abstractVal = "T1.abstractVal" // warn +} with Base + +trait T2 extends { + type X = Int // okay +} with Base + + +class C1 extends { + val abstractVal = "C1.abstractVal" // okay +} with Base + +object Test { + def main(args: Array[String]) { + assert(new C1 ().useAbstractVal == "C1.abstractVal") + // This currently fails. a more ambitious approach to this ticket would add $earlyinit$ + // to traits and call it from the right places in the right order. + // + // For now, we'll just issue a warning. + assert(new T1 {}.useAbstractVal == "T1.abstractVal") + } +} -- cgit v1.2.3