Skip to content

Commit 15cff7c

Browse files
committed
Fix #4493: Use captured macro type parameters refrences
1 parent ecf95c1 commit 15cff7c

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
120120
*
121121
* See `isCaptured`
122122
*/
123-
val capturers = new mutable.HashMap[Symbol, RefTree => Tree]
123+
val capturers = new mutable.HashMap[Symbol, Tree => Tree]
124124
}
125125

126126
/** The main transformer class
@@ -144,6 +144,9 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
144144
/** We are in a `~(...)` context that is not shadowed by a nested `'(...)` */
145145
def inSplice = outer != null && !inQuote
146146

147+
/** We are not in a `~(...)` or a `'(...)` */
148+
def isRoot = outer != null
149+
147150
/** A map from type ref T to expressions of type `quoted.Type[T]`".
148151
* These will be turned into splices using `addTags` and represent type variables
149152
* that can be possibly healed.
@@ -291,6 +294,8 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
291294
else i"${sym.name}.this"
292295
if (!isThis && sym.maybeOwner.isType && !sym.is(Param))
293296
check(sym.owner, sym.owner.thisType, pos)
297+
else if (level == 1 && sym.isType && sym.is(Param) && sym.owner.is(Macro) && outer.isRoot)
298+
importedTags(sym.typeRef) = capturers(sym)(ref(sym))
294299
else if (sym.exists && !sym.isStaticOwner && !levelOK(sym))
295300
for (errMsg <- tryHeal(tp, pos))
296301
ctx.error(em"""access to $symStr from wrong staging level:

tests/pos/i4493-b.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Index[K]
2+
object Index {
3+
inline def succ[K](x: K): Unit = ~{
4+
implicit val t: quoted.Type[K] = '[K]
5+
'(new Index[K])
6+
}
7+
}

tests/pos/i4493-c.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Index[K]
2+
object Index {
3+
inline def succ[K]: Unit = ~{
4+
'(new Index[K])
5+
}
6+
}

tests/pos/i4493.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Index[K]
2+
object Index {
3+
inline def succ[K]: Unit = ~{
4+
implicit val t: quoted.Type[K] = '[K]
5+
'(new Index[K])
6+
}
7+
}

0 commit comments

Comments
 (0)