Skip to content

Commit 8eca7de

Browse files
committed
Add method attachments
1 parent b5d8941 commit 8eca7de

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ class Definitions {
11001100
@tu lazy val RetainsArgAnnot: ClassSymbol = requiredClass("scala.annotation.retainsArg")
11011101
@tu lazy val PublicInBinaryAnnot: ClassSymbol = requiredClass("scala.annotation.publicInBinary")
11021102
@tu lazy val WitnessNamesAnnot: ClassSymbol = requiredClass("scala.annotation.internal.WitnessNames")
1103+
@tu lazy val ErasurePreservationAnnot: ClassSymbol = requiredClass("scala.annotation.internal.ErasurePreservation")
11031104

11041105
@tu lazy val JavaRepeatableAnnot: ClassSymbol = requiredClass("java.lang.annotation.Repeatable")
11051106

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ import util.Chars.isOperatorPart
2727
import config.{Config, Feature}
2828
import config.Feature.sourceVersion
2929
import config.SourceVersion.*
30+
import reporting.*
31+
import dotty.tools.dotc.core.Decorators.i
3032

3133
import dotty.tools.dotc.util.SourcePosition
3234
import dotty.tools.dotc.ast.untpd.{MemberDef, Modifiers, PackageDef, RefTree, Template, TypeDef, ValOrDefDef}
3335
import cc.*
3436
import dotty.tools.dotc.parsing.JavaParsers
3537
import dotty.tools.dotc.transform.TreeExtractors.BinaryOp
38+
import dotty.tools.dotc.transform.InstructionTypeArguments
39+
import dotty.tools.dotc.transform.InvokeReturnType
3640

3741
class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
3842

@@ -462,7 +466,9 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
462466

463467
toTextLocal(expr) ~ "." ~ selectorsText
464468

465-
tree match {
469+
(if tree.hasAttachment(InvokeReturnType) then Str("InvokeReturnType("+tree.attachment(InvokeReturnType).toString + ") ") else Str("")) ~
470+
(if tree.hasAttachment(InstructionTypeArguments) then Str("InstructionTypeArguments("+tree.attachment(InstructionTypeArguments).toString + ") ") else Str("")) ~
471+
(tree match {
466472
case id: Trees.SearchFailureIdent[?] =>
467473
tree.typeOpt match {
468474
case reason: Implicits.SearchFailureType =>
@@ -850,7 +856,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
850856
toText(pname) ~ " : " ~ toText(tycon) ~ (" as " ~ toText(ownName) `provided` !ownName.isEmpty)
851857
case _ =>
852858
tree.fallbackToText(this)
853-
}
859+
})
854860
}
855861

856862
override protected def toTextCapturing(tp: Type, refs: GeneralCaptureSet, boxText: Text): Text = tp match

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

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,87 @@ import typer.NoChecking
1818
import ast.{tpd, untpd}
1919
import reporting.*
2020
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
21+
import dotty.tools.dotc.util.Property
2122

22-
class ErasurePreservation extends MiniPhase {
23+
class ErasurePreservation extends MiniPhase with InfoTransformer {
2324

2425
override def phaseName: String = ErasurePreservation.name
2526

2627
override def description: String = ErasurePreservation.description
2728

28-
override def transformDefDef(tree: tpd.DefDef)(using Context): tpd.Tree =
29-
val sym = tree.symbol
30-
sym.info match
29+
def toTypeA(tp: Type)(using Context): TypeA = trace(s"toTypeA ${tp}") {tp match
30+
case tpr: TypeParamRef => TypeA.M(tpr.paramNum)
31+
case tr: TypeRef =>
32+
// println(tr.symbol.owner.paramSymss)
33+
if tr.isRef(defn.IntClass) then TypeA.Int else
34+
if tr.symbol.isTypeParam then
35+
val ind = tr.symbol.owner.paramSymss.head.indexWhere(tr.isRef(_))
36+
if ind != -1 then TypeA.M(ind)
37+
else TypeA.Ref
38+
else TypeA.Ref
39+
case _ => assert(false)
40+
}
41+
42+
43+
def toTypeB(tp: Type)(using Context): TypeB = tp match
44+
case tpr: TypeParamRef => TypeB.M(tpr.paramNum)
45+
case _ => TypeB.None
46+
47+
def toReturnTypeB(tp: Type)(using Context): TypeB = tp match
48+
case tr: TypeRef =>
49+
// println(tr.symbol.owner.paramSymss)
50+
if tr.symbol.isTypeParam then
51+
val ind = tr.symbol.owner.paramSymss.head.indexWhere(tr.isRef(_))
52+
if ind != -1 then TypeB.M(ind)
53+
else TypeB.None
54+
else TypeB.None
55+
case _ => TypeB.None
56+
57+
58+
override def transformInfo(tp: Type, sym: Symbol)(using Context): Type =
59+
tp match
3160
case pt: PolyType =>
32-
def toTypeB(tp: Type): TypeB = tp match
33-
case tpr: TypeParamRef => TypeB.M(tpr.paramNum)
34-
case _ => TypeB.None
3561
pt.resType match
3662
case mt: MethodType =>
3763
sym.addAnnotation(ErasedInfo(pt.paramInfos.size, mt.paramInfos.map(toTypeB), toTypeB(mt.resType)))
3864
case other =>
3965
sym.addAnnotation(ErasedInfo(pt.paramInfos.size, Nil, toTypeB(other.widenExpr)))
4066
case _ => ()
67+
tp
68+
69+
override def transformApply(tree: tpd.Apply)(using Context): tpd.Tree =
70+
tree.putAttachment(InvokeReturnType, toReturnTypeB(tree.tpe))
4171
tree
72+
73+
override def transformTypeApply(tree: tpd.TypeApply)(using Context): tpd.Tree =
74+
val args = tree.args.map(_.tpe).map(toTypeA)
75+
tree.fun.putAttachment(InstructionTypeArguments, args) // Pattern match args based on their types
76+
tree
77+
4278
}
4379

4480
object ErasurePreservation {
4581
val name: String = "erasure preservation"
4682
val description: String = "preserve information in annotations before erasure"
4783
}
4884

85+
enum TypeA:
86+
case Int
87+
case M(x: Int)
88+
case K(x: Int)
89+
case Ref
90+
4991
enum TypeB:
5092
case None
5193
case M(x: Int)
5294
// case class TypeB(tp: Type)
5395

96+
object InstructionTypeArguments extends Property.StickyKey[List[TypeA]]
97+
object InvokeReturnType extends Property.StickyKey[TypeB]
98+
5499
class ErasedInfo(paramCount: Int, paramType: List[TypeB], returnType: TypeB) extends Annotation {
55100
override def tree(using Context) =
56-
tpd.New(defn.SourceFileAnnot.typeRef,
101+
tpd.New(defn.ErasurePreservationAnnot.typeRef,
57102
List(tpd.Literal(Constant(toString))))
58103

59104
override def toString =
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package scala.annotation.internal
2+
3+
import scala.annotation.Annotation
4+
5+
class ErasurePreservation(path: String) extends Annotation {
6+
7+
}

0 commit comments

Comments
 (0)