Skip to content

Commit 7f0c087

Browse files
committed
Disallow phantom var
1 parent 5db4230 commit 7f0c087

File tree

7 files changed

+14
-73
lines changed

7 files changed

+14
-73
lines changed

compiler/src/dotty/tools/dotc/transform/FirstTransform.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota
162162
}
163163

164164
override def transformValDef(vdef: tpd.ValDef)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
165-
if (vdef.symbol.hasAnnotation(defn.VolatileAnnot) && vdef.tpt.tpe.isPhantom)
166-
ctx.error("Phantom fields can not be @volatile", vdef.pos)
165+
if (vdef.tpt.tpe.isPhantom) {
166+
if (vdef.symbol.is(Mutable)) ctx.error("var fields can not have Phantom types", vdef.pos)
167+
else if (vdef.symbol.hasAnnotation(defn.VolatileAnnot)) ctx.error("Phantom fields can not be @volatile", vdef.pos)
168+
}
167169
vdef
168170
}
169171

tests/neg/phantom-var.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
class Foo {
3+
var foo = Boo.boo // error: var fields can not have Phantom types
4+
}
5+
6+
object Boo extends Phantom {
7+
def boo = assume
8+
}

tests/neg/phantom-volitile.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
class Foo {
3-
@volatile var foo = Boo.boo // error
3+
@volatile var foo1 = Boo.boo // error: var fields can not have Phantom types
4+
@volatile val foo2 = Boo.boo // error: Phantom fields can not be @volatile
45
}
56

67
object Boo extends Phantom {

tests/run/phantom-var-2.check

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/run/phantom-var-2.scala

Lines changed: 0 additions & 35 deletions
This file was deleted.

tests/run/phantom-var.check

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/run/phantom-var.scala

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)