Skip to content

Commit d9cf473

Browse files
committed
Fix #4846: Allow transparent parameters at any stage
Stop checking and using capture tranformation for transparent parameters
1 parent 4dbfaa8 commit d9cf473

File tree

2 files changed

+19
-48
lines changed

2 files changed

+19
-48
lines changed

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

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
225225
def levelOK(sym: Symbol)(implicit ctx: Context): Boolean = levelOf.get(sym) match {
226226
case Some(l) =>
227227
l == level ||
228-
l == 0 && level == -1 && isStageNegOneValue(sym)
228+
level == -1 && sym == defn.TastyTopLevelSplice_tastyContext
229229
case None =>
230230
!sym.is(Param) || levelOK(sym.owner)
231231
}
@@ -355,18 +355,12 @@ class ReifyQuotes extends MacroTransformWithImplicits {
355355
}
356356
else body match {
357357
case body: RefTree if isCaptured(body.symbol, level + 1) =>
358-
if (isStageNegOneValue(body.symbol)) {
359-
// Optimization: avoid the full conversion when capturing inlined `x`
360-
// in '{ x } to '{ x$1.toExpr.unary_~ } and go directly to `x$1.toExpr`
361-
liftInlineParamValue(capturers(body.symbol)(body))
362-
} else {
363-
// Optimization: avoid the full conversion when capturing `x`
364-
// in '{ x } to '{ x$1.unary_~ } and go directly to `x$1`
365-
capturers(body.symbol)(body)
366-
}
358+
// Optimization: avoid the full conversion when capturing `x`
359+
// in '{ x } to '{ x$1.unary_~ } and go directly to `x$1`
360+
capturers(body.symbol)(body)
367361
case _=>
368362
val (body1, splices) = nested(isQuote = true).split(body)
369-
if (level >= 0) pickledQuote(body1, splices, body.tpe, isType).withPos(quote.pos)
363+
if (level == 0 && !ctx.owner.ownersIterator.exists(_.isTransparentMethod)) pickledQuote(body1, splices, body.tpe, isType).withPos(quote.pos)
370364
else {
371365
// In top-level splice in an transparent def. Keep the tree as it is, it will be transformed at inline site.
372366
body
@@ -474,7 +468,6 @@ class ReifyQuotes extends MacroTransformWithImplicits {
474468
val tpw = tree.tpe.widen
475469
val argTpe =
476470
if (tree.isType) defn.QuotedTypeType.appliedTo(tpw)
477-
else if (isStageNegOneValue(tree.symbol)) tpw
478471
else defn.QuotedExprType.appliedTo(tpw)
479472
val selectArg = arg.select(nme.apply).appliedTo(Literal(Constant(i))).asInstance(argTpe)
480473
val capturedArg = SyntheticValDef(UniqueName.fresh(tree.symbol.name.toTermName).toTermName, selectArg)
@@ -499,21 +492,17 @@ class ReifyQuotes extends MacroTransformWithImplicits {
499492
val captured = mutable.LinkedHashMap.empty[Symbol, Tree]
500493
val captured2 = capturer(captured)
501494

502-
outer.enteredSyms.foreach(sym => capturers.put(sym, captured2))
495+
outer.enteredSyms.foreach(sym => if (!sym.is(Transparent)) capturers.put(sym, captured2))
503496

504497
val tree2 = transform(tree)
505498
capturers --= outer.enteredSyms
506499

507500
seq(captured.result().valuesIterator.toList, tree2)
508501
}
509502

510-
/** Returns true if this tree will be captured by `makeLambda` */
511-
private def isCaptured(sym: Symbol, level: Int)(implicit ctx: Context): Boolean = {
512-
// Check phase consistency and presence of capturer
513-
( (level == 1 && levelOf.get(sym).contains(1)) ||
514-
(level == 0 && isStageNegOneValue(sym))
515-
) && capturers.contains(sym)
516-
}
503+
/** Returns true if this tree will be captured by `makeLambda`. Checks phase consistency and presence of capturer. */
504+
private def isCaptured(sym: Symbol, level: Int)(implicit ctx: Context): Boolean =
505+
level == 1 && levelOf.get(sym).contains(1) && capturers.contains(sym) //
517506

518507
/** Transform `tree` and return the resulting tree and all `embedded` quotes
519508
* or splices as a pair, after performing the `addTags` transform.
@@ -549,13 +538,11 @@ class ReifyQuotes extends MacroTransformWithImplicits {
549538
splice(ref(splicedType).select(tpnme.UNARY_~).withPos(tree.pos))
550539
case tree: Select if tree.symbol.isSplice =>
551540
splice(tree)
541+
case tree: RefTree if tree.symbol.is(Transparent) && tree.symbol.is(Param) =>
542+
tree
552543
case tree: RefTree if isCaptured(tree.symbol, level) =>
553-
val capturer = capturers(tree.symbol)
554-
def captureAndSplice(t: Tree) =
555-
splice(t.select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
556-
if (!isStageNegOneValue(tree.symbol)) captureAndSplice(capturer(tree))
557-
else if (level == 0) capturer(tree)
558-
else captureAndSplice(liftInlineParamValue(capturer(tree)))
544+
val t = capturers(tree.symbol).apply(tree)
545+
splice(t.select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
559546
case Block(stats, _) =>
560547
val last = enteredSyms
561548
stats.foreach(markDef)
@@ -570,28 +557,6 @@ class ReifyQuotes extends MacroTransformWithImplicits {
570557
}
571558
}
572559

573-
/** Takes a reference to an transparent parameter `tree` and lifts it to an Expr */
574-
private def liftInlineParamValue(tree: Tree)(implicit ctx: Context): Tree = {
575-
val tpSym = tree.tpe.widenDealias.classSymbol
576-
577-
val lifter =
578-
if (tpSym eq defn.BooleanClass) defn.QuotedLiftable_BooleanIsLiftable
579-
else if (tpSym eq defn.ByteClass) defn.QuotedLiftable_ByteIsLiftable
580-
else if (tpSym eq defn.CharClass) defn.QuotedLiftable_CharIsLiftable
581-
else if (tpSym eq defn.ShortClass) defn.QuotedLiftable_ShortIsLiftable
582-
else if (tpSym eq defn.IntClass) defn.QuotedLiftable_IntIsLiftable
583-
else if (tpSym eq defn.LongClass) defn.QuotedLiftable_LongIsLiftable
584-
else if (tpSym eq defn.FloatClass) defn.QuotedLiftable_FloatIsLiftable
585-
else if (tpSym eq defn.DoubleClass) defn.QuotedLiftable_DoubleIsLiftable
586-
else defn.QuotedLiftable_StringIsLiftable
587-
588-
ref(lifter).select("toExpr".toTermName).appliedTo(tree)
589-
}
590-
591-
private def isStageNegOneValue(sym: Symbol)(implicit ctx: Context): Boolean =
592-
(sym.is(Transparent) && sym.owner.is(Transparent) && !defn.isFunctionType(sym.info)) ||
593-
sym == defn.TastyTopLevelSplice_tastyContext // intrinsic value at stage 0
594-
595560
private def liftList(list: List[Tree], tpe: Type)(implicit ctx: Context): Tree = {
596561
list.foldRight[Tree](ref(defn.NilModule)) { (x, acc) =>
597562
acc.select("::".toTermName).appliedToType(tpe).appliedTo(x)

tests/pos/i4846.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.quoted._
2+
3+
object Test {
4+
transparent def foo(transparent x: Int): Int = ~fooImpl(x, '(x), '( '(x) ), '( '( '(x) ) ))
5+
def fooImpl(a: Int, b: Expr[Int], c: Expr[Expr[Int]], d: Expr[Expr[Expr[Int]]]): Expr[Int] = ???
6+
}

0 commit comments

Comments
 (0)