|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import core.Phases._ |
| 5 | +import core.DenotTransformers._ |
| 6 | +import core.Symbols._ |
| 7 | +import core.Contexts._ |
| 8 | +import core.Types._ |
| 9 | +import core.Decorators._ |
| 10 | +import dotty.tools.dotc.ast.{Trees, tpd} |
| 11 | +import ast.Trees._ |
| 12 | + |
| 13 | +import dotty.tools.dotc.core.Flags |
| 14 | +import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo, TreeTransform} |
| 15 | + |
| 16 | +import scala.annotation.tailrec |
| 17 | + |
| 18 | +class PhantomErasure extends MiniPhaseTransform with InfoTransformer { |
| 19 | + thisTransformer => |
| 20 | + |
| 21 | + import dotty.tools.dotc.ast.tpd._ |
| 22 | + |
| 23 | + override def phaseName: String = "phantomErasure" |
| 24 | + |
| 25 | + /** List of names of phases that should precede this phase */ |
| 26 | + override def runsAfter: Set[Class[_ <: Phase]] = |
| 27 | + Set(classOf[InterceptedMethods], classOf[Splitter], classOf[ElimRepeated]) |
| 28 | + |
| 29 | + /** Check what the phase achieves, to be called at any point after it is finished. |
| 30 | + */ |
| 31 | + override def checkPostCondition(tree: tpd.Tree)(implicit ctx: Context): Unit = { |
| 32 | + def assertNotPhantom(tpe: Type): Unit = |
| 33 | + assert(!tpe.derivesFrom(defn.PhantomAnyClass), "All phantom type values should be erased in " + tree) |
| 34 | + |
| 35 | + tree match { |
| 36 | + case _: TypeTree => |
| 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 | + case _ => assertNotPhantom(tree.tpe) |
| 42 | + } |
| 43 | + super.checkPostCondition(tree) |
| 44 | + } |
| 45 | + |
| 46 | + // Transform trees |
| 47 | + |
| 48 | + 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 | + |
| 63 | + case tree @ Apply(fun, _) if tree.tpe.derivesFrom(defn.PhantomAnyClass) => |
| 64 | + ctx.error(s"Functions returning a phantom type can not be in statement position.", fun.pos) |
| 65 | + false |
| 66 | + case tree @ TypeApply(fun, _) if tree.tpe.derivesFrom(defn.PhantomAnyClass) => |
| 67 | + ctx.error(s"Functions returning a phantom type can not be in statement position.", fun.pos) |
| 68 | + false |
| 69 | + case tree: Select if tree.tpe.derivesFrom(defn.PhantomAnyClass) => |
| 70 | + ctx.error(s"Fields containing a phantom type can not be accessed in statement position.", tree.pos) |
| 71 | + false |
| 72 | + |
| 73 | + case _ => |
| 74 | + true |
| 75 | + } |
| 76 | + super.transformStats(newTrees) |
| 77 | + } |
| 78 | + |
| 79 | + override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo): Tree = { |
| 80 | + val paramTypes = { |
| 81 | + tree.fun.typeOpt match { |
| 82 | + case tpe: TermRef => |
| 83 | + tpe.info match { |
| 84 | + case tpe2: MethodType => tpe2.paramTypes |
| 85 | + case _ => Nil |
| 86 | + } |
| 87 | + |
| 88 | + case tpe: MethodType => tpe.paramTypes |
| 89 | + case _ => Nil |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + val newTree = |
| 94 | + if (paramTypes.nonEmpty || tree.args.isEmpty) tree |
| 95 | + else cpy.Apply(tree)(tree.fun, Nil) |
| 96 | + |
| 97 | + super.transformApply(newTree) |
| 98 | + } |
| 99 | + |
| 100 | + override def transformDefDef(ddef: DefDef)(implicit ctx: Context, info: TransformerInfo): Tree = { |
| 101 | + def filterPhantom(vparamss: List[List[ValDef]]): List[List[ValDef]] = { |
| 102 | + vparamss.filter { vparams => |
| 103 | + val phantoms = vparams.filter(_.tpt.typeOpt.derivesFrom(defn.PhantomAnyClass)) |
| 104 | + if (phantoms.nonEmpty && phantoms.size != vparams.size) |
| 105 | + ctx.error("Lists of parameters with runtime and phantom types are not allowed.", vparams.head.pos) |
| 106 | + phantoms.isEmpty |
| 107 | + } |
| 108 | + } |
| 109 | + val newVparamss = filterPhantom(ddef.vparamss) |
| 110 | + val ddef1 = |
| 111 | + if (ddef.vparamss.length == newVparamss.length) ddef |
| 112 | + else cpy.DefDef(ddef)(vparamss = newVparamss) |
| 113 | + super.transformDefDef(ddef1) |
| 114 | + } |
| 115 | + |
| 116 | + // Transform symbols |
| 117 | + |
| 118 | + def transformInfo(tp: Type, sym: Symbol)(implicit ctx: Context): Type = erasedPhantoms(tp) |
| 119 | + |
| 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 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private def erasedMethodPhantoms(tp: MethodType)(implicit ctx: Context): MethodType = { |
| 138 | + val (erasedParamNames, erasedParamTypes) = |
| 139 | + if (tp.paramTypes.isEmpty || tp.paramTypes.head.derivesFrom(defn.PhantomAnyClass)) (Nil, Nil) |
| 140 | + else (tp.paramNames, tp.paramTypes) |
| 141 | + val erasedResultType = erasedPhantoms(tp.resultType) |
| 142 | + tp match { |
| 143 | + case _: ImplicitMethodType => |
| 144 | + ImplicitMethodType(erasedParamNames, erasedParamTypes, erasedResultType) |
| 145 | + case _ => |
| 146 | + MethodType(erasedParamNames, erasedParamTypes, erasedResultType) |
| 147 | + } |
| 148 | + } |
| 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 | +} |
0 commit comments