@@ -9,16 +9,15 @@ import core.Types._
99import core .Decorators ._
1010import dotty .tools .dotc .ast .{Trees , tpd }
1111import ast .Trees ._
12-
12+ import dotty . tools . dotc . core . StdNames . _
1313import dotty .tools .dotc .core .Flags
1414import dotty .tools .dotc .transform .TreeTransforms .{MiniPhaseTransform , TransformerInfo , TreeTransform }
15+ import dotty .tools .dotc .util .Positions .Position
1516
1617import scala .annotation .tailrec
1718
1819class PhantomRefErasure extends MiniPhaseTransform with InfoTransformer {
19- thisTransformer =>
20-
21- import dotty .tools .dotc .ast .tpd ._
20+ import tpd ._
2221
2322 override def phaseName : String = " phantomRefErasure"
2423
@@ -28,7 +27,7 @@ class PhantomRefErasure extends MiniPhaseTransform with InfoTransformer {
2827
2928 /** Check what the phase achieves, to be called at any point after it is finished.
3029 */
31- override def checkPostCondition (tree : tpd. Tree )(implicit ctx : Context ): Unit = {
30+ override def checkPostCondition (tree : Tree )(implicit ctx : Context ): Unit = {
3231 def assertNotPhantom (tpe : Type ): Unit =
3332 assert(! tpe.derivesFrom(defn.PhantomAnyClass ), " All phantom type values should be erased in " + tree)
3433
@@ -42,17 +41,32 @@ class PhantomRefErasure extends MiniPhaseTransform with InfoTransformer {
4241
4342 // Transform trees
4443
45- override def transformStats (trees : List [tpd.Tree ])(implicit ctx : Context , info : TransformerInfo ): List [tpd.Tree ] = {
46- trees.foreach {
47- case tree @ Apply (fun, _) if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
48- ctx.error(s " Functions returning a phantom type can not be in statement position. " , fun.pos)
49- case tree @ TypeApply (fun, _) if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
50- ctx.error(s " Functions returning a phantom type can not be in statement position. " , fun.pos)
51- case tree : Select if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
52- ctx.error(s " Fields containing a phantom type can not be accessed in statement position. " , tree.pos)
44+ override def transformStats (trees : List [Tree ])(implicit ctx : Context , info : TransformerInfo ): List [Tree ] = {
45+ ctx.owner match {
46+ case cls : ClassSymbol if cls.classDenot.classSymbol.derivesFrom(defn.PhantomAnyClass ) =>
47+ trees.foreach {
48+ case tree : TypeDef =>
49+ case tree : DefDef =>
50+ case tree =>
51+ ctx.error(s " Phantom classes can not have expressions in statement position. " , tree.pos)
52+ }
5353 case _ =>
54+ trees.foreach {
55+ case tree@ Apply (fun, _) =>
56+ fun match {
57+ case Select (_ : This , name) if name == nme.CONSTRUCTOR =>
58+ case _ if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
59+ ctx.error(s " Functions returning a phantom type can not be in statement position. " , fun.pos)
60+ case _ =>
61+ }
62+ case tree@ TypeApply (fun, _) if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
63+ ctx.error(s " Functions returning a phantom type can not be in statement position. " , fun.pos)
64+ case tree : Select if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
65+ ctx.error(s " Fields containing a phantom type can not be accessed in statement position. " , tree.pos)
66+ case _ =>
67+ }
5468 }
55- super .transformStats( trees)
69+ trees
5670 }
5771
5872 override def transformApply (tree : Apply )(implicit ctx : Context , info : TransformerInfo ): Tree = {
0 commit comments