Skip to content

Commit af1acc1

Browse files
committed
Use simpler desugaring for splices
This allows writing explicitly the context function of a splice. It will make the desugaring simpler to explain as one can write it explicitly. The downside is that each nested context will get a generic "evidence" name which might be visible to the users. Though this is general limitation of context functions that might need a general fix.
1 parent 2c3212b commit af1acc1

File tree

5 files changed

+12
-19
lines changed

5 files changed

+12
-19
lines changed

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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)(ctx1).withSpan(tree.span)
122108
}

tests/neg/i8052.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object Macro2 {
1111
def derived[T: Type](ev: Expr[Mirror.Of[T]])(using qctx: QuoteContext): Expr[TC[T]] = '{
1212
new TC[T] {
1313
def encode(): Unit = $ev match {
14-
case '{ $m: Mirror.ProductOf[T] } => ??? // error
14+
case '{ $m: Mirror.ProductOf[T] } => ??? // error // error
1515
}
1616
}
1717
}
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)