From c30b2db59a097e11a98204a4cf930abc678314a2 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 9 May 2017 13:09:52 +0200 Subject: [PATCH] Disallow phantom var --- .../tools/dotc/transform/FirstTransform.scala | 6 ++-- tests/neg/phantom-var.scala | 8 +++++ tests/neg/phantom-volitile.scala | 3 +- tests/run/phantom-var-2.check | 3 -- tests/run/phantom-var-2.scala | 35 ------------------- tests/run/phantom-var.check | 2 -- tests/run/phantom-var.scala | 30 ---------------- 7 files changed, 14 insertions(+), 73 deletions(-) create mode 100644 tests/neg/phantom-var.scala delete mode 100644 tests/run/phantom-var-2.check delete mode 100644 tests/run/phantom-var-2.scala delete mode 100644 tests/run/phantom-var.check delete mode 100644 tests/run/phantom-var.scala diff --git a/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala b/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala index 469e247a1033..db91ff26705f 100644 --- a/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala +++ b/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala @@ -162,8 +162,10 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota } override def transformValDef(vdef: tpd.ValDef)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { - if (vdef.symbol.hasAnnotation(defn.VolatileAnnot) && vdef.tpt.tpe.isPhantom) - ctx.error("Phantom fields can not be @volatile", vdef.pos) + if (vdef.tpt.tpe.isPhantom) { + if (vdef.symbol.is(Mutable)) ctx.error("var fields cannot have Phantom types", vdef.pos) + else if (vdef.symbol.hasAnnotation(defn.VolatileAnnot)) ctx.error("Phantom fields cannot be @volatile", vdef.pos) + } vdef } diff --git a/tests/neg/phantom-var.scala b/tests/neg/phantom-var.scala new file mode 100644 index 000000000000..5a7e2db3fdb6 --- /dev/null +++ b/tests/neg/phantom-var.scala @@ -0,0 +1,8 @@ + +class Foo { + var foo = Boo.boo // error: var fields cannot have Phantom types +} + +object Boo extends Phantom { + def boo = assume +} diff --git a/tests/neg/phantom-volitile.scala b/tests/neg/phantom-volitile.scala index 1fada8572a7a..e8874c035503 100644 --- a/tests/neg/phantom-volitile.scala +++ b/tests/neg/phantom-volitile.scala @@ -1,6 +1,7 @@ class Foo { - @volatile var foo = Boo.boo // error + @volatile var foo1 = Boo.boo // error: var fields cannot have Phantom types + @volatile val foo2 = Boo.boo // error: Phantom fields cannot be @volatile } object Boo extends Phantom { diff --git a/tests/run/phantom-var-2.check b/tests/run/phantom-var-2.check deleted file mode 100644 index c02f34162f78..000000000000 --- a/tests/run/phantom-var-2.check +++ /dev/null @@ -1,3 +0,0 @@ -foo -foo2 -foo3 diff --git a/tests/run/phantom-var-2.scala b/tests/run/phantom-var-2.scala deleted file mode 100644 index 1e766df5a935..000000000000 --- a/tests/run/phantom-var-2.scala +++ /dev/null @@ -1,35 +0,0 @@ - -object Test { - import Boo._ - - def main(args: Array[String]): Unit = { - val f = new Foo - f.foo - f.foo - f.foo = { - println("foo3") - any - } - f.foo - assert(!f.getClass.getDeclaredFields.exists(_.getName.startsWith("foo")), "field foo not erased") - } -} - -class Foo { - import Boo._ - - var foo = { - println("foo") - any - } - - foo = { - println("foo2") - any - } -} - -object Boo extends Phantom { - type BooAny = this.Any - def any: BooAny = assume -} diff --git a/tests/run/phantom-var.check b/tests/run/phantom-var.check deleted file mode 100644 index b210800439ff..000000000000 --- a/tests/run/phantom-var.check +++ /dev/null @@ -1,2 +0,0 @@ -foo -foo2 diff --git a/tests/run/phantom-var.scala b/tests/run/phantom-var.scala deleted file mode 100644 index dda20fe92bfe..000000000000 --- a/tests/run/phantom-var.scala +++ /dev/null @@ -1,30 +0,0 @@ - -/* Run this test with - * `run tests/run/xyz.scala -Xprint-diff-del -Xprint:arrayConstructors,phantomTermErasure,phantomTypeErasure,erasure` - * to see the the diffs after PhantomRefErasure, PhantomDeclErasure and Erasure. - */ - -object Test { - import Boo._ - - def main(args: Array[String]): Unit = { - foo - foo - foo = { - println("foo2") - any - } - foo - - } - - var foo = { - println("foo") - any - } -} - -object Boo extends Phantom { - type BooAny = this.Any - def any: BooAny = assume -}