Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix capturing conditions of HOAS quote patterns #14822

Merged
merged 1 commit into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,24 +203,33 @@ object QuoteMatcher {
// Matches an open term and wraps it into a lambda that provides the free variables
case Apply(TypeApply(Ident(_), List(TypeTree())), SeqLiteral(args, _) :: Nil)
if pattern.symbol.eq(defn.QuotedRuntimePatterns_higherOrderHole) =>
val names: List[TermName] = args.map {
case Block(List(DefDef(nme.ANON_FUN, _, _, Apply(Ident(name), _))), _) => name.asTermName
case arg => arg.symbol.name.asTermName
def hoasClosure = {
val names: List[TermName] = args.map {
case Block(List(DefDef(nme.ANON_FUN, _, _, Apply(Ident(name), _))), _) => name.asTermName
case arg => arg.symbol.name.asTermName
}
val argTypes = args.map(x => x.tpe.widenTermRefExpr)
val methTpe = MethodType(names)(_ => argTypes, _ => pattern.tpe)
val meth = newAnonFun(ctx.owner, methTpe)
def bodyFn(lambdaArgss: List[List[Tree]]): Tree = {
val argsMap = args.map(_.symbol).zip(lambdaArgss.head).toMap
val body = new TreeMap {
override def transform(tree: Tree)(using Context): Tree =
tree match
case tree: Ident => summon[Env].get(tree.symbol).flatMap(argsMap.get).getOrElse(tree)
case tree => super.transform(tree)
}.transform(scrutinee)
TreeOps(body).changeNonLocalOwners(meth)
}
Closure(meth, bodyFn)
}
val argTypes = args.map(x => x.tpe.widenTermRefExpr)
val methTpe = MethodType(names)(_ => argTypes, _ => pattern.tpe)
val meth = newAnonFun(ctx.owner, methTpe)
def bodyFn(lambdaArgss: List[List[Tree]]): Tree = {
val argsMap = args.map(_.symbol).zip(lambdaArgss.head).toMap
val body = new TreeMap {
override def transform(tree: Tree)(using Context): Tree =
tree match
case tree: Ident => summon[Env].get(tree.symbol).flatMap(argsMap.get).getOrElse(tree)
case tree => super.transform(tree)
}.transform(scrutinee)
TreeOps(body).changeNonLocalOwners(meth)
val capturedArgs = args.map(_.symbol)
val captureEnv = summon[Env].filter((k, v) => !capturedArgs.contains(v))
withEnv(captureEnv) {
scrutinee match
case ClosedPatternTerm(scrutinee) => matched(hoasClosure)
case _ => notMatched
}
matched(Closure(meth, bodyFn))

/* Match type ascription (b) */
case Typed(expr2, _) =>
Expand Down
7 changes: 7 additions & 0 deletions tests/run-macros/pattern-scope-extrusion.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(42: scala.Int)
((x: scala.Int) => x.+(x)).apply(1)

(42: scala.Int)
((x: scala.Int) => x.+(x)).apply(1)
((y: scala.Int) => y.+(y)).apply(2)
((x: scala.Int, y: scala.Int) => x.+(y)).apply(1, 2)
18 changes: 18 additions & 0 deletions tests/run-macros/pattern-scope-extrusion/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import scala.quoted.*

inline def test1(inline x: Int): String = ${ test1Expr('x) }
inline def test2(inline x: Int): String = ${ test2Expr('x) }

private def test1Expr(x: Expr[Int])(using Quotes) : Expr[String] =
x match
case '{ val x: Int = 1; $z: Int } => Expr(z.show)
case '{ val x: Int = 1; $z(x): Int } => Expr('{$z(1)}.show)
case _ => '{"No match"}

private def test2Expr(x: Expr[Int])(using Quotes) : Expr[String] =
x match
case '{ val x: Int = 1; val y: Int = 2; $z: Int } => Expr(z.show)
case '{ val x: Int = 1; val y: Int = 2; $z(x): Int } => Expr('{$z(1)}.show)
case '{ val x: Int = 1; val y: Int = 2; $z(y): Int } => Expr('{$z(2)}.show)
case '{ val x: Int = 1; val y: Int = 2; $z(x,y): Int } => Expr('{$z(1, 2)}.show)
case _ => '{"No match"}
34 changes: 34 additions & 0 deletions tests/run-macros/pattern-scope-extrusion/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@main def Test =
println(test1 {
val a: Int = 1
42: Int
})
println(test1 {
val a: Int = 1
a + a
})

println()

println(test2 {
val a: Int = 1
val b: Int = 2
42: Int
})
println(test2 {
val a: Int = 1
val b: Int = 2
a + a
})

println(test2 {
val a: Int = 1
val b: Int = 2
b + b
})

println(test2 {
val a: Int = 1
val b: Int = 2
a + b
})