Skip to content

Commit 9e62d33

Browse files
committed
1 parent 3c1ec5a commit 9e62d33

File tree

4 files changed

+37
-58
lines changed

4 files changed

+37
-58
lines changed

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

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -130,51 +130,41 @@ class ReifyQuotes extends MacroTransformWithImplicits {
130130
* defined versions. As a side effect, prepend the expressions `tag1, ..., `tagN`
131131
* as splices to `embedded`.
132132
*/
133-
private def addTags(expr: Tree)(implicit ctx: Context): Tree =
133+
private def addTags(expr: Tree)(implicit ctx: Context): Tree = {
134+
135+
def mkTagSymbolAndAssign(typeRef: TypeRef, tag: Tree): Tree = {
136+
val rhs = transform(tag.select(tpnme.UNARY_~))
137+
val alias = ctx.typeAssigner.assignType(untpd.TypeBoundsTree(rhs, rhs), rhs, rhs)
138+
139+
val original = typeRef.symbol.asType
140+
141+
val local = ctx.newSymbol(
142+
owner = ctx.owner,
143+
name = UniqueName.fresh("T".toTermName).toTypeName,
144+
flags = Synthetic,
145+
info = TypeAlias(tag.tpe.select(tpnme.UNARY_~)),
146+
coord = typeRef.prefix.termSymbol.coord).asType
147+
148+
ctx.typeAssigner.assignType(untpd.TypeDef(local.name, alias), local)
149+
}
150+
134151
if (importedTags.isEmpty && explicitTags.isEmpty) expr
135152
else {
136153
val itags = importedTags.toList
137154

138-
println("addTags: expr: " + expr)
139-
println("addTags: tags: ")
140-
itags.foreach(println)
141-
142155
val typeDefs = for ((tref, tag) <- itags) yield {
143-
val rhs = transform(tag.select(tpnme.UNARY_~))
144-
val alias = ctx.typeAssigner.assignType(untpd.TypeBoundsTree(rhs, rhs), rhs, rhs)
145-
val original = tref.symbol.asType
146-
val local = original.copy(
147-
owner = ctx.owner,
148-
name = (original.name + "$$").toTypeName,
149-
flags = Synthetic,
150-
info = TypeAlias(tag.tpe.select(tpnme.UNARY_~))).asType
151-
152-
ctx.typeAssigner.assignType(untpd.TypeDef(local.name, alias), local)
156+
mkTagSymbolAndAssign(tref, tag)
153157
}
154158
importedTags.clear()
155159

156-
157160
val explicitTypeDefs = for (tref <- explicitTags) yield {
158161
val tag = ref(tref.prefix.termSymbol)
159-
val rhs = transform(tag.select(tpnme.UNARY_~))
160-
161-
val alias = ctx.typeAssigner.assignType(untpd.TypeBoundsTree(rhs, rhs), rhs, rhs)
162-
163-
val local = ctx.newSymbol(
164-
owner = ctx.owner,
165-
name = UniqueName.fresh("ttt".toTermName).toTypeName,
166-
flags = Synthetic,
167-
info = TypeAlias(tag.tpe.select(tpnme.UNARY_~)),
168-
coord = tref.prefix.termSymbol.coord).asType
169-
170-
(tref, ctx.typeAssigner.assignType(untpd.TypeDef(local.name, alias), local))
162+
mkTagSymbolAndAssign(tref, tag)
171163
}
172-
val map: Map[Type, Type] = explicitTypeDefs.map(x => (x._1, x._2.symbol.typeRef)).toMap
173164

174-
println()
175-
println(map)
176-
println()
177-
println()
165+
val tagsExplicitTypeDefsPairs = explicitTags.zip(explicitTypeDefs)
166+
167+
val map: Map[Type, Type] = tagsExplicitTypeDefsPairs.map(x => (x._1, x._2.symbol.typeRef)).toMap
178168

179169
val tMap = new TypeMap() {
180170
override def apply(tp: Type): Type = {
@@ -185,16 +175,14 @@ class ReifyQuotes extends MacroTransformWithImplicits {
185175
}
186176
}
187177

188-
val ret = Block(typeDefs ++ explicitTypeDefs.map(_._2),
189-
new TreeTypeMap(typeMap = tMap,
190-
substFrom = itags.map(_._1.symbol), substTo = typeDefs.map(_.symbol))
191-
.apply(expr))
192-
193-
println("addTags: ret: " + ret)
194-
println(ret.show)
195-
println()
196-
ret
178+
Block(typeDefs ++ tagsExplicitTypeDefsPairs.map(_._2),
179+
new TreeTypeMap(
180+
typeMap = tMap,
181+
substFrom = itags.map(_._1.symbol),
182+
substTo = typeDefs.map(_.symbol)
183+
).apply(expr))
197184
}
185+
}
198186

199187
/** Enter staging level of symbol defined by `tree`, if applicable. */
200188
def markDef(tree: Tree)(implicit ctx: Context) = tree match {
@@ -501,18 +489,11 @@ class ReifyQuotes extends MacroTransformWithImplicits {
501489
case Quoted(quotedTree) =>
502490
quotation(quotedTree, tree)
503491
case tree: TypeTree if tree.tpe.typeSymbol.isSplice =>
504-
val splicedType = tree.tpe.asInstanceOf[TypeRef].prefix.termSymbol
505-
val ret = splice(ref(splicedType).select(tpnme.UNARY_~))
506-
// println()
507-
// println("TypeTree: " + tree)
508-
// println("TypeTrees: " + tree.show)
509-
// println("TypeTree ret: " + ret)
510-
// println("TypeTree rets: " + ret.show)
511-
ret
492+
val splicedType = tree.tpe.asInstanceOf[TypeRef].prefix.termSymbol
493+
splice(ref(splicedType).select(tpnme.UNARY_~))
512494
case tree: TypeApply =>
513495
super.transform(tree)
514496
case tree: Select if tree.symbol.isSplice =>
515-
// println("Select: " + tree)
516497
splice(tree)
517498
case tree: RefTree if needsLifting(tree) =>
518499
val lift = lifters(tree.symbol)

tests/run-with-compiler/i3847-b.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
dotty.runtime.Arrays.newGenericArray[Int](3)(reflect.ClassTag.Int)
3-
}
2+
new Array[List[Int]](1)
3+
}

tests/run-with-compiler/i3847-b.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import scala.quoted._
33
import scala.reflect.ClassTag
44

55
object Arrays {
6-
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[List[T]]] = (arr: Array[List[T]]) => '{
7-
new Array[List[~t]](3)
6+
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T]): Liftable[Array[List[T]]] = (arr: Array[List[T]]) => '{
7+
new Array[List[~t]](~(arr.length: Expr[Int]))
88
// TODO add elements
99
}
1010
}

tests/run-with-compiler/i3847.check

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
{
2-
dotty.runtime.Arrays.newGenericArray[Int](3)(reflect.ClassTag.Int)
3-
}
1+
dotty.runtime.Arrays.newGenericArray[Int](3)(reflect.ClassTag.Int)

0 commit comments

Comments
 (0)