Skip to content

Commit 33ca2cd

Browse files
committed
Move scala.internal.quoted.{ Matcher.unapply => Expr.unapply }
1 parent 39974f5 commit 33ca2cd

File tree

7 files changed

+49
-42
lines changed

7 files changed

+49
-42
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,8 @@ class Definitions {
651651
@tu lazy val InternalQuoted_patternBindHoleAnnot: ClassSymbol = InternalQuotedModule.requiredClass("patternBindHole")
652652
@tu lazy val InternalQuoted_QuoteTypeTagAnnot: ClassSymbol = InternalQuotedModule.requiredClass("quoteTypeTag")
653653

654-
@tu lazy val InternalQuotedMatcherModule: Symbol = ctx.requiredModule("scala.internal.quoted.Matcher")
655-
@tu lazy val InternalQuotedMatcher_unapply: Symbol = InternalQuotedMatcherModule.requiredMethod(nme.unapply)
654+
@tu lazy val InternalQuotedExprModule: Symbol = ctx.requiredModule("scala.internal.quoted.Expr")
655+
@tu lazy val InternalQuotedExpr_unapply: Symbol = InternalQuotedExprModule.requiredMethod(nme.unapply)
656656

657657
@tu lazy val QuotedTypeClass: ClassSymbol = ctx.requiredClass("scala.quoted.Type")
658658
@tu lazy val QuotedType_splice: Symbol = QuotedTypeClass.requiredType(tpnme.splice)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ trait QuotesAndSplices { self: Typer =>
249249
}
250250

251251
/** Type a quote pattern `case '{ <quoted> } =>` qiven the a current prototype. Typing the pattern
252-
* will also transform it into a call to `scala.internal.quoted.Matcher.unapply`.
252+
* will also transform it into a call to `scala.internal.quoted.Expr.unapply`.
253253
*
254254
* Code directly inside the quote is typed as an expression using Mode.QuotedPattern. Splices
255255
* within the quotes become patterns again and typed acordingly.
@@ -276,7 +276,7 @@ trait QuotesAndSplices { self: Typer =>
276276
* and the patterns in the splices. All these are recombined into a call to `Matcher.unapply`.
277277
*
278278
* ```
279-
* case scala.internal.quoted.Matcher.unapply[
279+
* case scala.internal.quoted.Expr.unapply[
280280
* Tuple1[$t @ _], // Type binging definition
281281
* Tuple2[Type[$t], Expr[List[$t]]] // Typing the result of the pattern match
282282
* ](
@@ -347,7 +347,7 @@ trait QuotesAndSplices { self: Typer =>
347347
val splicePat = typed(untpd.Tuple(splices.map(x => untpd.TypedSplice(replaceBindingsInTree.transform(x)))).withSpan(quoted.span), patType)
348348

349349
UnApply(
350-
fun = ref(defn.InternalQuotedMatcher_unapply.termRef).appliedToTypeTrees(typeBindingsTuple :: TypeTree(patType) :: Nil),
350+
fun = ref(defn.InternalQuotedExpr_unapply.termRef).appliedToTypeTrees(typeBindingsTuple :: TypeTree(patType) :: Nil),
351351
implicits =
352352
ref(defn.InternalQuoted_exprQuote.termRef).appliedToType(defn.AnyType).appliedTo(shape).select(nme.apply).appliedTo(qctx) ::
353353
Literal(Constant(typeBindings.nonEmpty)) ::

docs/docs/reference/other-new-features/quoted-pattern-spec.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ def foo(x: Expr[Int]) given tasty.Reflect: Expr[Int] = x match {
1818
```
1919
In the example above we have `$a` which provides a `Bind[Int]`, `$x` which provides an `Expr[Int]` and `${Bind(`a`)}` which probides an `Expr[Int]` that is pattern matched against `Bind(`a`)` to check that it is a reference to `a`.
2020

21-
Quoted patterns are transformed during typer to a call of `scala.internal.quoted.Matcher.unapply` which splits the quoted code into the patterns and a reifiable quote that will be used as witnesses at runtime.
21+
Quoted patterns are transformed during typer to a call of `scala.internal.quoted.Expr.unapply` which splits the quoted code into the patterns and a reifiable quote that will be used as witnesses at runtime.
2222

2323
```scala
2424
def foo(x: Expr[Int]) given tasty.Reflect: Expr[Int] = x match {
25-
case scala.internal.quoted.Matcher.unapply[Tuple3[Bind[Int], Expr[Int], Expr[Int]]](Tuple3(a, x, Bind(`a`), y))('{ @patternBindHole val a: Int = patternHole[Int]; patternHole[Int] + 1 }) =>
25+
case scala.internal.quoted.Expr.unapply[Tuple3[Bind[Int], Expr[Int], Expr[Int]]](Tuple3(a, x, Bind(`a`), y))('{ @patternBindHole val a: Int = patternHole[Int]; patternHole[Int] + 1 }) =>
2626
}
2727
```
2828

2929

3030
## Runtime semantics
3131

32-
At runtime to a `quoted.Expr` can be matched to another using `scala.internal.quoted.Matcher.unapply`.
32+
At runtime to a `quoted.Expr` can be matched to another using `scala.internal.quoted.Expr.unapply`.
3333

3434
```scala
3535
def unapply[Tup <: Tuple](scrutineeExpr: Expr[_])(implicit patternExpr: Expr[_], reflection: Reflection): Option[Tup]
@@ -49,7 +49,7 @@ def matched[T](x: T) = Some(Tuple1(x))
4949
def (x: Matching) && (y: Matching) = if (x == None || y == None) None else Some(x.get ++ y.get)
5050
def fold[T](m: Mattching*) given Env: Matching = m.fold(matched)(_ && _)
5151

52-
// `a =#= b` stands for `a` matches `b`
52+
// `a =#= b` stands for `a` matches `b`
5353
def (scrutinee: Tree) =#= pattern: Tree) given Env: Matching // described by cases in the tables below
5454

5555
def envWith(equiv: (Symbol, Symbol)*) given Env: Env // Adds to the current environment the fact that s1 from the scrutinee is equivalent to s2 in the pattern
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package scala.internal.quoted
2+
3+
import scala.quoted._
4+
5+
object Expr {
6+
7+
/** Pattern matches an the scrutineeExpr against the patternExpr and returns a tuple
8+
* with the matched holes if successful.
9+
*
10+
* Examples:
11+
* - `Matcher.unapply('{ f(0, myInt) })('{ f(0, myInt) }, _)`
12+
* will return `Some(())` (where `()` is a tuple of arity 0)
13+
* - `Matcher.unapply('{ f(0, myInt) })('{ f(patternHole[Int], patternHole[Int]) }, _)`
14+
* will return `Some(Tuple2('{0}, '{ myInt }))`
15+
* - `Matcher.unapply('{ f(0, "abc") })('{ f(0, patternHole[Int]) }, _)`
16+
* will return `None` due to the missmatch of types in the hole
17+
*
18+
* Holes:
19+
* - scala.internal.Quoted.patternHole[T]: hole that matches an expression `x` of type `Expr[U]`
20+
* if `U <:< T` and returns `x` as part of the match.
21+
*
22+
* @param scrutineeExpr `Expr[_]` on which we are pattern matching
23+
* @param patternExpr `Expr[_]` containing the pattern tree
24+
* @param hasTypeSplices `Boolean` notify if the pattern has type splices (if so we use a GADT context)
25+
* @param qctx the current QuoteContext
26+
* @return None if it did not match, `Some(tup)` if it matched where `tup` contains `Expr[Ti]``
27+
*/
28+
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: Expr[_])(implicit patternExpr: Expr[_],
29+
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] = {
30+
import qctx.tasty._
31+
new Matcher.QuoteMatcher[qctx.type].termMatch(scrutineeExpr.unseal, patternExpr.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
32+
}
33+
34+
}

