@@ -15,12 +15,12 @@ import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, Transforme
1515
1616import scala .annotation .tailrec
1717
18- class PhantomErasure extends MiniPhaseTransform with InfoTransformer {
18+ class PhantomRefErasure extends MiniPhaseTransform with InfoTransformer {
1919 thisTransformer =>
2020
2121 import dotty .tools .dotc .ast .tpd ._
2222
23- override def phaseName : String = " phantomErasure "
23+ override def phaseName : String = " phantomRefErasure "
2424
2525 /** List of names of phases that should precede this phase */
2626 override def runsAfter : Set [Class [_ <: Phase ]] =
@@ -35,9 +35,6 @@ class PhantomErasure extends MiniPhaseTransform with InfoTransformer {
3535 tree match {
3636 case _ : TypeTree =>
3737 case _ : Trees .TypeDef [_] =>
38- case tree : TypeDef => tree.symbol.asClass.classInfo.decls // TODO check decls
39- case ValDef (_, tpt, _) => assertNotPhantom(tpt.typeOpt)
40- case DefDef (_, _, _, tpt, _) => assertNotPhantom(tpt.typeOpt)
4138 case _ => assertNotPhantom(tree.tpe)
4239 }
4340 super .checkPostCondition(tree)
@@ -46,34 +43,16 @@ class PhantomErasure extends MiniPhaseTransform with InfoTransformer {
4643 // Transform trees
4744
4845 override def transformStats (trees : List [tpd.Tree ])(implicit ctx : Context , info : TransformerInfo ): List [tpd.Tree ] = {
49- val newTrees = trees.filter {
50- case ValDef (_, tpt, _) =>
51- ! tpt.tpe.derivesFrom(defn.PhantomAnyClass )
52-
53- case tree @ DefDef (_, _, _, tpt, _) =>
54- val isPhantom = tpt.tpe.derivesFrom(defn.PhantomAnyClass )
55- if (isPhantom && tree.symbol.flags.is(Flags .Accessor )) {
56- if (tree.symbol.flags.is(Flags .Mutable ))
57- ctx.error(" Can not define var with phantom type." , tree.pos)
58- else if (tree.symbol.flags.is(Flags .Lazy ))
59- ctx.error(" Can not define lazy var with phantom type." , tree.pos)
60- }
61- ! isPhantom
62-
46+ trees.foreach {
6347 case tree @ Apply (fun, _) if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
6448 ctx.error(s " Functions returning a phantom type can not be in statement position. " , fun.pos)
65- false
6649 case tree @ TypeApply (fun, _) if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
6750 ctx.error(s " Functions returning a phantom type can not be in statement position. " , fun.pos)
68- false
6951 case tree : Select if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
7052 ctx.error(s " Fields containing a phantom type can not be accessed in statement position. " , tree.pos)
71- false
72-
7353 case _ =>
74- true
7554 }
76- super .transformStats(newTrees )
55+ super .transformStats(trees )
7756 }
7857
7958 override def transformApply (tree : Apply )(implicit ctx : Context , info : TransformerInfo ): Tree = {
@@ -117,21 +96,11 @@ class PhantomErasure extends MiniPhaseTransform with InfoTransformer {
11796
11897 def transformInfo (tp : Type , sym : Symbol )(implicit ctx : Context ): Type = erasedPhantoms(tp)
11998
120- private def erasedPhantoms (tp : Type )(implicit ctx : Context ): Type = {
121- val flags = tp.typeSymbol.flags
122- tp match {
123- case tp : ClassInfo if ! flags.is(Flags .Package ) =>
124- val newDecls = tp.decls.filteredScope(sym => ! isPhantomMethodType(sym.info))
125- ClassInfo (tp.prefix, tp.cls, tp.classParents, newDecls, tp.selfInfo)
126-
127- case tp : JavaMethodType => tp
128- case tp : MethodType => erasedMethodPhantoms(tp)
129-
130- case tp : PolyType =>
131- PolyType (tp.paramNames)(_ => tp.paramBounds, _ => erasedPhantoms(tp.resType))
132-
133- case _ => tp
134- }
99+ private def erasedPhantoms (tp : Type )(implicit ctx : Context ): Type = tp match {
100+ case tp : JavaMethodType => tp
101+ case tp : MethodType => erasedMethodPhantoms(tp)
102+ case tp : PolyType => PolyType (tp.paramNames)(_ => tp.paramBounds, _ => erasedPhantoms(tp.resType))
103+ case _ => tp
135104 }
136105
137106 private def erasedMethodPhantoms (tp : MethodType )(implicit ctx : Context ): MethodType = {
@@ -140,15 +109,8 @@ class PhantomErasure extends MiniPhaseTransform with InfoTransformer {
140109 else (tp.paramNames, tp.paramTypes)
141110 val erasedResultType = erasedPhantoms(tp.resultType)
142111 tp match {
143- case _ : ImplicitMethodType =>
144- ImplicitMethodType (erasedParamNames, erasedParamTypes, erasedResultType)
145- case _ =>
146- MethodType (erasedParamNames, erasedParamTypes, erasedResultType)
112+ case _ : ImplicitMethodType => ImplicitMethodType (erasedParamNames, erasedParamTypes, erasedResultType)
113+ case _ => MethodType (erasedParamNames, erasedParamTypes, erasedResultType)
147114 }
148115 }
149-
150- @ tailrec private def isPhantomMethodType (tpe : Type )(implicit ctx : Context ): Boolean = tpe match {
151- case tpe : MethodicType => tpe.resultType.derivesFrom(defn.PhantomAnyClass ) || isPhantomMethodType(tpe.resultType)
152- case _ => false
153- }
154116}
0 commit comments