diff options
-rw-r--r-- | test/dotc/tests.scala | 1 | ||||
-rw-r--r-- | tests/neg/singletons.scala | 6 | ||||
-rw-r--r-- | tests/pos/singletons.scala | 40 |
3 files changed, 47 insertions, 0 deletions
diff --git a/test/dotc/tests.scala b/test/dotc/tests.scala index b39d0e928..a3fb8185b 100644 --- a/test/dotc/tests.scala +++ b/test/dotc/tests.scala @@ -146,6 +146,7 @@ class tests extends CompilerTest { @Test def neg_instantiateAbstract = compileFile(negDir, "instantiateAbstract", xerrors = 8) @Test def neg_selfInheritance = compileFile(negDir, "selfInheritance", xerrors = 6) @Test def neg_selfreq = compileFile(negDir, "selfreq", xerrors = 4) + @Test def neg_singletons = compileFile(negDir, "singletons", xerrors = 2) @Test def neg_shadowedImplicits = compileFile(negDir, "arrayclone-new", xerrors = 2) @Test def neg_traitParamsTyper = compileFile(negDir, "traitParamsTyper", xerrors = 5) @Test def neg_traitParamsMixin = compileFile(negDir, "traitParamsMixin", xerrors = 2) diff --git a/tests/neg/singletons.scala b/tests/neg/singletons.scala new file mode 100644 index 000000000..77e2a924f --- /dev/null +++ b/tests/neg/singletons.scala @@ -0,0 +1,6 @@ +object Test { + final val y: 2 = { println("x"); 2 } // error: side effect + val a: 42 = 43 // error: different constant + val x = 42 + val z: 42 = x // error: x is not final +} diff --git a/tests/pos/singletons.scala b/tests/pos/singletons.scala new file mode 100644 index 000000000..55f9b5ecb --- /dev/null +++ b/tests/pos/singletons.scala @@ -0,0 +1,40 @@ + +object Test { + + val x: 1 = 1 + final val y = x + val z: 1 = y + + object O { final val x = 42 } + val fourtyTwo: 42 = O.x + + final val a = { println("x"); 2 } // side effects don't matter + val b: 2 = a + + def f: 3 = 3 + val c = f + +} +/* To do: test that after erasure we have generated code like this: + * +package <empty> { + final lazy module val Test: Test$ = new Test$() + final module class Test$() extends Object() { this: <notype> => + <accessor> def x(): Int = 1 + final <accessor> def y(): Int = 1 + <accessor> def z(): Int = 1 + final lazy module val O: Test.O$ = new Test.O$() + final module class O$() extends Object() { this: <notype> => + final <accessor> def x(): Int = 42 + } + <accessor> def fourtyTwo(): Int = 42 + final <accessor> def a(): Int = { + println("x") + 2 + } + <accessor> def b(): Int = 2 + def f(): Int = 3 + <accessor> def c(): Int = Test.f() + } +} +*/ |