Skip to content

Commit 8ed2487

Browse files
committed
Encode quote unpickle quotes directly on QuoteContext
1 parent f2d546c commit 8ed2487

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ class Definitions {
795795
@tu lazy val QuotedExprModule: Symbol = QuotedExprClass.companionModule
796796

797797
@tu lazy val QuoteContextClass: ClassSymbol = requiredClass("scala.quoted.QuoteContext")
798+
@tu lazy val QuoteContextInternalClass: ClassSymbol = requiredClass("scala.internal.quoted.QuoteContextInternal")
798799

799800
@tu lazy val LiftableModule: Symbol = requiredModule("scala.quoted.Liftable")
800801
@tu lazy val LiftableModule_BooleanLiftable: Symbol = LiftableModule.requiredMethod("BooleanLiftable")
@@ -837,8 +838,6 @@ class Definitions {
837838
@tu lazy val TastyReflectionClass: ClassSymbol = requiredClass("scala.tasty.Reflection")
838839

839840
@tu lazy val PickledQuote_make: Symbol = requiredMethod("scala.internal.quoted.PickledQuote.make")
840-
@tu lazy val PickledQuote_unpickleExpr: Symbol = requiredMethod("scala.internal.quoted.PickledQuote.unpickleExpr")
841-
@tu lazy val PickledQuote_unpickleType: Symbol = requiredMethod("scala.internal.quoted.PickledQuote.unpickleType")
842841

843842
@tu lazy val EqlClass: ClassSymbol = requiredClass("scala.Eql")
844843
def Eql_eqlAny(using Context): TermSymbol = EqlClass.companionModule.requiredMethod(nme.eqlAny)

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,30 @@ class ReifyQuotes extends MacroTransform {
190190
}
191191
}
192192

193+
/** Encode quote using QuoteContextInternal.{unpickleExpr, unpickleType}
194+
*
195+
* Generate the code
196+
* ```scala
197+
* qctx => qctx.asInstanceOf[QuoteContextInternal].<unpickleExpr|unpickleType>(
198+
* <pickledQuote>
199+
* ).asInstanceOf[scala.quoted.<Expr|Type>[<type>]]
200+
* ```
201+
* this closure is always applied directly to the actual context and the BetaReduce phase removes it.
202+
*/
193203
def pickleAsTasty() = {
194-
val unpickleMeth = if isType then defn.PickledQuote_unpickleType else defn.PickledQuote_unpickleExpr
195204
val pickledQuoteStrings = liftList(PickledQuotes.pickleQuote(body).map(x => Literal(Constant(x))), defn.StringType)
196205
// TODO: generate an instance of PickledSplices directly instead of passing through a List
197206
val splicesList = liftList(splices, defn.FunctionType(1).appliedTo(defn.SeqType.appliedTo(defn.AnyType), defn.AnyType))
198207
val pickledQuote = ref(defn.PickledQuote_make).appliedTo(pickledQuoteStrings, splicesList)
199-
ref(unpickleMeth).appliedToType(originalTp).appliedTo(pickledQuote)
208+
val quoteClass = if isType then defn.QuotedTypeClass else defn.QuotedExprClass
209+
val quotedType = quoteClass.typeRef.appliedTo(originalTp)
210+
val lambdaTpe = MethodType(defn.QuoteContextClass.typeRef :: Nil, quotedType)
211+
def callUnpickle(ts: List[Tree]) = {
212+
val qctx = ts.head.asInstance(defn.QuoteContextInternalClass.typeRef)
213+
val unpickleMethName = if isType then "unpickleType" else "unpickleExpr"
214+
qctx.select(unpickleMethName.toTermName).appliedTo(pickledQuote).asInstance(quotedType)
215+
}
216+
Lambda(lambdaTpe, callUnpickle).withSpan(body.span)
200217
}
201218

202219
/** Encode quote using Reflection.TypeRepr.typeConstructorOf

library/src/scala/internal/quoted/PickledQuote.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ trait PickledQuote:
1616

1717
object PickledQuote:
1818

19-
def unpickleExpr[T](pickledQuote: PickledQuote): QuoteContext ?=> Expr[T] =
20-
qctx.asInstanceOf[QuoteContextInternal].unpickleExpr(pickledQuote).asInstanceOf[Expr[T]]
21-
22-
def unpickleType[T](pickledQuote: PickledQuote): QuoteContext ?=> Type[T] =
23-
qctx.asInstanceOf[QuoteContextInternal].unpickleType(pickledQuote).asInstanceOf[Type[T]]
24-
2519
/** Create an instance of PickledExpr from encoded tasty and sequence of labmdas to fill holes
2620
*
2721
* @param pickled: Bytes of tasty encoded using TastyString.pickle

0 commit comments

Comments
 (0)