Skip to content

Commit 3a5c390

Browse files
Merge pull request #8743 from dotty-staging/remove-quote-pattern-pattern
Remove quoted pattern patterns
2 parents 8884ec0 + fda12ab commit 3a5c390

File tree

3 files changed

+0
-224
lines changed

3 files changed

+0
-224
lines changed

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

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,6 @@ import scala.quoted._
8787
* /* Match def */
8888
* '{ def x0(x1: T1, ..., xn: Tn): T0 = e1; e2 } =?= '{ def y0(y1: P1, ..., yn: Pn): P0 = p1; p2 } ===> withEnv(x0 -> y0, ..., xn -> yn)('[T0] =?= '[P0] &&& ... &&& '[Tn] =?= '[Pn] &&& '{e1} =?= '{p1} &&& '{e2} =?= '{p2})
8989
*
90-
* /* Match match */
91-
* '{ e0 match { case u1 => e1; ...; case un => en } } =?= '{ p0 match { case q1 => p1; ...; case qn => pn } } ===>
92-
* '{e0} =?= '{p0} &&& ... &&& '{en} =?= '{pn} &&& '⟨u1⟩ =?= '⟨q1⟩ &&& ... &&& '⟨un⟩ =?= '⟨qn⟩
93-
*
94-
* /* Match try */
95-
* '{ try e0 catch { case u1 => e1; ...; case un => en } finally ef } =?= '{ try p0 catch { case q1 => p1; ...; case qn => pn } finally pf } ===> '{e0} =?= '{p0} &&& ... &&& '{en} =?= '{pn} &&& '⟨u1⟩ =?= '⟨q1⟩ &&& ... &&& '⟨un⟩ =?= '⟨qn⟩ &&& '{ef} =?= '{pf}
96-
*
9790
* // Types
9891
*
9992
* /* Match type */
@@ -107,30 +100,6 @@ import scala.quoted._
107100
*
108101
* /* Match annot (b) */
109102
* '[T] =?= '[P @annot] ===> '[T] =?= '[P]
110-
*
111-
* // Patterns
112-
*
113-
* /* Match pattern whildcard */
114-
* '⟨ _ ⟩ =?= '⟨ _ ⟩ ===> matched
115-
*
116-
* /* Match pattern bind */
117-
* '⟨ x @ e ⟩ =?= '⟨ y @ p ⟩ ===> withEnv(x -> y)('⟨e⟩ =?= '⟨p⟩)
118-
*
119-
* /* Match pattern unapply */
120-
* '⟨ e0(e1, ..., en)(using i1, ..., im ) ⟩ =?= '⟨ p0(p1, ..., pn)(using q1, ..., 1m) ⟩ ===> '⟨e0⟩ =?= '⟨p0⟩ &&& ... &&& '⟨en⟩ =?= '⟨pn⟩ &&& '{i1} =?= '{q1} &&& ... &&& '{im} =?= '{qm}
121-
*
122-
* /* Match pattern alternatives */
123-
* '⟨ e1 | ... | en ⟩ =?= '⟨ p1 | ... | pn ⟩ ===> '⟨e1⟩ =?= '⟨p1⟩ &&& ... &&& '⟨en⟩ =?= '⟨pn⟩
124-
*
125-
* /* Match pattern type test */
126-
* '⟨ e: T ⟩ =?= '⟨ p: U ⟩ ===> '⟨e⟩ =?= '⟨p⟩ &&& '[T] =?= [U]
127-
*
128-
* /* Match pattern ref */
129-
* '⟨ `x` ⟩ =?= '⟨ `x` ⟩ ===> matched
130-
*
131-
* /* Match pattern ref splice */
132-
* '⟨ `x` ⟩ =?= '⟨ hole ⟩ ===> matched('{`x`})
133-
*
134103
* ```
135104
*/
136105
private[quoted] object Matcher {
@@ -409,14 +378,6 @@ private[quoted] object Matcher {
409378
// TODO match tpt1 with tpt2?
410379
matched
411380

412-
/* Match match */
413-
case (Match(scru1, cases1), Match(scru2, cases2)) =>
414-
scru1 =?= scru2 &&& matchLists(cases1, cases2)(caseMatches)
415-
416-
/* Match try */
417-
case (Try(body1, cases1, finalizer1), Try(body2, cases2, finalizer2)) =>
418-
body1 =?= body2 &&& matchLists(cases1, cases2)(caseMatches) &&& treeOptMatches(finalizer1, finalizer2)
419-
420381
// Ignore type annotations
421382
// TODO remove this
422383
/* Match annot (a) */
@@ -488,86 +449,6 @@ private[quoted] object Matcher {
488449
}
489450
}
490451

491-
private def caseMatches(scrutinee: CaseDef, pattern: CaseDef)(using Context, Env): Matching = {
492-
val (caseEnv, patternMatch) = patternsMatches(scrutinee.pattern, pattern.pattern)
493-
withEnv(caseEnv) {
494-
patternMatch
495-
&&& treeOptMatches(scrutinee.guard, pattern.guard)
496-
&&& scrutinee.rhs =?= pattern.rhs
497-
}
498-
}
499-
500-
501-
/** Check that the pattern trees match and return the contents from the pattern holes.
502-
* Return a tuple with the new environment containing the bindings defined in this pattern and a matching.
503-
* The matching is None if the pattern trees do not match otherwise return Some of a tuple containing all the contents in the holes.
504-
*
505-
* @param scrutinee The pattern tree beeing matched
506-
* @param pattern The pattern tree that the scrutinee should match. Contains `patternHole` holes.
507-
* @param `summon[Env]` Set of tuples containing pairs of symbols (s, p) where s defines a symbol in `scrutinee` which corresponds to symbol p in `pattern`.
508-
* @return The new environment containing the bindings defined in this pattern tuppled with
509-
* `None` if it did not match or `Some(tup: Tuple)` if it matched where `tup` contains the contents of the holes.
510-
*/
511-
private def patternsMatches(scrutinee: Tree, pattern: Tree)(using Context, Env): (Env, Matching) = (scrutinee, pattern) match {
512-
/* Match pattern ref splice */
513-
case (v1: Term, Unapply(TypeApply(Select(patternHole @ Ident("patternHole"), "unapply"), List(tpt)), Nil, Nil))
514-
if patternHole.symbol.owner == summon[Context].requiredModule("scala.runtime.quoted.Matcher") =>
515-
(summon[Env], matched(v1.seal))
516-
517-
/* Match pattern whildcard */
518-
case (Ident("_"), Ident("_")) =>
519-
(summon[Env], matched)
520-
521-
/* Match pattern bind */
522-
case (Bind(name1, body1), Bind(name2, body2)) =>
523-
val bindEnv = summon[Env] + (scrutinee.symbol -> pattern.symbol)
524-
patternsMatches(body1, body2)(using summon[Context], bindEnv)
525-
526-
/* Match pattern unapply */
527-
case (Unapply(fun1, implicits1, patterns1), Unapply(fun2, implicits2, patterns2)) =>
528-
val (patEnv, patternsMatch) = foldPatterns(patterns1, patterns2)
529-
(patEnv, patternsMatches(fun1, fun2)._2 &&& implicits1 =?= implicits2 &&& patternsMatch)
530-
531-
/* Match pattern alternatives */
532-
case (Alternatives(patterns1), Alternatives(patterns2)) =>
533-
foldPatterns(patterns1, patterns2)
534-
535-
/* Match pattern type test */
536-
case (Typed(Ident("_"), tpt1), Typed(Ident("_"), tpt2)) =>
537-
(summon[Env], tpt1 =?= tpt2)
538-
539-
case (v1: Term, v2: Term) =>
540-
(summon[Env], v1 =?= v2)
541-
542-
case _ =>
543-
if (debug)
544-
println(
545-
s""">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
546-
|Scrutinee
547-
| ${scrutinee.show}
548-
|
549-
|${scrutinee.showExtractors}
550-
|
551-
|did not match pattern
552-
| ${pattern.show}
553-
|
554-
|${pattern.showExtractors}
555-
|
556-
|with environment: ${summon[Env]}
557-
|
558-
|
559-
|""".stripMargin)
560-
(summon[Env], notMatched)
561-
}
562-
563-
private def foldPatterns(patterns1: List[Tree], patterns2: List[Tree])(using Context, Env): (Env, Matching) = {
564-
if (patterns1.size != patterns2.size) (summon[Env], notMatched)
565-
else patterns1.zip(patterns2).foldLeft((summon[Env], matched)) { (acc, x) =>
566-
val (env, res) = patternsMatches(x._1, x._2)(using summon[Context], acc._1)
567-
(env, acc._2 &&& res)
568-
}
569-
}
570-
571452
private def isTypeBinding(tree: Tree): Boolean = tree match {
572453
case tree: TypeDef => hasPatternTypeAnnotation(tree.symbol)
573454
case _ => false
@@ -595,20 +476,6 @@ private[quoted] object Matcher {
595476
case _ => None
596477
}
597478

598-
/** Is this matching the result of a successful match */
599-
def (self: Matching) isMatch: Boolean = self.isDefined
600-
601-
/** Joins the mattchings into a single matching. If any matching is `None` the result is `None`.
602-
* Otherwise the result is `Some` of the concatenation of the tupples.
603-
*/
604-
def foldMatchings(matchings: Matching*): Matching = {
605-
// TODO improve performance
606-
matchings.foldLeft[Matching](Some(())) {
607-
case (Some(acc), Some(holes)) => Some(acc ++ holes)
608-
case (_, _) => None
609-
}
610-
}
611-
612479
}
613480

614481
}

tests/run-macros/quote-matcher-runtime.check

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -634,86 +634,6 @@ Pattern: {
634634
}
635635
Result: Some(List())
636636

637-
Scrutinee: 1 match {
638-
case _ =>
639-
2
640-
}
641-
Pattern: 1 match {
642-
case _ =>
643-
2
644-
}
645-
Result: Some(List())
646-
647-
Scrutinee: 1 match {
648-
case _ =>
649-
2
650-
}
651-
Pattern: scala.internal.quoted.CompileTime.patternHole[scala.Int] match {
652-
case _ =>
653-
scala.internal.quoted.CompileTime.patternHole[scala.Int]
654-
}
655-
Result: Some(List(Expr(1), Expr(2)))
656-
657-
Scrutinee: scala.Predef.??? match {
658-
case scala.None =>
659-
2
660-
}
661-
Pattern: scala.Predef.??? match {
662-
case scala.None =>
663-
2
664-
}
665-
Result: Some(List())
666-
667-
Scrutinee: scala.Predef.??? match {
668-
case scala.Some(1) =>
669-
2
670-
}
671-
Pattern: scala.Predef.??? match {
672-
case scala.Some(1) =>
673-
2
674-
}
675-
Result: Some(List())
676-
677-
Scrutinee: try 1 catch {
678-
case _ =>
679-
2
680-
}
681-
Pattern: try 1 catch {
682-
case _ =>
683-
2
684-
}
685-
Result: Some(List())
686-
687-
Scrutinee: try 1 finally {
688-
2
689-
()
690-
}
691-
Pattern: try 1 finally {
692-
2
693-
()
694-
}
695-
Result: Some(List())
696-
697-
Scrutinee: try 1 catch {
698-
case _ =>
699-
2
700-
}
701-
Pattern: try scala.internal.quoted.CompileTime.patternHole[scala.Int] catch {
702-
case _ =>
703-
scala.internal.quoted.CompileTime.patternHole[scala.Int]
704-
}
705-
Result: Some(List(Expr(1), Expr(2)))
706-
707-
Scrutinee: try 1 finally {
708-
2
709-
()
710-
}
711-
Pattern: try scala.internal.quoted.CompileTime.patternHole[scala.Int] finally {
712-
scala.internal.quoted.CompileTime.patternHole[scala.Int]
713-
()
714-
}
715-
Result: Some(List(Expr(1), Expr(2)))
716-
717637
Scrutinee: scala.List.apply[scala.Int](1, 2, 3).foreach[scala.Unit](((x: scala.Int) => scala.Predef.println(x)))
718638
Pattern: {
719639
@scala.internal.quoted.CompileTime.patternType type T

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,6 @@ object Test {
123123
matches({ def a: Int = a; a + a }, { def a: Int = a; a + a })
124124
matches({ def a: Int = a; a + a }, { def a: Int = patternHole[Int]; a + patternHole[Int] })
125125
matches({ lazy val a: Int = a }, { lazy val b: Int = b })
126-
matches(1 match { case _ => 2 }, 1 match { case _ => 2 })
127-
matches(1 match { case _ => 2 }, patternHole[Int] match { case _ => patternHole[Int] })
128-
matches(??? match { case None => 2 }, ??? match { case None => 2 })
129-
matches(??? match { case Some(1) => 2 }, ??? match { case Some(1) => 2 })
130-
// matches(??? match { case Some(1) => 2 }, ??? match { case Some(patternMatchHole()) => 2 })
131-
// matches(??? match { case Some(n) => 2 }, ??? match { case Some(patternMatchBindHole(n)) => 2 })
132-
// matches(??? match { case Some(n @ Some(m)) => 2 }, ??? match { case Some(patterMatchBindHole(n @ Some(patternMatchBindHole(m)))) => 2 })
133-
matches(try 1 catch { case _ => 2 }, try 1 catch { case _ => 2 })
134-
matches(try 1 finally 2, try 1 finally 2)
135-
matches(try 1 catch { case _ => 2 }, try patternHole[Int] catch { case _ => patternHole[Int] })
136-
matches(try 1 finally 2, try patternHole[Int] finally patternHole[Int])
137126
matches(List(1, 2, 3).foreach(x => println(x)), { @patternType type T; patternHole[List[Int]].foreach[T](patternHole[Int => T]) })
138127
matches(List(1, 2, 3).foreach(x => println(x)), { @patternType type T = Unit; patternHole[List[Int]].foreach[T](patternHole[Int => T]) })
139128
matches(List(1, 2, 3).foreach(x => println(x)), { @patternType type T <: String; patternHole[List[Int]].foreach[T](patternHole[Int => T]) })

0 commit comments

Comments
 (0)