@@ -15,12 +15,12 @@ import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, Transforme
15
15
16
16
import scala .annotation .tailrec
17
17
18
- class PhantomErasure extends MiniPhaseTransform with InfoTransformer {
18
+ class PhantomRefErasure extends MiniPhaseTransform with InfoTransformer {
19
19
thisTransformer =>
20
20
21
21
import dotty .tools .dotc .ast .tpd ._
22
22
23
- override def phaseName : String = " phantomErasure "
23
+ override def phaseName : String = " phantomRefErasure "
24
24
25
25
/** List of names of phases that should precede this phase */
26
26
override def runsAfter : Set [Class [_ <: Phase ]] =
@@ -35,9 +35,6 @@ class PhantomErasure extends MiniPhaseTransform with InfoTransformer {
35
35
tree match {
36
36
case _ : TypeTree =>
37
37
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)
41
38
case _ => assertNotPhantom(tree.tpe)
42
39
}
43
40
super .checkPostCondition(tree)
@@ -46,34 +43,16 @@ class PhantomErasure extends MiniPhaseTransform with InfoTransformer {
46
43
// Transform trees
47
44
48
45
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 {
63
47
case tree @ Apply (fun, _) if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
64
48
ctx.error(s " Functions returning a phantom type can not be in statement position. " , fun.pos)
65
- false
66
49
case tree @ TypeApply (fun, _) if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
67
50
ctx.error(s " Functions returning a phantom type can not be in statement position. " , fun.pos)
68
- false
69
51
case tree : Select if tree.tpe.derivesFrom(defn.PhantomAnyClass ) =>
70
52
ctx.error(s " Fields containing a phantom type can not be accessed in statement position. " , tree.pos)
71
- false
72
-
73
53
case _ =>
74
- true
75
54
}
76
- super .transformStats(newTrees )
55
+ super .transformStats(trees )
77
56
}
78
57
79
58
override def transformApply (tree : Apply )(implicit ctx : Context , info : TransformerInfo ): Tree = {
@@ -117,21 +96,11 @@ class PhantomErasure extends MiniPhaseTransform with InfoTransformer {
117
96
118
97
def transformInfo (tp : Type , sym : Symbol )(implicit ctx : Context ): Type = erasedPhantoms(tp)
119
98
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
135
104
}
136
105
137
106
private def erasedMethodPhantoms (tp : MethodType )(implicit ctx : Context ): MethodType = {
@@ -140,15 +109,8 @@ class PhantomErasure extends MiniPhaseTransform with InfoTransformer {
140
109
else (tp.paramNames, tp.paramTypes)
141
110
val erasedResultType = erasedPhantoms(tp.resultType)
142
111
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)
147
114
}
148
115
}
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
- }
154
116
}
0 commit comments