Skip to content

Commit 2352d90

Browse files
Merge pull request #8660 from dotty-staging/simple-splice-desugaring
Use simpler desugaring for splices
2 parents 33a6f9e + 926905d commit 2352d90

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ trait QuotesAndSplices {
5353
ctx.error(em"Quotes require stable QuoteContext, but found non stable $qctx", qctx.sourcePos)
5454

5555
val tree1 =
56-
if ctx.mode.is(Mode.Pattern) && level == 0 then
56+
if ctx.mode.is(Mode.Pattern) then
5757
typedQuotePattern(tree, pt, qctx)
5858
else if (tree.quoted.isType)
5959
typedTypeApply(untpd.TypeApply(untpd.ref(defn.InternalQuoted_typeQuote.termRef), tree.quoted :: Nil), pt)(using quoteContext)
@@ -72,7 +72,7 @@ trait QuotesAndSplices {
7272
ctx.warning("Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ.", tree.sourcePos)
7373
case _ =>
7474
}
75-
if (ctx.mode.is(Mode.QuotedPattern) && level == 1)
75+
if (ctx.mode.is(Mode.QuotedPattern))
7676
if (isFullyDefined(pt, ForceDegree.all)) {
7777
def spliceOwner(ctx: Context): Symbol =
7878
if (ctx.mode.is(Mode.QuotedPattern)) spliceOwner(ctx.outer) else ctx.owner
@@ -99,24 +99,10 @@ trait QuotesAndSplices {
9999

100100
val (outerQctx, ctx1) = popQuoteContext()
101101

102-
// Explicitly provide the given QuoteContext of the splice.
103-
// * Avoids leaking implementation details of scala.internal.quoted.CompileTime.exprSplice,
104-
// such as exprSplice taking a ?=> function argument
105-
// * Provide meaningful names for QuoteContext synthesized by within `${ ... }`
106-
// * If within a quote, provide a QuoteContext is linked typewise with the outer QuoteContext
107-
val qctxParamName = NameKinds.UniqueName.fresh(s"qctx${if level > 0 then level - 1 else ""}_".toTermName)
108-
val qctxParamTpe = outerQctx match {
109-
case Some(qctxRef) => qctxRef.tpe.select("NestedContext".toTypeName)
110-
case _ => defn.QuoteContextClass.typeRef // splice at level 0 (or lower)
111-
}
112-
val qctxParamTpt = untpd.TypedSplice(TypeTree(qctxParamTpe))
113-
val qctxParam = untpd.makeParameter(qctxParamName, qctxParamTpt, untpd.Modifiers(Given))
114-
val expr = untpd.Function(List(qctxParam), tree.expr).withSpan(tree.span)
115-
116102
val internalSplice =
117103
outerQctx match
118-
case Some(qctxRef) => untpd.Apply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprNestedSplice.termRef), qctxRef), expr)
119-
case _ => untpd.Apply(untpd.ref(defn.InternalQuoted_exprSplice.termRef), expr)
104+
case Some(qctxRef) => untpd.Apply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprNestedSplice.termRef), qctxRef), tree.expr)
105+
case _ => untpd.Apply(untpd.ref(defn.InternalQuoted_exprSplice.termRef), tree.expr)
120106

121107
typedApply(internalSplice, pt)(using ctx1).withSpan(tree.span)
122108
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.quoted._
2+
3+
def f(a: Expr[Int])(using qctx: QuoteContext): Unit =
4+
5+
'{ val x: Int = ${ (using qctx2) => a } }
6+
7+
'{ val x: Int = ${ (using qctx2: qctx.NestedContext) => a } }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
((qctx: scala.quoted.QuoteContext) ?=> {
22
val a: scala.quoted.Expr[scala.Int] = scala.internal.quoted.CompileTime.exprQuote[scala.Int](4).apply(using qctx)
3-
((qctx1_$1: qctx.NestedContext) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.QuoteContext, scala.quoted.Expr[scala.Int]]].apply(using qctx)
3+
((evidence$2: qctx.NestedContext) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.QuoteContext, scala.quoted.Expr[scala.Int]]].apply(using qctx)
44
})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
((qctx: scala.quoted.QuoteContext) ?=> {
22
val a: scala.quoted.Expr[scala.Int] = scala.internal.quoted.CompileTime.exprQuote[scala.Int](4).apply(using qctx)
3-
((qctx2: scala.quoted.QuoteContext) ?=> ((qctx1_$1: qctx2.NestedContext) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.QuoteContext, scala.quoted.Expr[scala.Int]]].apply(using qctx2)).apply(using qctx)
3+
((qctx2: scala.quoted.QuoteContext) ?=> ((evidence$3: qctx2.NestedContext) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.QuoteContext, scala.quoted.Expr[scala.Int]]].apply(using qctx2)).apply(using qctx)
44
})

0 commit comments

Comments
 (0)