Skip to content

Commit 75bacdb

Browse files
committed
Backwards compatibility support for quotes and splice in reflection API
1 parent 76d0daf commit 75bacdb

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
609609
end extension
610610
end NamedArgMethods
611611

612-
type Apply = tpd.Apply
612+
type Apply = tpd.Apply | tpd.Quote | tpd.Splice
613613

614614
object ApplyTypeTest extends TypeTest[Tree, Apply]:
615615
def unapply(x: Tree): Option[Apply & x.type] = x match
616616
case x: (tpd.Apply & x.type) => Some(x)
617+
case x: (tpd.Quote & x.type) => Some(x) // TODO expose Quote AST in Quotes
618+
case x: (tpd.Splice & x.type) => Some(x) // TODO expose Splice AST in Quotes
617619
case _ => None
618620
end ApplyTypeTest
619621

@@ -630,8 +632,23 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
630632

631633
given ApplyMethods: ApplyMethods with
632634
extension (self: Apply)
633-
def fun: Term = self.fun
634-
def args: List[Term] = self.args
635+
def fun: Term = self match
636+
case self: tpd.Apply => self.fun
637+
case self: tpd.Quote => // TODO expose Quote AST in Quotes
638+
import dotty.tools.dotc.ast.tpd.TreeOps
639+
tpd.ref(dotc.core.Symbols.defn.QuotedRuntime_exprQuote)
640+
.appliedToTypeTree(self.tpt)
641+
.withSpan(self.span)
642+
case self: tpd.Splice => // TODO expose Splice AST in Quotes
643+
import dotty.tools.dotc.ast.tpd.TreeOps
644+
tpd.ref(dotc.core.Symbols.defn.QuotedRuntime_exprSplice)
645+
.appliedToTypeTree(self.tpt)
646+
.withSpan(self.span)
647+
648+
def args: List[Term] = self match
649+
case self: tpd.Apply => self.args
650+
case self: tpd.Quote => List(self.expr) // TODO expose Quote AST in Quotes
651+
case self: tpd.Splice => List(self.expr) // TODO expose Splice AST in Quotes
635652
end extension
636653
end ApplyMethods
637654

tests/run-staging/multi-staging.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
stage1 code: ((q1: scala.quoted.Quotes) ?=> {
22
val x1: scala.Int = 2
3-
scala.quoted.runtime.Expr.quote[scala.Int](1.+(scala.quoted.runtime.Expr.nestedSplice[scala.Int](q1)(((evidence$5: scala.quoted.Quotes) ?=> scala.quoted.Expr.apply[scala.Int](x1)(scala.quoted.ToExpr.IntToExpr[scala.Int])(evidence$5))))).apply(using q1)
3+
scala.quoted.runtime.Expr.quote[scala.Int](1.+(scala.quoted.runtime.Expr.splice[scala.Int](((evidence$5: scala.quoted.Quotes) ?=> scala.quoted.Expr.apply[scala.Int](x1)(scala.quoted.ToExpr.IntToExpr[scala.Int])(evidence$5))))).apply(using q1)
44
})
55
3
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
((q: scala.quoted.Quotes) ?=> {
22
val a: scala.quoted.Expr[scala.Int] = scala.quoted.runtime.Expr.quote[scala.Int](4).apply(using q)
3-
((q2: scala.quoted.Quotes) ?=> ((evidence$3: scala.quoted.Quotes) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.Quotes, scala.quoted.Expr[scala.Int]]].apply(using q2)).apply(using q)
4-
})
3+
((q2: scala.quoted.Quotes) ?=> ((evidence$2: scala.quoted.Quotes) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.Quotes, scala.quoted.Expr[scala.Int]]].apply(using q2))
4+
}.apply(using q))

0 commit comments

Comments
 (0)