Skip to content

Commit a3affa7

Browse files
committed
Move QuoteContextInternal functionality in scala.quoted.internal
Split `QuoteContextInternal` into `QuoteUnpickler` and `QuoteMatching`. Also make sure that `QuoteContext` must extend `QuoteUnpickler` and `QuoteMatching`
1 parent 57c4cf3 commit a3affa7

File tree

14 files changed

+44
-42
lines changed

14 files changed

+44
-42
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -795,11 +795,14 @@ 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")
799-
@tu lazy val QuoteContextInternal_unpickleExpr: Symbol = QuoteContextInternalClass.requiredMethod("unpickleExpr")
800-
@tu lazy val QuoteContextInternal_unpickleType: Symbol = QuoteContextInternalClass.requiredMethod("unpickleType")
801-
@tu lazy val QuoteContextInternal_ExprMatch: Symbol = QuoteContextInternalClass.requiredMethod("ExprMatch")
802-
@tu lazy val QuoteContextInternal_TypeMatch: Symbol = QuoteContextInternalClass.requiredMethod("TypeMatch")
798+
799+
@tu lazy val QuoteUnpicklerClass: ClassSymbol = requiredClass("scala.quoted.internal.QuoteUnpickler")
800+
@tu lazy val QuoteUnpickler_unpickleExpr: Symbol = QuoteUnpicklerClass.requiredMethod("unpickleExpr")
801+
@tu lazy val QuoteUnpickler_unpickleType: Symbol = QuoteUnpicklerClass.requiredMethod("unpickleType")
802+
803+
@tu lazy val QuoteMatchingClass: ClassSymbol = requiredClass("scala.quoted.internal.QuoteMatching")
804+
@tu lazy val QuoteMatching_ExprMatch: Symbol = QuoteMatchingClass.requiredMethod("ExprMatch")
805+
@tu lazy val QuoteMatching_TypeMatch: Symbol = QuoteMatchingClass.requiredMethod("TypeMatch")
803806

804807
@tu lazy val LiftableModule: Symbol = requiredModule("scala.quoted.Liftable")
805808
@tu lazy val LiftableModule_BooleanLiftable: Symbol = LiftableModule.requiredMethod("BooleanLiftable")

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import dotty.tools.dotc.quoted.QuoteUtils._
1414
import dotty.tools.dotc.core.Decorators._
1515

1616
import scala.quoted.QuoteContext
17+
import scala.quoted.internal.{QuoteUnpickler, QuoteMatching}
1718
import dotty.tools.dotc.quoted.printers.{Extractors, SourceCode, SyntaxHighlight}
1819

1920
import scala.tasty.reflect._
@@ -40,7 +41,7 @@ object QuoteContextImpl {
4041

4142
}
4243

43-
class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.internal.quoted.QuoteContextInternal:
44+
class QuoteContextImpl private (ctx: Context) extends QuoteContext, QuoteUnpickler, QuoteMatching:
4445

4546
object reflect extends scala.tasty.Reflection:
4647

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ class PickleQuotes extends MacroTransform {
193193
}
194194
}
195195

