Skip to content

Commit 8c17945

Browse files
committed
Use attachments instead of annotations
1 parent ab3a169 commit 8c17945

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import dotty.tools.dotc.parsing.JavaParsers
3737
import dotty.tools.dotc.transform.TreeExtractors.BinaryOp
3838
import dotty.tools.dotc.transform.InstructionTypeArguments
3939
import dotty.tools.dotc.transform.InvokeReturnType
40+
import dotty.tools.dotc.transform.MethodParameterReturnType
4041

4142
class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
4243

@@ -468,6 +469,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
468469

469470
(if tree.hasAttachment(InvokeReturnType) then Str("InvokeReturnType("+tree.attachment(InvokeReturnType).toString + ") ") else Str("")) ~
470471
(if tree.hasAttachment(InstructionTypeArguments) then Str("InstructionTypeArguments("+tree.attachment(InstructionTypeArguments).toString + ") ") else Str("")) ~
472+
(if tree.hasAttachment(MethodParameterReturnType) then Str("MethodParameterReturnType("+tree.attachment(MethodParameterReturnType).toString + ") ") else Str("")) ~
471473
(tree match {
472474
case id: Trees.SearchFailureIdent[?] =>
473475
tree.typeOpt match {

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

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ import dotty.tools.dotc.util.Property
2222
import dotty.tools.dotc.cc.pathOwner
2323
import scala.annotation.tailrec
2424

25-
class ErasurePreservation extends MiniPhase with InfoTransformer {
25+
class ErasurePreservation extends MiniPhase {
2626

2727
override def phaseName: String = ErasurePreservation.name
2828

2929
override def description: String = ErasurePreservation.description
3030

31-
def toTypeA(tp: Type, sourceSym: Symbol)(using Context): TypeA = trace(s"toTypeA ${tp}") {tp match
31+
def toTypeA(tp: Type, sourceSym: Symbol)(using Context): TypeA = trace(i"toTypeA ${tp}, ${sourceSym}") {tp.widen match
3232
case tpr: TypeParamRef => TypeA.M(tpr.paramNum)
3333
case tr: TypeRef =>
3434
// println(tr.symbol.owner.paramSymss)
@@ -56,7 +56,7 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
5656
TypeA.Ref
5757
}
5858

59-
def debrujin(source: Symbol, outer: Symbol)(using Context): Int = trace.force(i"debrujin: $source, $outer") {
59+
def debrujin(source: Symbol, outer: Symbol)(using Context): Int = trace(i"debrujin: $source, $outer") {
6060
if (source.enclosingClass == outer) then 0
6161
else
6262
// println(s"$source, $outer, ${outer.owner}")
@@ -94,28 +94,32 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
9494
if ind != -1 then TypeB.M(ind) else TypeB.None
9595
case _ => TypeB.None
9696

97-
override def transformInfo(tp: Type, sym: Symbol)(using Context): Type = trace(i"transformInfo ${tp}, ${sym}") {
98-
tp match
99-
case pt: PolyType =>
100-
pt.resType match
101-
case mt: MethodType =>
102-
sym.addAnnotation(ErasedInfo(pt.paramInfos.size, mt.paramInfos.map(p => toTypeB(p, sym)), toTypeB(mt.resType, sym)))
103-
case other =>
104-
sym.addAnnotation(ErasedInfo(pt.paramInfos.size, Nil, toTypeB(other.widenExpr, sym)))
105-
case mt: MethodType =>
106-
val params = mt.paramInfos.map(p => toTypeB(p, sym))
107-
// println(i"MethodType")
108-
val ret = toTypeB(mt.resType, sym)
109-
if (params.exists(_ != TypeB.None) || ret != TypeB.None) then
110-
sym.addAnnotation(ErasedInfo(0, params, ret))
111-
case et: ExprType =>
112-
// println(i"ExprType")
113-
val ret = toTypeB(et.widenExpr, sym)
114-
if (ret != TypeB.None) then
115-
sym.addAnnotation(ErasedInfo(0, Nil, ret))
116-
()
117-
case other =>
118-
tp
97+
override def transformDefDef(tree: tpd.DefDef)(using Context): tpd.Tree = trace(i"transformDefDef $tree, ${tree.tpe}, ${tree.tpe.widen}"){
98+
tree.tpe.widen match
99+
case pt: PolyType =>
100+
pt.resType match
101+
case mt: MethodType =>
102+
tree.putAttachment(MethodParameterReturnType,
103+
(pt.paramInfos.size, mt.paramInfos.map(p => toTypeB(p, ctx.owner)), toTypeB(mt.resType, ctx.owner)))
104+
case other =>
105+
tree.putAttachment(MethodParameterReturnType,
106+
(pt.paramInfos.size, Nil, toTypeB(other.widenExpr, ctx.owner)))
107+
case mt: MethodType =>
108+
val params = mt.paramInfos.map(p => toTypeB(p, ctx.owner))
109+
// println(i"MethodType")
110+
val ret = toTypeB(mt.resType, ctx.owner)
111+
if (params.exists(_ != TypeB.None) || ret != TypeB.None) then
112+
tree.putAttachment(MethodParameterReturnType,
113+
(0, params, ret))
114+
case tr: TypeRef =>
115+
tree.putAttachment(MethodParameterReturnType, (0, Nil, toTypeB(tr, ctx.owner)))
116+
case et: ExprType =>
117+
???
118+
val ret = toTypeB(et.widenExpr, ctx.owner)
119+
if (ret != TypeB.None) then
120+
tree.putAttachment(MethodParameterReturnType, (0, Nil, ret))
121+
case other => ()
122+
tree
119123
}
120124

121125
override def transformApply(tree: tpd.Apply)(using Context): tpd.Tree = trace(i"transfromApply ${tree}") {
@@ -128,9 +132,6 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
128132
tree.fun.putAttachment(InstructionTypeArguments, args) // Pattern match args based on their types
129133
tree
130134
}
131-
// override def transformTypeDef(tree: tpd.TypeDef)(using Context): tpd.Tree =
132-
// println(s"$tree")
133-
// tree
134135

135136
}
136137

@@ -164,6 +165,7 @@ enum TypeB:
164165

165166
object InstructionTypeArguments extends Property.StickyKey[List[TypeA]]
166167
object InvokeReturnType extends Property.StickyKey[TypeB]
168+
object MethodParameterReturnType extends Property.StickyKey[(Int, List[TypeB], TypeB)]
167169

168170
class ErasedInfo(val paramCount: Int, val paramType: List[TypeB], val returnType: TypeB) extends Annotation {
169171
override def tree(using Context) =

0 commit comments

Comments
 (0)