Skip to content

Commit c9579c9

Browse files
committed
Move quote pattern matching out of the stdlib
1 parent ca27ceb commit c9579c9

File tree

9 files changed

+111
-151
lines changed

9 files changed

+111
-151
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ object desugar {
922922
def quotedPatternTypeDef(tree: TypeDef)(using Context): TypeDef = {
923923
assert(ctx.mode.is(Mode.QuotedPattern))
924924
if (tree.name.startsWith("$") && !tree.isBackquoted) {
925-
val patternBindHoleAnnot = New(ref(defn.InternalQuotedMatcher_patternTypeAnnot.typeRef)).withSpan(tree.span)
925+
val patternBindHoleAnnot = New(ref(defn.InternalQuotedPatterns_patternTypeAnnot.typeRef)).withSpan(tree.span)
926926
val mods = tree.mods.withAddedAnnotation(patternBindHoleAnnot)
927927
tree.withMods(mods)
928928
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,12 @@ class Definitions {
808808
@tu lazy val InternalQuoted_exprNestedSplice : Symbol = InternalQuotedModule.requiredMethod("exprNestedSplice")
809809
@tu lazy val InternalQuoted_QuoteTypeTagAnnot: ClassSymbol = InternalQuotedModule.requiredClass("quoteTypeTag")
810810

811-
@tu lazy val InternalQuotedMatcher: Symbol = requiredModule("scala.internal.quoted.Matcher")
812-
@tu lazy val InternalQuotedMatcher_patternHole: Symbol = InternalQuotedMatcher.requiredMethod("patternHole")
813-
@tu lazy val InternalQuotedMatcher_patternHigherOrderHole: Symbol = InternalQuotedMatcher.requiredMethod("patternHigherOrderHole")
814-
@tu lazy val InternalQuotedMatcher_higherOrderHole: Symbol = InternalQuotedMatcher.requiredMethod("higherOrderHole")
815-
@tu lazy val InternalQuotedMatcher_patternTypeAnnot: ClassSymbol = InternalQuotedMatcher.requiredClass("patternType")
816-
@tu lazy val InternalQuotedMatcher_fromAboveAnnot: ClassSymbol = InternalQuotedMatcher.requiredClass("fromAbove")
811+
@tu lazy val InternalQuotedPatterns: Symbol = requiredModule("scala.internal.quoted.Patterns")
812+
@tu lazy val InternalQuotedPatterns_patternHole: Symbol = InternalQuotedPatterns.requiredMethod("patternHole")
813+
@tu lazy val InternalQuotedPatterns_patternHigherOrderHole: Symbol = InternalQuotedPatterns.requiredMethod("patternHigherOrderHole")
814+
@tu lazy val InternalQuotedPatterns_higherOrderHole: Symbol = InternalQuotedPatterns.requiredMethod("higherOrderHole")
815+
@tu lazy val InternalQuotedPatterns_patternTypeAnnot: ClassSymbol = InternalQuotedPatterns.requiredClass("patternType")
816+
@tu lazy val InternalQuotedPatterns_fromAboveAnnot: ClassSymbol = InternalQuotedPatterns.requiredClass("fromAbove")
817817

818818
@tu lazy val InternalQuotedExprModule: Symbol = requiredModule("scala.internal.quoted.Expr")
819819
@tu lazy val InternalQuotedExpr_unapply: Symbol = InternalQuotedExprModule.requiredMethod(nme.unapply)

library/src-bootstrapped/scala/internal/quoted/Matcher.scala renamed to compiler/src/dotty/tools/dotc/quoted/Matcher.scala

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package scala.internal.quoted
1+
package dotty.tools.dotc.quoted
22

33
import scala.annotation.internal.sharable
44
import scala.annotation.{Annotation, compileTimeOnly}
@@ -98,33 +98,8 @@ import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
9898
*/
9999
object Matcher {
100100

101-
/** A splice in a quoted pattern is desugared by the compiler into a call to this method */
102-
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternHole`")
103-
def patternHole[T]: T = ???
104-
105-
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternHigherOrderHole`")
106-
/** A higher order splice in a quoted pattern is desugared by the compiler into a call to this method */
107-
def patternHigherOrderHole[U](pat: Any, args: Any*): U = ???
108-
109-
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.higherOrderHole`")
110-
/** A higher order splice in a quoted pattern is desugared by the compiler into a call to this method */
111-
def higherOrderHole[U](args: Any*): U = ???
112-
113-
// TODO remove
114-
/** A splice of a name in a quoted pattern is desugared by wrapping getting this annotation */
115-
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternBindHole`")
116-
class patternBindHole extends Annotation
117-
118-
/** A splice of a name in a quoted pattern is that marks the definition of a type splice */
119-
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternType`")
120-
class patternType extends Annotation
121-
122-
/** A type pattern that must be aproximated from above */
123-
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.fromAbove`")
124-
class fromAbove extends Annotation
125-
126101
class QuoteMatcher[QCtx <: QuoteContext & Singleton](val qctx0: QCtx) {
127-
val qctx = quoteContextWithCompilerInterface(qctx0)
102+
val qctx = qctx0.asInstanceOf[qctx0.type { val tasty: qctx0.tasty.type & scala.internal.tasty.CompilerInterface }]
128103

129104
// TODO improve performance
130105

@@ -203,23 +178,23 @@ object Matcher {
203178

204179
/* Term hole */
205180
// Match a scala.internal.Quoted.patternHole typed as a repeated argument and return the scrutinee tree
206-
case (scrutinee as Typed(s, tpt1), Typed(TypeApply(patternHole, tpt :: Nil), tpt2))
207-
if patternHole.symbol == qctx.tasty.Definitions_InternalQuotedMatcher_patternHole &&
181+
case (scrutinee @ Typed(s, tpt1), Typed(TypeApply(patternHole, tpt :: Nil), tpt2))
182+
if patternHole.symbol == qctx.tasty.Definitions_InternalQuotedPatterns_patternHole &&
208183
s.tpe <:< tpt.tpe &&
209184
tpt2.tpe.derivesFrom(defn.RepeatedParamClass) =>
210185
matched(scrutinee.seal)
211186

212187
/* Term hole */
213188
// Match a scala.internal.Quoted.patternHole and return the scrutinee tree
214189
case (ClosedPatternTerm(scrutinee), TypeApply(patternHole, tpt :: Nil))
215-
if patternHole.symbol == qctx.tasty.Definitions_InternalQuotedMatcher_patternHole &&
190+
if patternHole.symbol == qctx.tasty.Definitions_InternalQuotedPatterns_patternHole &&
216191
scrutinee.tpe <:< tpt.tpe =>
217192
matched(scrutinee.seal)
218193

219194
/* Higher order term hole */
220195
// Matches an open term and wraps it into a lambda that provides the free variables
221-
case (scrutinee, pattern as Apply(TypeApply(Ident("higherOrderHole"), List(Inferred())), Repeated(args, _) :: Nil))
222-
if pattern.symbol == qctx.tasty.Definitions_InternalQuotedMatcher_higherOrderHole =>
196+
case (scrutinee, pattern @ Apply(TypeApply(Ident("higherOrderHole"), List(Inferred())), Repeated(args, _) :: Nil))
197+
if pattern.symbol == qctx.tasty.Definitions_InternalQuotedPatterns_higherOrderHole =>
223198

224199
def bodyFn(lambdaArgs: List[Tree]): Tree = {
225200
val argsMap = args.map(_.symbol).zip(lambdaArgs.asInstanceOf[List[Term]]).toMap

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,7 +2556,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
25562556
def isTypeHoleDef(tree: Tree): Boolean =
25572557
tree match
25582558
case tree: TypeDef =>
2559-
tree.symbol.hasAnnotation(dotc.core.Symbols.defn.InternalQuotedMatcher_patternTypeAnnot)
2559+
tree.symbol.hasAnnotation(dotc.core.Symbols.defn.InternalQuotedPatterns_patternTypeAnnot)
25602560
case _ => false
25612561

25622562
def extractTypeHoles(pat: Term): (Term, List[Symbol]) =
@@ -2581,7 +2581,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
25812581
val qctx1 = dotty.tools.dotc.quoted.QuoteContextImpl()(using ctx1)
25822582
.asInstanceOf[QuoteContext { val tasty: QuoteContextImpl.this.tasty.type }]
25832583

2584-
val matcher = new scala.internal.quoted.Matcher.QuoteMatcher[qctx1.type](qctx1)
2584+
val matcher = new Matcher.QuoteMatcher[qctx1.type](qctx1)
25852585

25862586
val matchings =
25872587
if pat1.isType then matcher.termMatch(scrutinee, pat1)
@@ -2593,15 +2593,15 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
25932593
// After matching and doing all subtype checks, we have to approximate all the type bindings
25942594
// that we have found, seal them in a quoted.Type and add them to the result
25952595
def typeHoleApproximation(sym: Symbol) =
2596-
ctx1.gadt.approximation(sym, !sym.hasAnnotation(dotc.core.Symbols.defn.InternalQuotedMatcher_fromAboveAnnot)).seal
2596+
ctx1.gadt.approximation(sym, !sym.hasAnnotation(dotc.core.Symbols.defn.InternalQuotedPatterns_fromAboveAnnot)).seal
25972597
matchings.map { tup =>
25982598
Tuple.fromIArray(typeHoles.map(typeHoleApproximation).toArray.asInstanceOf[IArray[Object]]) ++ tup
25992599
}
26002600
}
26012601
}
26022602

2603-
def Definitions_InternalQuotedMatcher_patternHole: Symbol = dotc.core.Symbols.defn.InternalQuotedMatcher_patternHole
2604-
def Definitions_InternalQuotedMatcher_higherOrderHole: Symbol = dotc.core.Symbols.defn.InternalQuotedMatcher_higherOrderHole
2603+
def Definitions_InternalQuotedPatterns_patternHole: Symbol = dotc.core.Symbols.defn.InternalQuotedPatterns_patternHole
2604+
def Definitions_InternalQuotedPatterns_higherOrderHole: Symbol = dotc.core.Symbols.defn.InternalQuotedPatterns_higherOrderHole
26052605

26062606
def betaReduce(tree: Term): Option[Term] =
26072607
tree match

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ trait QuotesAndSplices {
139139
report.error("Missing arguments for open pattern", tree.srcPos)
140140
val argTypes = typedArgs.map(_.tpe.widenTermRefExpr)
141141
val typedPat = typedSplice(splice, defn.FunctionOf(argTypes, pt))
142-
ref(defn.InternalQuotedMatcher_patternHigherOrderHole).appliedToType(pt).appliedTo(typedPat, SeqLiteral(typedArgs, TypeTree(defn.AnyType)))
142+
ref(defn.InternalQuotedPatterns_patternHigherOrderHole).appliedToType(pt).appliedTo(typedPat, SeqLiteral(typedArgs, TypeTree(defn.AnyType)))
143143
}
144144

145145
/** Translate ${ t: Type[T] }` into type `t.splice` while tracking the quotation level in the context */
@@ -166,7 +166,7 @@ trait QuotesAndSplices {
166166
case pt: TypeBounds => pt
167167
case _ => TypeBounds.empty
168168
val typeSym = newSymbol(spliceOwner(ctx), name, EmptyFlags, typeSymInfo, NoSymbol, tree.expr.span)
169-
typeSym.addAnnotation(Annotation(New(ref(defn.InternalQuotedMatcher_patternTypeAnnot.typeRef)).withSpan(tree.expr.span)))
169+
typeSym.addAnnotation(Annotation(New(ref(defn.InternalQuotedPatterns_patternTypeAnnot.typeRef)).withSpan(tree.expr.span)))
170170
val pat = typedPattern(tree.expr, defn.QuotedTypeClass.typeRef.appliedTo(typeSym.typeRef))(
171171
using spliceContext.retractMode(Mode.QuotedPattern).withOwner(spliceOwner(ctx)))
172172
pat.select(tpnme.spliceType)
@@ -236,20 +236,20 @@ trait QuotesAndSplices {
236236
val exprTpt = AppliedTypeTree(TypeTree(defn.QuotedExprClass.typeRef), tpt1 :: Nil)
237237
val newSplice = ref(defn.InternalQuoted_exprSplice).appliedToType(tpt1.tpe).appliedTo(Typed(pat, exprTpt))
238238
transform(newSplice)
239-
case Apply(TypeApply(fn, targs), Apply(sp, pat :: Nil) :: args :: Nil) if fn.symbol == defn.InternalQuotedMatcher_patternHigherOrderHole =>
239+
case Apply(TypeApply(fn, targs), Apply(sp, pat :: Nil) :: args :: Nil) if fn.symbol == defn.InternalQuotedPatterns_patternHigherOrderHole =>
240240
args match // TODO support these patterns. Possibly using scala.quoted.util.Var
241241
case SeqLiteral(args, _) =>
242242
for arg <- args; if arg.symbol.is(Mutable) do
243243
report.error("References to `var`s cannot be used in higher-order pattern", arg.srcPos)
244-
try ref(defn.InternalQuotedMatcher_higherOrderHole.termRef).appliedToTypeTrees(targs).appliedTo(args).withSpan(tree.span)
244+
try ref(defn.InternalQuotedPatterns_higherOrderHole.termRef).appliedToTypeTrees(targs).appliedTo(args).withSpan(tree.span)
245245
finally {
246246
val patType = pat.tpe.widen
247247
val patType1 = patType.translateFromRepeated(toArray = false)
248248
val pat1 = if (patType eq patType1) pat else pat.withType(patType1)
249249
patBuf += pat1
250250
}
251251
case Apply(fn, pat :: Nil) if fn.symbol.isExprSplice =>
252-
try ref(defn.InternalQuotedMatcher_patternHole.termRef).appliedToType(tree.tpe).withSpan(tree.span)
252+
try ref(defn.InternalQuotedPatterns_patternHole.termRef).appliedToType(tree.tpe).withSpan(tree.span)
253253
finally {
254254
val patType = pat.tpe.widen
255255
val patType1 = patType.translateFromRepeated(toArray = false)
@@ -265,7 +265,7 @@ trait QuotesAndSplices {
265265
else
266266
tree
267267
case tdef: TypeDef =>
268-
if tdef.symbol.hasAnnotation(defn.InternalQuotedMatcher_patternTypeAnnot) then
268+
if tdef.symbol.hasAnnotation(defn.InternalQuotedPatterns_patternTypeAnnot) then
269269
transformTypeBindingTypeDef(tdef, typePatBuf)
270270
else if tdef.symbol.isClass then
271271
val kind = if tdef.symbol.is(Module) then "objects" else "classes"
@@ -304,7 +304,7 @@ trait QuotesAndSplices {
304304

305305
private def transformTypeBindingTypeDef(tdef: TypeDef, buff: mutable.Builder[Tree, List[Tree]])(using Context): Tree = {
306306
if (variance == -1)
307-
tdef.symbol.addAnnotation(Annotation(New(ref(defn.InternalQuotedMatcher_fromAboveAnnot.typeRef)).withSpan(tdef.span)))
307+
tdef.symbol.addAnnotation(Annotation(New(ref(defn.InternalQuotedPatterns_fromAboveAnnot.typeRef)).withSpan(tdef.span)))
308308
val bindingType = getBinding(tdef.symbol).symbol.typeRef
309309
val bindingTypeTpe = AppliedType(defn.QuotedTypeClass.typeRef, bindingType :: Nil)
310310
val bindName = tdef.name.toString.stripPrefix("$").toTermName

library/src-non-bootstrapped/scala/internal/quoted/Matcher.scala renamed to library/src-bootstrapped/scala/internal/quoted/Patterns.scala

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package scala.internal.quoted
22

3-
import scala.annotation.internal.sharable
43
import scala.annotation.{Annotation, compileTimeOnly}
54

6-
import scala.quoted._
7-
8-
object Matcher {
5+
object Patterns {
96

107
/** A splice in a quoted pattern is desugared by the compiler into a call to this method */
118
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.patternHole`")
@@ -32,16 +29,4 @@ object Matcher {
3229
@compileTimeOnly("Illegal reference to `scala.internal.quoted.CompileTime.fromAbove`")
3330
class fromAbove extends Annotation
3431

35-
class QuoteMatcher[QCtx <: QuoteContext & Singleton](val qctx: QCtx) {
36-
import qctx.tasty._
37-
38-
class SymBinding(val sym: Symbol, val fromAbove: Boolean)
39-
40-
def termMatch(scrutineeTerm: Term, patternTerm: Term): Option[Tuple] =
41-
throw new Exception("Non bootstrapped lib")
42-
43-
def typeTreeMatch(scrutineeTypeTree: TypeTree, patternTypeTree: TypeTree): Option[Tuple] =
44-
throw new Exception("Non bootstrapped lib")
45-
}
46-
4732
}

library/src/scala/internal/tasty/CompilerInterface.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ trait CompilerInterface { self: scala.tasty.Reflection =>
5555
// TYPES
5656
//
5757

58-
/** Symbol of scala.internal.CompileTime.patternHole */
59-
def Definitions_InternalQuotedMatcher_patternHole: Symbol
58+
/** Symbol of scala.internal.quoted.Patterns.patternHole */
59+
def Definitions_InternalQuotedPatterns_patternHole: Symbol
6060

61-
/** Symbol of scala.internal.CompileTime.higherOrderHole */
62-
def Definitions_InternalQuotedMatcher_higherOrderHole: Symbol
61+
/** Symbol of scala.internal.quoted.Patterns.higherOrderHole */
62+
def Definitions_InternalQuotedPatterns_higherOrderHole: Symbol
6363

6464
/** Returns Some with a beta-reduced application or None */
6565
def betaReduce(tree: Term): Option[Term]

0 commit comments

Comments
 (0)