196-
/** Encode quote using QuoteContextInternal.{unpickleExpr, unpickleType}
196+
/** Encode quote using QuoteUnpickler.{unpickleExpr, unpickleType}
197197
*
198198
* Generate the code
199199
* ```scala
200-
* qctx => qctx.asInstanceOf[QuoteContextInternal].<unpickleExpr|unpickleType>[<type>](
200+
* qctx => qctx.asInstanceOf[QuoteUnpickler].<unpickleExpr|unpickleType>[<type>](
201201
* <pickledQuote>,
202202
* <typeHole>,
203203
* <termHole>,
@@ -255,8 +255,8 @@ class PickleQuotes extends MacroTransform {
255255
val quotedType = quoteClass.typeRef.appliedTo(originalTp)
256256
val lambdaTpe = MethodType(defn.QuoteContextClass.typeRef :: Nil, quotedType)
257257
def callUnpickle(ts: List[Tree]) = {
258-
val qctx = ts.head.asInstance(defn.QuoteContextInternalClass.typeRef)
259-
val unpickleMeth = if isType then defn.QuoteContextInternal_unpickleType else defn.QuoteContextInternal_unpickleExpr
258+
val qctx = ts.head.asInstance(defn.QuoteUnpicklerClass.typeRef)
259+
val unpickleMeth = if isType then defn.QuoteUnpickler_unpickleType else defn.QuoteUnpickler_unpickleExpr
260260
qctx.select(unpickleMeth).appliedToType(originalTp).appliedTo(pickledQuoteStrings, typeHoles, termHoles)
261261
}
262262
Lambda(lambdaTpe, callUnpickle).withSpan(body.span)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ trait QuotesAndSplices {
463463
if (tree.quoted.isTerm) ref(defn.InternalQuoted_exprQuote.termRef).appliedToType(defn.AnyType).appliedTo(shape).select(nme.apply).appliedTo(qctx)
464464
else ref(defn.QuotedTypeModule_apply.termRef).appliedToTypeTree(shape).select(nme.apply).appliedTo(qctx)
465465

466-
val matchModule = if tree.quoted.isTerm then defn.QuoteContextInternal_ExprMatch else defn.QuoteContextInternal_TypeMatch
467-
val unapplyFun = qctx.asInstance(defn.QuoteContextInternalClass.typeRef).select(matchModule).select(nme.unapply)
466+
val matchModule = if tree.quoted.isTerm then defn.QuoteMatching_ExprMatch else defn.QuoteMatching_TypeMatch
467+
val unapplyFun = qctx.asInstance(defn.QuoteMatchingClass.typeRef).select(matchModule).select(nme.unapply)
468468

469469
UnApply(
470470
fun = unapplyFun.appliedToTypeTrees(typeBindingsTuple :: TypeTree(patType) :: Nil),

library/src-bootstrapped/scala/quoted/Expr.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class Expr[+T] private[scala] {
1818
* ```
1919
*/
2020
final def matches(that: Expr[Any])(using qctx: QuoteContext): Boolean =
21-
val ExprMatch = qctx.asInstanceOf[scala.internal.quoted.QuoteContextInternal].ExprMatch
21+
val ExprMatch = qctx.asInstanceOf[scala.quoted.internal.QuoteMatching].ExprMatch
2222
ExprMatch.unapply[EmptyTuple, EmptyTuple](this)(using that).nonEmpty
2323

2424
/** Checks is the `quoted.Expr[?]` is valid expression of type `X` */

library/src-non-bootstrapped/scala/quoted/QuoteContext.scala

Lines changed: 0 additions & 11 deletions
This file was deleted.

