Skip to content

Commit 02ee000

Browse files
Merge pull request #4856 from dotty-staging/fix-#4846
Fix #4846: Allow transparent parameters at any stage
2 parents 02096b7 + 817cc9b commit 02ee000

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
@@ -237,7 +237,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
237237
def levelOK(sym: Symbol)(implicit ctx: Context): Boolean = levelOf.get(sym) match {
238238
case Some(l) =>
239239
l == level ||
240-
l == 0 && level == -1 && isStageNegOneValue(sym)
240+
level == -1 && sym == defn.TastyTopLevelSplice_tastyContext
241241
case None =>
242242
!sym.is(Param) || levelOK(sym.owner)
243243
}
@@ -371,18 +371,12 @@ class ReifyQuotes extends MacroTransformWithImplicits {
371371
}
372372
else body match {
373373
case body: RefTree if isCaptured(body.symbol, level + 1) =>
374-
if (isStageNegOneValue(body.symbol)) {
375-
// Optimization: avoid the full conversion when capturing inlined `x`
376-
// in '{ x } to '{ x$1.toExpr.unary_~ } and go directly to `x$1.toExpr`
377-
liftInlineParamValue(capturers(body.symbol)(body))
378-
} else {
379-
// Optimization: avoid the full conversion when capturing `x`
380-
// in '{ x } to '{ x$1.unary_~ } and go directly to `x$1`
381-
capturers(body.symbol)(body)
382-
}
374+
// Optimization: avoid the full conversion when capturing `x`
375+
// in '{ x } to '{ x$1.unary_~ } and go directly to `x$1`
376+
capturers(body.symbol)(body)
383377
case _=>
384378
val (body1, splices) = nested(isQuote = true).split(body)
385-
if (level >= 0) pickledQuote(body1, splices, body.tpe, isType).withPos(quote.pos)
379+
if (level == 0 && !ctx.inTransparentMethod) pickledQuote(body1, splices, body.tpe, isType).withPos(quote.pos)
386380
else {
387381
// In top-level splice in an transparent def. Keep the tree as it is, it will be transformed at inline site.
388382
body
@@ -476,7 +470,6 @@ class ReifyQuotes extends MacroTransformWithImplicits {
476470
val tpw = tree.tpe.widen
477471
val argTpe =
478472
if (tree.isType) defn.QuotedTypeType.appliedTo(tpw)
479-
else if (isStageNegOneValue(tree.symbol)) tpw
480473
else defn.QuotedExprType.appliedTo(tpw)
481474
val selectArg = arg.select(nme.apply).appliedTo(Literal(Constant(i))).asInstance(argTpe)
482475
val capturedArg = SyntheticValDef(UniqueName.fresh(tree.symbol.name.toTermName).toTermName, selectArg)
@@ -501,21 +494,17 @@ class ReifyQuotes extends MacroTransformWithImplicits {
501494
val captured = mutable.LinkedHashMap.empty[Symbol, Tree]
502495
val captured2 = capturer(captured)
503496

504-
outer.enteredSyms.foreach(sym => capturers.put(sym, captured2))
497+
outer.enteredSyms.foreach(sym => if (!sym.is(Transparent)) capturers.put(sym, captured2))
505498

506499
val tree2 = transform(tree)
507500
capturers --= outer.enteredSyms
508501

509502
seq(captured.result().valuesIterator.toList, tree2)
510503
}
511504

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

520509
/** Transform `tree` and return the resulting tree and all `embedded` quotes
521510
* or splices as a pair, after performing the `addTags` transform.
@@ -551,13 +540,11 @@ class ReifyQuotes extends MacroTransformWithImplicits {
551540
splice(ref(splicedType).select(tpnme.UNARY_~).withPos(tree.pos))
552541
case tree: Select if tree.symbol.isSplice =>
553542
splice(tree)
543+
case tree: RefTree if tree.symbol.is(Transparent) && tree.symbol.is(Param) =>
544+
tree
554545
case tree: RefTree if isCaptured(tree.symbol, level) =>
555-
val capturer = capturers(tree.symbol)
556-
def captureAndSplice(t: Tree) =
557-
splice(t.select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
558-
if (!isStageNegOneValue(tree.symbol)) captureAndSplice(capturer(tree))
559-
else if (level == 0) capturer(tree)
560-
else captureAndSplice(liftInlineParamValue(capturer(tree)))
546+
val t = capturers(tree.symbol).apply(tree)
547+
splice(t.select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
561548
case Block(stats, _) =>
562549
val last = enteredSyms
563550
stats.foreach(markDef)
@@ -601,28 +588,6 @@ class ReifyQuotes extends MacroTransformWithImplicits {
601588
}
602589
}
603590

604-
/** Takes a reference to an transparent parameter `tree` and lifts it to an Expr */
605-
private def liftInlineParamValue(tree: Tree)(implicit ctx: Context): Tree = {
606-
val tpSym = tree.tpe.widenDealias.classSymbol
607-
608-
val lifter =
609-
if (tpSym eq defn.BooleanClass) defn.QuotedLiftable_BooleanIsLiftable
610-
else if (tpSym eq defn.ByteClass) defn.QuotedLiftable_ByteIsLiftable
611-
else if (tpSym eq defn.CharClass) defn.QuotedLiftable_CharIsLiftable
612-
else if (tpSym eq defn.ShortClass) defn.QuotedLiftable_ShortIsLiftable
613-
else if (tpSym eq defn.IntClass) defn.QuotedLiftable_IntIsLiftable
614-
else if (tpSym eq defn.LongClass) defn.QuotedLiftable_LongIsLiftable
615-
else if (tpSym eq defn.FloatClass) defn.QuotedLiftable_FloatIsLiftable
616-
else if (tpSym eq defn.DoubleClass) defn.QuotedLiftable_DoubleIsLiftable
617-
else defn.QuotedLiftable_StringIsLiftable
618-
619-
ref(lifter).select("toExpr".toTermName).appliedTo(tree)
620-
}
621-
622-
private def isStageNegOneValue(sym: Symbol)(implicit ctx: Context): Boolean =
623-
(sym.is(Transparent) && sym.owner.is(Transparent) && !defn.isFunctionType(sym.info)) ||
624-
sym == defn.TastyTopLevelSplice_tastyContext // intrinsic value at stage 0
625-
626591
private def liftList(list: List[Tree], tpe: Type)(implicit ctx: Context): Tree = {
627592
list.foldRight[Tree](ref(defn.NilModule)) { (x, acc) =>
628593
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)