Skip to content

Commit 932d63c

Browse files
committed
wip
1 parent 0a7038d commit 932d63c

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,18 @@ private[quoted] object Matcher {
145145
matched(scrutinee.seal)
146146

147147
// Matches an open term and wraps it into a lambda that provides the free variables
148-
case (scrutinee, pattern @ Apply(Select(TypeApply(Ident("patternHole"), List(Inferred())), "apply"), IdentArgs(args))) =>
148+
case (scrutinee, pattern @ Apply(Select(TypeApply(Ident("patternHole"), List(Inferred())), "apply"), args0 @ IdentArgs(args))) =>
149149
def bodyFn(lambdaArgs: List[Tree]): Tree = {
150150
val argsMap = args.map(_.symbol).zip(lambdaArgs.asInstanceOf[List[Term]]).toMap
151151
new TreeMap {
152152
override def transformTerm(tree: Term)(given ctx: Context): Term =
153-
super.transformTerm(tree) match
153+
tree match
154154
case tree: Ident => summon[Env].get(tree.symbol).flatMap(argsMap.get).getOrElse(tree)
155-
case tree => tree
155+
case tree => super.transformTerm(tree)
156156
}.transformTree(scrutinee)
157157
}
158158
val names = args.map(_.name)
159-
val argTypes = args.map(_.tpe.widenTermRefExpr)
159+
val argTypes = args0.map(x => x.tpe.widenTermRefExpr)
160160
val resType = pattern.tpe
161161
val res = Lambda(MethodType(names)(_ => argTypes, _ => resType), bodyFn)
162162
matched(res.seal)
@@ -183,10 +183,10 @@ private[quoted] object Matcher {
183183
case (_: Ref, _: Ref) if scrutinee.symbol == pattern.symbol =>
184184
matched
185185

186-
case (Apply(fn1, args1), Apply(fn2, args2)) if fn1.symbol == fn2.symbol =>
186+
case (Apply(fn1, args1), Apply(fn2, args2)) if fn1.symbol == fn2.symbol || summon[Env].get(fn1.symbol).contains(fn2.symbol) =>
187187
fn1 =?= fn2 && args1 =?= args2
188188

189-
case (TypeApply(fn1, args1), TypeApply(fn2, args2)) if fn1.symbol == fn2.symbol =>
189+
case (TypeApply(fn1, args1), TypeApply(fn2, args2)) if fn1.symbol == fn2.symbol || summon[Env].get(fn1.symbol).contains(fn2.symbol) =>
190190
fn1 =?= fn2 && args1 =?= args2
191191

192192
case (Block(stats1, expr1), Block(binding :: stats2, expr2)) if isTypeBinding(binding) =>
@@ -292,7 +292,7 @@ private[quoted] object Matcher {
292292
|
293293
|${pattern.showExtractors}
294294
|
295-
|
295+
|with environment: ${summon[Env]}
296296
|
297297
|
298298
|""".stripMargin)
@@ -317,8 +317,11 @@ private[quoted] object Matcher {
317317

318318
private object IdentArgs {
319319
def unapply(args: List[Term])(given Context): Option[List[Ident]] =
320-
args.foldLeft(Option(List.empty[Ident])) {
321-
case (Some(acc), id: Ident) => Some(id :: acc)
320+
args.foldRight(Option(List.empty[Ident])) {
321+
case (id: Ident, Some(acc)) => Some(id :: acc)
322+
case (Block(List(DefDef("$anonfun", Nil, List(params), Inferred(), Some(Apply(id: Ident, args)))), Closure(Ident("$anonfun"), None)), Some(acc))
323+
if params.zip(args).forall(_.symbol == _.symbol) =>
324+
Some(id :: acc)
322325
case _ => None
323326
}
324327
}
@@ -390,7 +393,7 @@ private[quoted] object Matcher {
390393
|
391394
|${pattern.showExtractors}
392395
|
393-
|
396+
|with environment: ${summon[Env]}
394397
|
395398
|
396399
|""".stripMargin)

tests/run-macros/quoted-pattern-open-expr.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ Matched open
44
((y: scala.Int) => y.*(y))
55
Matched open
66
((y: scala.Function1[scala.Int, scala.Int]) => y.apply(3))
7+
Matched open
8+
((g: scala.Function1[scala.Int, scala.Int], x: scala.Int) => 5)
9+
Matched open
10+
((g: scala.Function1[scala.Int, scala.Int], x: scala.Int) => x)

tests/run-macros/quoted-pattern-open-expr/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ private def testExpr(e: Expr[Int])(given QuoteContext): Expr[String] = {
77
case '{ val y: Int = 4; $body } => Expr("Matched closed\n" + body.show)
88
case '{ val y: Int = 4; ($body: Int => Int)(y) } => Expr("Matched open\n" + body.show)
99
case '{ val y: Int => Int = x => x + 1; ($body: (Int => Int) => Int)(y) } => Expr("Matched open\n" + body.show)
10-
case '{ def g(x: Int): Int = ($body: (Int => Int, Int) => Int)(g, x); 5 } => Expr("Matched open\n" + body.show)
10+
case '{ def g(x: Int): Int = ($body: (Int => Int, Int) => Int)(g, x); g(5) } => Expr("Matched open\n" + body.show)
1111
}
1212
}

tests/run-macros/quoted-pattern-open-expr/Test_2.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ object Test {
44
println(test { val x: Int = 4; 6: Int })
55
println(test { val x: Int = 4; x * x })
66
println(test { val f: Int => Int = x => x + 1; f(3) })
7-
println(test { def g(x: Int): Int = x; 5 })
8-
println(test { def g(x: Int): Int = g(4); 5 })
9-
println(test { def g(x: Int): Int = g(x); 5 })
7+
println(test { def f(x: Int): Int = 5; f(5) })
8+
println(test { def f(x: Int): Int = x; f(5) })
9+
// FIXME
10+
// println(test { def f(x: Int): Int = f(4); f(5) })
11+
// println(test { def f(x: Int): Int = f(x); f(5) })
1012

1113
}
1214
}

0 commit comments

Comments
 (0)