library/src/scala/internal/quoted/Matcher.scala

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,13 @@ import scala.annotation.internal.sharable
55
import scala.quoted._
66
import scala.quoted.matching.Bind
77

8-
object Matcher {
9-
10-
private final val debug = false
11-
12-
/** Pattern matches an the scrutineeExpr against the patternExpr and returns a tuple
13-
* with the matched holes if successful.
14-
*
15-
* Examples:
16-
* - `Matcher.unapply('{ f(0, myInt) })('{ f(0, myInt) }, _)`
17-
* will return `Some(())` (where `()` is a tuple of arity 0)
18-
* - `Matcher.unapply('{ f(0, myInt) })('{ f(patternHole[Int], patternHole[Int]) }, _)`
19-
* will return `Some(Tuple2('{0}, '{ myInt }))`
20-
* - `Matcher.unapply('{ f(0, "abc") })('{ f(0, patternHole[Int]) }, _)`
21-
* will return `None` due to the missmatch of types in the hole
22-
*
23-
* Holes:
24-
* - scala.internal.Quoted.patternHole[T]: hole that matches an expression `x` of type `Expr[U]`
25-
* if `U <:< T` and returns `x` as part of the match.
26-
*
27-
* @param scrutineeExpr `Expr[_]` on which we are pattern matching
28-
* @param patternExpr `Expr[_]` containing the pattern tree
29-
* @param hasTypeSplices `Boolean` notify if the pattern has type splices (if so we use a GADT context)
30-
* @param qctx the current QuoteContext
31-
* @return None if it did not match, `Some(tup)` if it matched where `tup` contains `Expr[Ti]``
32-
*/
33-
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: Expr[_])(implicit patternExpr: Expr[_],
34-
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] = {
35-
import qctx.tasty._
36-
new QuoteMatcher[qctx.type].termMatch(scrutineeExpr.unseal, patternExpr.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
37-
}
8+
private[quoted] object Matcher {
389

39-
private class QuoteMatcher[QCtx <: QuoteContext & Singleton] given (val qctx: QCtx) {
10+
class QuoteMatcher[QCtx <: QuoteContext & Singleton] given (val qctx: QCtx) {
4011
// TODO improve performance
4112

13+
private final val debug = false
14+
4215
import qctx.tasty.{Bind => BindPattern, _}
4316
import Matching._
4417

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macros {
99
private def impl[A, B](a: Expr[A], b: Expr[B]) given (qctx: QuoteContext): Expr[Unit] = {
1010
import qctx.tasty.{Bind => _, _}
1111

12-
val res = scala.internal.quoted.Matcher.unapply[Tuple, Tuple](a)(b, true, qctx).map { tup =>
12+
val res = scala.internal.quoted.Expr.unapply[Tuple, Tuple](a)(b, true, qctx).map { tup =>
1313
tup.toArray.toList.map {
1414
case r: Expr[_] =>
1515
s"Expr(${r.unseal.show})"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import Macros._
33

4-
import scala.internal.quoted.Matcher._
4+
import scala.internal.quoted.Expr._
55

66
import scala.internal.Quoted._
77

0 commit comments

Comments
 (0)