library/src-bootstrapped/scala/quoted/QuoteContext.scala renamed to library/src/scala/quoted/QuoteContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package scala.quoted
88
*
99
* @param tasty Typed AST API. Usage: `def f(qctx: QuoteContext) = { import qctx.reflect._; ... }`.
1010
*/
11-
trait QuoteContext { self =>
11+
trait QuoteContext { self: internal.QuoteUnpickler & internal.QuoteMatching =>
1212

1313
/** Low-level Typed AST API metaprogramming API.
1414
* This API does not have the static type guarantiees that `Expr` and `Type` provide.

library/src/scala/internal/quoted/QuoteContextInternal.scala renamed to library/src/scala/quoted/internal/QuoteMatching.scala

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
package scala.internal.quoted
1+
package scala.quoted.internal
22

33
import scala.quoted.{QuoteContext, Expr, Type}
44
import scala.tasty.reflect._
55

66
/** Part of the QuoteContext interface that needs to be implemented by the compiler but is not visible to users */
7-
trait QuoteContextInternal { self: QuoteContext =>
8-
9-
/** Unpickle `repr` which represents a pickled `Expr` tree,
10-
* replacing splice nodes with `holes`
11-
*/
12-
def unpickleExpr[T](pickled: String | List[String], typeHole: (Int, Seq[Any]) => Type[?], termHole: (Int, Seq[Any], QuoteContext) => Expr[?]): scala.quoted.Expr[T]
13-
14-
/** Unpickle `repr` which represents a pickled `Type` tree,
15-
* replacing splice nodes with `holes`
16-
*/
17-
def unpickleType[T <: AnyKind](pickled: String | List[String], typeHole: (Int, Seq[Any]) => Type[?], termHole: (Int, Seq[Any], QuoteContext) => Expr[?]): scala.quoted.Type[T]
7+
trait QuoteMatching { self: QuoteContext & QuoteUnpickler =>
188

199
val ExprMatch: ExprMatchModule
2010

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package scala.quoted.internal
2+
3+
import scala.quoted.{QuoteContext, Expr, Type}
4+
import scala.tasty.reflect._
5+
6+
/** Part of the QuoteContext interface that needs to be implemented by the compiler but is not visible to users */
7+
trait QuoteUnpickler { self: QuoteContext & QuoteMatching =>
8+
9+
/** Unpickle `repr` which represents a pickled `Expr` tree,
10+
* replacing splice nodes with `holes`
11+
*/
12+
def unpickleExpr[T](pickled: String | List[String], typeHole: (Int, Seq[Any]) => Type[?], termHole: (Int, Seq[Any], QuoteContext) => Expr[?]): scala.quoted.Expr[T]
13+
14+
/** Unpickle `repr` which represents a pickled `Type` tree,
15+
* replacing splice nodes with `holes`
16+
*/
17+
def unpickleType[T <: AnyKind](pickled: String | List[String], typeHole: (Int, Seq[Any]) => Type[?], termHole: (Int, Seq[Any], QuoteContext) => Expr[?]): scala.quoted.Type[T]
18+
19+
}

tests/run-macros/quote-matcher-runtime/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Macros {
77
private def impl[A, B](a: Expr[A], b: Expr[B])(using qctx: QuoteContext) : Expr[Unit] = {
88
import qctx.reflect._
99

10-
val res = qctx.asInstanceOf[scala.internal.quoted.QuoteContextInternal].ExprMatch.unapply[Tuple, Tuple](a)(using b).map { tup =>
10+
val res = qctx.asInstanceOf[scala.quoted.internal.QuoteMatching].ExprMatch.unapply[Tuple, Tuple](a)(using b).map { tup =>
1111
tup.toArray.toList.map {
1212
case r: Expr[_] =>
1313
s"Expr(${r.show})"

tests/run-macros/quote-type-matcher/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Macros {
77
private def matchesExpr[A, B](using a: Type[A], b: Type[B])(using qctx: QuoteContext) : Expr[Unit] = {
88
import qctx.reflect._
99

10-
val res = qctx.asInstanceOf[scala.internal.quoted.QuoteContextInternal].TypeMatch.unapply[Tuple, Tuple](a)(using b).map { tup =>
10+
val res = qctx.asInstanceOf[scala.quoted.internal.QuoteMatching].TypeMatch.unapply[Tuple, Tuple](a)(using b).map { tup =>
1111
tup.toArray.toList.map {
1212
case r: Type[_] =>
1313
s"Type(${TypeTree.of(using r).show})"

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: ((qctx1: scala.quoted.QuoteContext) ?=> {
22
val x1: scala.Int = 2
3-
scala.internal.quoted.CompileTime.exprQuote[scala.Int](1.+(scala.internal.quoted.CompileTime.exprNestedSplice[scala.Int](qctx1)(((evidence$5: qctx1.Nested) ?=> scala.quoted.Expr.apply[scala.Int](x1)(evidence$5, scala.quoted.Liftable.IntLiftable[scala.Int]))))).apply(using qctx1)
3+
scala.quoted.internal.Expr.quote[scala.Int](1.+(scala.quoted.internal.Expr.nestedSplice[scala.Int](qctx1)(((evidence$5: qctx1.Nested) ?=> scala.quoted.Expr.apply[scala.Int](x1)(evidence$5, scala.quoted.Liftable.IntLiftable[scala.Int]))))).apply(using qctx1)
44
})
55
3
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
((qctx: scala.quoted.QuoteContext) ?=> scala.internal.quoted.CompileTime.exprQuote[scala.Int](3).apply(using qctx))
1+
((qctx: scala.quoted.QuoteContext) ?=> scala.quoted.internal.Expr.quote[scala.Int](3).apply(using qctx))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
((qctx: scala.quoted.QuoteContext) ?=> {
2-
val a: scala.quoted.Expr[scala.Int] = scala.internal.quoted.CompileTime.exprQuote[scala.Int](4).apply(using qctx)
2+
val a: scala.quoted.Expr[scala.Int] = scala.quoted.internal.Expr.quote[scala.Int](4).apply(using qctx)
33
((evidence$2: qctx.Nested) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.QuoteContext, scala.quoted.Expr[scala.Int]]].apply(using qctx)
44
})

0 commit comments

Comments
 (0)