Skip to content

Commit b5d8941

Browse files
committed
Add Erasure Preservation Phase
1 parent 13077f7 commit b5d8941

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class Compiler {
109109
new TupleOptimizations, // Optimize generic operations on tuples
110110
new LetOverApply, // Lift blocks from receivers of applications
111111
new ArrayConstructors) :: // Intercept creation of (non-generic) arrays and intrinsify.
112+
List(new ErasurePreservation) :: //
112113
List(new Erasure) :: // Rewrite types to JVM model, erasing all type parameters, abstract types and refinements.
113114
List(new ElimErasedValueType, // Expand erased value types to their underlying implementation types
114115
new PureStats, // Remove pure stats from blocks

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,18 @@ object Erasure {
926926
* with more than `MaxImplementedFunctionArity` parameters to use a single
927927
* parameter of type `[]Object`.
928928
*/
929-
override def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(using Context): Tree =
929+
override def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(using Context): Tree = // trace(i"typedDefDef $sym,"+s" ${atPhase(ctx.phaseId-1)(sym.info)}") {// Possible Annotation target
930+
// atPhase(ctx.phaseId-1)(sym.info) match
931+
// case pt: PolyType =>
932+
// def toTypeB(tp: Type): TypeB = tp match
933+
// case tpr: TypeParamRef => TypeB.M(tpr.paramNum)
934+
// case _ => TypeB.None
935+
// pt.resType match
936+
// case mt: MethodType =>
937+
// sym.addAnnotation(ErasedInfo(pt.paramInfos.size, mt.paramInfos.map(toTypeB), toTypeB(mt.resType)))
938+
// case other =>
939+
// sym.addAnnotation(ErasedInfo(pt.paramInfos.size, Nil, toTypeB(other.widenExpr)))
940+
// case _ => ()
930941
if sym.isEffectivelyErased || sym.name.is(BodyRetainerName) then
931942
erasedDef(sym)
932943
else
@@ -970,6 +981,7 @@ object Erasure {
970981
tpt = untpd.TypedSplice(TypeTree(restpe).withSpan(ddef.tpt.span)),
971982
rhs = rhs1)
972983
super.typedDefDef(ddef1, sym)
984+
// }
973985
end typedDefDef
974986

975987
/** The outer parameter definition of a constructor if it needs one */
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package dotty.tools
2+
package dotc
3+
package transform
4+
5+
import ast.Trees
6+
import core.Phases.*
7+
import core.DenotTransformers.*
8+
import core.Annotations.*
9+
import core.Denotations.*
10+
import core.SymDenotations.*
11+
import core.Symbols.*
12+
import core.Contexts.*
13+
import core.Types.*
14+
import core.Names.*
15+
import core.Constants.*
16+
import core.Decorators.*
17+
import typer.NoChecking
18+
import ast.{tpd, untpd}
19+
import reporting.*
20+
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
21+
22+
class ErasurePreservation extends MiniPhase {
23+
24+
override def phaseName: String = ErasurePreservation.name
25+
26+
override def description: String = ErasurePreservation.description
27+
28+
override def transformDefDef(tree: tpd.DefDef)(using Context): tpd.Tree =
29+
val sym = tree.symbol
30+
sym.info match
31+
case pt: PolyType =>
32+
def toTypeB(tp: Type): TypeB = tp match
33+
case tpr: TypeParamRef => TypeB.M(tpr.paramNum)
34+
case _ => TypeB.None
35+
pt.resType match
36+
case mt: MethodType =>
37+
sym.addAnnotation(ErasedInfo(pt.paramInfos.size, mt.paramInfos.map(toTypeB), toTypeB(mt.resType)))
38+
case other =>
39+
sym.addAnnotation(ErasedInfo(pt.paramInfos.size, Nil, toTypeB(other.widenExpr)))
40+
case _ => ()
41+
tree
42+
}
43+
44+
object ErasurePreservation {
45+
val name: String = "erasure preservation"
46+
val description: String = "preserve information in annotations before erasure"
47+
}
48+
49+
enum TypeB:
50+
case None
51+
case M(x: Int)
52+
// case class TypeB(tp: Type)
53+
54+
class ErasedInfo(paramCount: Int, paramType: List[TypeB], returnType: TypeB) extends Annotation {
55+
override def tree(using Context) =
56+
tpd.New(defn.SourceFileAnnot.typeRef,
57+
List(tpd.Literal(Constant(toString))))
58+
59+
override def toString =
60+
s"$paramCount, $paramType, $returnType"
61+
}

0 commit comments

Comments
 (0)