Skip to content

Commit d33bd89

Browse files
committed
Add Expr.matches
1 parent 5948090 commit d33bd89

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

library/src/scala/quoted/Expr.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ package quoted {
1919
*/
2020
final def getValue[U >: T](given qctx: QuoteContext, valueOf: ValueOfExpr[U]): Option[U] = valueOf(this)
2121

22+
/** Pattern matches `this` against `that`. Effectively performing a deep equality check.
23+
* It does the equivalent of
24+
* ```
25+
* this match
26+
* case '{...} => true // where the contens of the pattern are the contents of `that`
27+
* case _ => false
28+
* ```
29+
*/
30+
final def matches(that: Expr[Any])(given qctx: QuoteContext): Boolean =
31+
!scala.internal.quoted.Expr.unapply[Unit, Unit](this)(given that, false, qctx).isEmpty
32+
2233
}
2334

2435
object Expr {

tests/run-macros/flops-rewrite-2/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private class Rewriter(preTransform: List[Transformation[_]] = Nil, postTransfor
6868
val e2 = preTransform.foldLeft(e)((ei, transform) => transform(ei))
6969
val e3 = mapChildren(e2)
7070
val e4 = postTransform.foldLeft(e3)((ei, transform) => transform(ei))
71-
if fixPoint && e4 != e then map(e4)
71+
if fixPoint && !e4.matches(e) then map(e4)
7272
else e4
7373
}
7474

tests/run-macros/flops-rewrite-3/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private class Rewriter private (preTransform: List[Transformation] = Nil, postTr
105105
val e2 = preTransform.foldLeft(e)((ei, transform) => transform(ei))
106106
val e3 = mapChildren(e2)
107107
val e4 = postTransform.foldLeft(e3)((ei, transform) => transform(ei))
108-
if fixPoint && e4 != e then map(e4) else e4
108+
if fixPoint && !e4.matches(e) then map(e4) else e4
109109
}
110110

111111
}

tests/run-macros/flops-rewrite/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private class Rewriter(preTransform: Expr[Any] => Expr[Any], postTransform: Expr
3333
val e2 = checkedTransform(e, preTransform)
3434
val e3 = mapChildren(e2)
3535
val e4 = checkedTransform(e3, postTransform)
36-
if fixPoint && e4 != e then map(e4)
36+
if fixPoint && !e4.matches(e) then map(e4)
3737
else e4
3838
}
3939

tests/run-staging/expr-matches.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.quoted._
2+
import scala.quoted.staging._
3+
4+
5+
object Test {
6+
given Toolbox = Toolbox.make(getClass.getClassLoader)
7+
def main(args: Array[String]): Unit = withQuoteContext {
8+
assert('{1} matches '{1})
9+
assert('{println("foo")} matches '{println("foo")})
10+
assert('{println("foo")} matches '{println(${Expr("foo")})})
11+
assert('{println(Some("foo"))} matches '{println(${ val a = '{Some("foo")}; a})})
12+
}
13+
}

0 commit comments

Comments
 (0)