@@ -20,14 +20,15 @@ import reporting.*
2020import dotty .tools .dotc .transform .MegaPhase .MiniPhase
2121import dotty .tools .dotc .util .Property
2222import dotty .tools .dotc .cc .pathOwner
23+ import scala .annotation .tailrec
2324
2425class ErasurePreservation extends MiniPhase with InfoTransformer {
2526
2627 override def phaseName : String = ErasurePreservation .name
2728
2829 override def description : String = ErasurePreservation .description
2930
30- def toTypeA (tp : Type )(using Context ): TypeA = trace(s " toTypeA ${tp}" ) {tp match
31+ def toTypeA (tp : Type , sourceSym : Symbol )(using Context ): TypeA = trace(s " toTypeA ${tp}" ) {tp match
3132 case tpr : TypeParamRef => TypeA .M (tpr.paramNum)
3233 case tr : TypeRef =>
3334 // println(tr.symbol.owner.paramSymss)
@@ -43,7 +44,8 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
4344 val owner = tr.symbol.owner
4445 if owner.isClass then
4546 val ind = owner.typeParams.indexOf(tr.symbol)
46- if ind != - 1 then TypeA .K (ind) else ???
47+ val n = debrujin(sourceSym.enclosingClass, owner)
48+ if ind != - 1 then TypeA .K (n, ind) else ???
4749 else
4850 val ind = owner.paramSymss.headOption match
4951 case None => assert(false , i " Got unexpected type ${tp}" )
@@ -53,14 +55,19 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
5355 case _ => assert(false )
5456 }
5557
58+ def debrujin (source : Symbol , outer : Symbol )(using Context ): Int = trace(i " debrujin: $source, $outer" ) {
59+ if (source.enclosingClass == outer) then 0
60+ else debrujin(source.owner, outer)+ 1
61+ }
5662
57- def toTypeB (tp : Type )(using Context ): TypeB = trace(i " toTypeB ${tp}" ){ tp match
63+ def toTypeB (tp : Type , sourceSym : Symbol )(using Context ): TypeB = trace(i " toTypeB ${tp}" ){ tp match
5864 case tpr : TypeParamRef => TypeB .M (tpr.paramNum)
5965 case tr : TypeRef if tr.symbol.isTypeParam =>
6066 val owner = tr.symbol.owner
6167 if owner.isClass then
6268 val ind = owner.typeParams.indexOf(tr.symbol)
63- if ind != - 1 then TypeB .K (ind) else TypeB .None
69+ val n = debrujin(sourceSym.enclosingClass, owner)
70+ if ind != - 1 then TypeB .K (n, ind) else TypeB .None
6471 else
6572 val ind = owner.paramSymss.headOption match
6673 case None => assert(false , i " Got unexpected type ${tp}" )
@@ -69,12 +76,13 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
6976 case _ => TypeB .None
7077 }
7178
72- def toReturnTypeB (tp : Type )(using Context ): TypeB = tp match
79+ def toReturnTypeB (tp : Type , sourceSym : Symbol )(using Context ): TypeB = tp match
7380 case tr : TypeRef if tr.symbol.isTypeParam =>
7481 val owner = tr.symbol.owner
7582 if owner.isClass then
7683 val ind = owner.typeParams.indexOf(tr.symbol)
77- if ind != - 1 then TypeB .K (ind) else TypeB .None
84+ val n = debrujin(sourceSym.enclosingClass, owner)
85+ if ind != - 1 then TypeB .K (n, ind) else TypeB .None
7886 else
7987 val ind = owner.paramSymss.headOption match
8088 case None => assert(false , i " Got unexpected type ${tp}" )
@@ -87,16 +95,17 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
8795 case pt : PolyType =>
8896 pt.resType match
8997 case mt : MethodType =>
90- sym.addAnnotation(ErasedInfo (pt.paramInfos.size, mt.paramInfos.map(toTypeB), toTypeB(mt.resType)))
98+ // println(i"sym context: $sym, ${sym.enclosingClass}, ${sym.enclosingClass.owner}")
99+ sym.addAnnotation(ErasedInfo (pt.paramInfos.size, mt.paramInfos.map(p => toTypeB(p, sym)), toTypeB(mt.resType, sym)))
91100 case other =>
92- sym.addAnnotation(ErasedInfo (pt.paramInfos.size, Nil , toTypeB(other.widenExpr)))
101+ sym.addAnnotation(ErasedInfo (pt.paramInfos.size, Nil , toTypeB(other.widenExpr, sym )))
93102 case mt : MethodType =>
94- val params = mt.paramInfos.map(toTypeB)
95- val ret = toTypeB(mt.resType)
103+ val params = mt.paramInfos.map(p => toTypeB(p, sym) )
104+ val ret = toTypeB(mt.resType, sym )
96105 if (params.exists(_ != TypeB .None ) || ret != TypeB .None ) then
97106 sym.addAnnotation(ErasedInfo (0 , params, ret))
98107 case et : ExprType =>
99- val ret = toTypeB(et.widenExpr)
108+ val ret = toTypeB(et.widenExpr, sym )
100109 if (ret != TypeB .None ) then
101110 sym.addAnnotation(ErasedInfo (0 , Nil , ret))
102111 ()
@@ -105,11 +114,11 @@ class ErasurePreservation extends MiniPhase with InfoTransformer {
105114 }
106115
107116 override def transformApply (tree : tpd.Apply )(using Context ): tpd.Tree =
108- tree.putAttachment(InvokeReturnType , toReturnTypeB(tree.tpe))
117+ tree.putAttachment(InvokeReturnType , toReturnTypeB(tree.tpe, tree.symbol ))
109118 tree
110119
111120 override def transformTypeApply (tree : tpd.TypeApply )(using Context ): tpd.Tree =
112- val args = tree.args.map(_.tpe).map(toTypeA)
121+ val args = tree.args.map(_.tpe).map(p => toTypeA(p, tree.symbol) )
113122 tree.fun.putAttachment(InstructionTypeArguments , args) // Pattern match args based on their types
114123 tree
115124
@@ -135,15 +144,15 @@ enum TypeA:
135144 case Boolean
136145 case M (paramNum : Int )
137146 case K (
138- // outer: Int,
147+ outer : Int ,
139148 paramNum : Int )
140149 case Ref
141150
142151enum TypeB :
143152 case None
144153 case M (paramNum : Int )
145154 case K (
146- // outer: Int,
155+ outer : Int ,
147156 paramNum : Int )
148157// case class TypeB(tp: Type)
149158
0 commit comments