From 90200957ca0beb4db24555a2563d0a902de0078d Mon Sep 17 00:00:00 2001 From: Iulian Dragos Date: Mon, 28 Jul 2008 18:05:50 +0000 Subject: Added -Ycheckinit, which causes all getters to ... Added -Ycheckinit, which causes all getters to check that fields are initialized before being used. It reuses the same machinery (and bitmaps) as lazy values. For now it requires the new initialization order (-Xexperimental) to work. --- test/files/run/checked.check | 35 +++++++++++++ test/files/run/checked.flags | 1 + test/files/run/checked.scala | 115 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 test/files/run/checked.check create mode 100644 test/files/run/checked.flags create mode 100644 test/files/run/checked.scala (limited to 'test') diff --git a/test/files/run/checked.check b/test/files/run/checked.check new file mode 100644 index 0000000000..a1a43027f0 --- /dev/null +++ b/test/files/run/checked.check @@ -0,0 +1,35 @@ +checked.scala:42: warning: the semantics of this definition has changed; +the initialization is no longer be executed before the superclass is called + val x = 1 + ^ +checked.scala:19: warning: the semantics of this definition has changed; +the initialization is no longer be executed before the superclass is called + val t1 = 1 + ^ +checked.scala:20: warning: the semantics of this definition has changed; +the initialization is no longer be executed before the superclass is called + var t2 = 2 + ^ +checked.scala:53: warning: the semantics of this definition has changed; +the initialization is no longer be executed before the superclass is called + val x = 1 + ^ +checked.scala:54: warning: the semantics of this definition has changed; +the initialization is no longer be executed before the superclass is called + val y = 2 + ^ +5 warnings found +sum = 12 +[OK] Cought UFE: Uninitialized field: checked.scala: 42 +2 +[OK] Cought UFE: Uninitialized field: checked.scala: 73 +x = 10 +y = 11 +lz1 = 1 +lz2 = 2 +[OK]: 24 +x = 10 +y = 11 +lz1 = 1 +lz2 = 2 +[OK]: 24 diff --git a/test/files/run/checked.flags b/test/files/run/checked.flags new file mode 100644 index 0000000000..2182668259 --- /dev/null +++ b/test/files/run/checked.flags @@ -0,0 +1 @@ +-Xexperimental -Ycheckinit \ No newline at end of file diff --git a/test/files/run/checked.scala b/test/files/run/checked.scala new file mode 100644 index 0000000000..a20b819353 --- /dev/null +++ b/test/files/run/checked.scala @@ -0,0 +1,115 @@ +/* Test checked initializers. Needs to be run with -Xexperimental and -checkinit + */ + +// 0 inherited fields +class A { + val x = 1 + val y = 2 + var z = 3 +} + +// 3 inherited fields +class B extends A { + val b1 = 1 + var b2 = 2 +} + + +trait T { + val t1 = 1 + var t2 = 2 +} + +// Should not throw +class D extends B with T { + val sum = x + y + z + b1 + b2 + t1 + t2 + override def toString = + "sum = " + sum + +} + +abstract class NeedsXEarly { + val x: Int + val y = x + 1 +} + +// should pass +class GoodX extends { val x = 1 } with NeedsXEarly { +} + +// should throw +class BadX extends NeedsXEarly { + val x = 1 + println(y) +} + +// should pass +class UglyX extends NeedsXEarly { + lazy val x = 1 + println(y) +} + +trait XY { + val x = 1 + val y = 2 +} + +// needs x and y early +trait LazyFields { + lazy val lz1 = 1 + lazy val lz2 = 2 + val x: Int + val y: Int + val needsSomeEarly = { + println("x = " + x) + println("y = " + y) + println("lz1 = " + lz1) + println("lz2 = " + lz2) + x + y + lz1 + lz2 + } +} + +// will fail at init +class BadMixin extends LazyFields with XY { + println("[OK]: " + needsSomeEarly) +} + +// should print 24 +class GoodMixin extends { + override val x = 10 + override val y = 11 + } with LazyFields with XY { + println("[OK]: " + needsSomeEarly) +} + +class TestInterference extends { + override val x = 10 + override val y = 11 +} with A with T with LazyFields { + println("[OK]: " + needsSomeEarly) +} + + +object Test extends Application { + + def shouldThrow(t: => Unit) = try { + t + println("[FAIL]: No UFE thrown") + } catch { + case UninitializedFieldError(msg) => + println("[OK] Cought UFE: " + msg) + } + + + val d = new D() + println(d) + + shouldThrow(new BadX) + (new GoodX) + (new UglyX) + + shouldThrow(new BadMixin) + (new GoodMixin) + + (new TestInterference) +} -- cgit v1.2.3