@@ -98,6 +98,8 @@ object TypePositions {
98
98
convert(body, tpe.resultType))
99
99
case (TypeDef (name, rhs), tpe @ TypeAlias (alias)) =>
100
100
PointPos .add(tree, tpe.derivedTypeAlias(convert(rhs, alias)))
101
+ case (TypeDef (name, rhs), tpe : PolyType ) if ! rhs.isInstanceOf [PolyTypeTree ] =>
102
+ PointPos .add(tree, tpe.derivedPolyType(resType = convert(rhs, tpe.resultType)))
101
103
case (TypeDef (name, rhs), tpe) =>
102
104
PointPos .add(tree, convert(rhs, tpe))
103
105
case (ValDef (name, tpt, _), tpe) =>
@@ -121,14 +123,16 @@ object TypePositions {
121
123
case (_, tpe) =>
122
124
tpe
123
125
}
124
- tpe1 match {
126
+ val res = tpe1 match {
125
127
case AnnotatedType (_, _ : PosAnnot ) => tpe1
126
128
case _ => if (tree.pos.exists) AnnotatedType (tpe1, PosAnnot (tree.pos)) else tpe1
127
129
}
130
+ // println(i"convert $tree: ${tree.tpe} / $tpe -> $tpe1 -> $res")
131
+ res
128
132
}
129
133
convert(tree, tree.tpe)
130
134
}
131
-
135
+
132
136
/** Given a type produce a corresponding definition with defined name `name`
133
137
*/
134
138
def toDef (name : Name , tpe : Type )(implicit ctx : Context ): MemberDef = {
@@ -185,12 +189,15 @@ object TypePositions {
185
189
*/
186
190
def toTree (tpe : Type , hasPos : Boolean = false )(implicit ctx : Context ): Tree = tpe match {
187
191
case AnnotatedType (tpe1, PosAnnot (pos)) =>
188
- val tree = toTree(tpe1, hasPos = true ) match {
189
- case t @ Select (qual, name) if name.length == pos.end - pos.start =>
190
- untpd.Ident (name).withType(t.tpe)
191
- case t => t
192
+ def addPos (tree : Tree ): Tree = tree match {
193
+ case tree @ SingletonTypeTree (ref) =>
194
+ untpd.SingletonTypeTree (addPos(ref)).withType(tree.tpe)
195
+ case tree @ Select (qual, name) if name.length == pos.end - pos.start =>
196
+ untpd.Ident (name).withType(tree.tpe).withPos(pos.toSynthetic)
197
+ case _ =>
198
+ tree.withPos(pos.toSynthetic)
192
199
}
193
- tree.withPos(pos.toSynthetic )
200
+ addPos(toTree(tpe1, hasPos = true ) )
194
201
case tpe =>
195
202
val utree : untpd.Tree = {
196
203
import untpd ._
@@ -264,12 +271,42 @@ object TypePositions {
264
271
utree.withType(tpe)
265
272
}
266
273
267
- def refPositions (tree : Tree )(implicit ctx : Context ): List [(Name , Position )] = {
268
- val b = new mutable.ListBuffer [(Name , Position )]
274
+ def adapt (rhs : Tree , tp : Type , tparams : List [untpd.TypeDef ])(implicit ctx : Context ): Tree = {
275
+ val rhs1 : untpd.Tree = tp match {
276
+ case TypeAlias (alias) if ! rhs.isInstanceOf [TypeBoundsTree ] =>
277
+ val rhs0 = adapt(rhs, alias, tparams)
278
+ TypeBoundsTree (rhs0, rhs0)
279
+ case tp : PolyType if ! rhs.isInstanceOf [PolyTypeTree ] =>
280
+ assert(tparams.length == tp.paramNames.length, i " bad poly $tp, $tparams" )
281
+ PolyTypeTree (tparams, rhs)
282
+ case _ =>
283
+ rhs
284
+ }
285
+ rhs1.withType(tp)
286
+ }
287
+
288
+ def refPositions (tree : Tree )(implicit ctx : Context ): List [(String , Position )] = {
289
+ val b = new mutable.ListBuffer [(String , Position )]
290
+ def essentialSuffix (name : Name ): String =
291
+ name.drop(name.lastIndexOf('$' ) + 1 ).toString
269
292
tree foreachSubTree {
270
- case t : RefTree => b += ((t.name, t.pos.toSynthetic))
293
+ case t : RefTree => b += ((essentialSuffix( t.name) , t.pos.toSynthetic))
271
294
case _ =>
272
295
}
273
296
b.toList
274
297
}
298
+
299
+ def checkTypePositions (posType : Type , tpt : Tree )(implicit ctx : Context ) = {
300
+ val tree = TypePositions .toTree(posType)
301
+ val origRefs = TypePositions .refPositions(tpt)
302
+ val newRefs = TypePositions .refPositions(tree)
303
+ assert(origRefs.map(_._2).toSet subsetOf newRefs.map(_._2).toSet,
304
+ i """ tpt = $tpt
305
+ |tptStr = ${tpt.toString}
306
+ |posType = $posType
307
+ |posTypeStr = ${posType.toString}
308
+ |tree = $tree
309
+ |origRefs = $origRefs%, %
310
+ |newRefs = $newRefs%, % """ )
311
+ }
275
312
}
0 commit comments