Skip to content

Commit 3d39fec

Browse files
committed
Port more of Tree =?= (part 2)
1 parent b370837 commit 3d39fec

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

compiler/src/scala/quoted/runtime/impl/Matcher.scala

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ import dotty.tools.dotc.core.StdNames.nme
2020
*
2121
* Operations:
2222
* - `s =?= p` checks if a scrutinee `s` matches the pattern `p` while accumulating extracted parts of the code.
23-
* - `isColosedUnder(x1, .., xn)('{e})` returns true if and only if all the references in `e` to names defined in the patttern are contained in the set `{x1, ... xn}`.
23+
* - `isClosedUnder(x1, .., xn)('{e})` returns true if and only if all the references in `e` to names defined in the pattern are contained in the set `{x1, ... xn}`.
2424
* - `lift(x1, .., xn)('{e})` returns `(y1, ..., yn) => [xi = $yi]'{e}` where `yi` is an `Expr` of the type of `xi`.
25-
* - `withEnv(x1 -> y1, ..., xn -> yn)(matching)` evaluates mathing recording that `xi` is equivalent to `yi`.
26-
* - `matched` denotes that the the match succedded and `matched('{e})` denotes that a matech succeded and extracts `'{e}`
25+
* - `withEnv(x1 -> y1, ..., xn -> yn)(matching)` evaluates matching recording that `xi` is equivalent to `yi`.
26+
* - `matched` denotes that the the match succeeded and `matched('{e})` denotes that a match succeeded and extracts `'{e}`
2727
* - `&&&` matches if both sides match. Concatenates the extracted expressions of both sides.
2828
*
2929
* Note: that not all quoted terms bellow are valid expressions
3030
*
3131
* ```scala
3232
* /* Term hole */
33-
* '{ e } =?= '{ hole[T] } && typeOf('{e}) <:< T && isColosedUnder()('{e}) ===> matched('{e})
33+
* '{ e } =?= '{ hole[T] } && typeOf('{e}) <:< T && isClosedUnder()('{e}) ===> matched('{e})
3434
*
3535
* /* Higher order term hole */
36-
* '{ e } =?= '{ hole[(T1, ..., Tn) => T](x1, ..., xn) } && isColosedUnder(x1, ... xn)('{e}) ===> matched(lift(x1, ..., xn)('{e}))
36+
* '{ e } =?= '{ hole[(T1, ..., Tn) => T](x1, ..., xn) } && isClosedUnder(x1, ... xn)('{e}) ===> matched(lift(x1, ..., xn)('{e}))
3737
*
3838
* /* Match literal */
3939
* '{ lit } =?= '{ lit } ===> matched
@@ -216,42 +216,6 @@ object Matcher {
216216
val res = Lambda(Symbol.spliceOwner, MethodType(names)(_ => argTypes, _ => resType), (meth, x) => bodyFn(x).changeOwner(meth))
217217
matched(res.asExpr)
218218

219-
//
220-
// Match two equivalent trees
221-
//
222-
223-
/* Match literal */
224-
case (Literal(constant1), Literal(constant2)) if constant1 == constant2 =>
225-
matched
226-
227-
/* Match type ascription (a) */
228-
case (Typed(expr1, _), pattern) =>
229-
expr1 =?= pattern
230-
231-
/* Match type ascription (b) */
232-
case (scrutinee, Typed(expr2, _)) =>
233-
scrutinee =?= expr2
234-
235-
/* Match selection */
236-
case (ref: Ref, Select(qual2, _)) if symbolMatch(scrutinee.asInstanceOf, pattern.asInstanceOf) =>
237-
ref match
238-
case Select(qual1, _) => qual1 =?= qual2
239-
case ref: Ident =>
240-
ref.tpe match
241-
case TermRef(qual: TermRef, _) => Ref.term(qual) =?= qual2
242-
case _ => matched
243-
244-
/* Match reference */
245-
case (_: Ref, _: Ident) if symbolMatch(scrutinee.asInstanceOf, pattern.asInstanceOf) =>
246-
matched
247-
248-
/* Match application */
249-
case (Apply(fn1, args1), Apply(fn2, args2)) =>
250-
fn1 =?= fn2 &&& args1 =?= args2
251-
252-
/* Match type application */
253-
case (TypeApply(fn1, args1), TypeApply(fn2, args2)) =>
254-
fn1 =?= fn2 &&& args1 =?= args2
255219

256220
// No Match
257221
case _ =>
@@ -263,6 +227,7 @@ object Matcher {
263227
def otherCases(scrutinee: tpd.Tree, pattern: tpd.Tree)(using Env): Matching =
264228
import tpd.* // TODO remove
265229
import dotc.core.Flags.* // TODO remove
230+
import dotc.core.Types.* // TODO remove
266231

267232
/** Check that both are `val` or both are `lazy val` or both are `var` **/
268233
def checkValFlags(): Boolean = {
@@ -281,6 +246,43 @@ object Matcher {
281246

282247
(scrutinee, pattern) match
283248

249+
//
250+
// Match two equivalent trees
251+
//
252+
253+
/* Match literal */
254+
case (Literal(constant1), Literal(constant2)) if constant1 == constant2 =>
255+
matched
256+
257+
/* Match type ascription (a) */
258+
case (Typed(expr1, _), pattern) =>
259+
expr1 =?= pattern
260+
261+
/* Match type ascription (b) */
262+
case (scrutinee, Typed(expr2, _)) =>
263+
scrutinee =?= expr2
264+
265+
/* Match selection */
266+
case (ref: RefTree, Select(qual2, _)) if symbolMatch(scrutinee, pattern) =>
267+
ref match
268+
case Select(qual1, _) => qual1 =?= qual2
269+
case ref: Ident =>
270+
ref.tpe match
271+
case TermRef(qual: TermRef, _) => tpd.ref(qual) =?= qual2
272+
case _ => matched
273+
274+
/* Match reference */
275+
case (_: RefTree, _: Ident) if symbolMatch(scrutinee, pattern) =>
276+
matched
277+
278+
/* Match application */
279+
case (Apply(fn1, args1), Apply(fn2, args2)) =>
280+
fn1 =?= fn2 &&& args1 =?= args2
281+
282+
/* Match type application */
283+
case (TypeApply(fn1, args1), TypeApply(fn2, args2)) =>
284+
fn1 =?= fn2 &&& args1 =?= args2
285+
284286
/* Match block */
285287
case (Block(stat1 :: stats1, expr1), Block(stat2 :: stats2, expr2)) =>
286288
val newEnv = (stat1, stat2) match {

0 commit comments

Comments
 (0)