Skip to content

Commit 5e45209

Browse files
committed
Restore symbols from eta-expanded expression
1 parent b67e269 commit 5e45209

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,31 @@ object QuoteMatcher {
206206
// Matches an open term and wraps it into a lambda that provides the free variables
207207
case Apply(TypeApply(Ident(_), List(TypeTree())), SeqLiteral(args, _) :: Nil)
208208
if pattern.symbol.eq(defn.QuotedRuntimePatterns_higherOrderHole) =>
209+
210+
/* Some of method symbols in arguments of higher-order term hole are eta-expanded.
211+
* e.g.
212+
* g: Int -> Int
213+
* => {
214+
* def $anonfun(y: Int): Int = g(y)
215+
* closure($anonfun)
216+
* }
217+
* This function restores the symbol of the original method from
218+
* the eta-expanded function.
219+
*/
220+
def getCapturedIdent(arg: Tree)(using Context): Ident =
221+
arg match
222+
case id: Ident => id
223+
case Block(DefDef(_, _, _, Apply(id: Ident, _))::_, _) => id
224+
case Apply(id: Ident, _) => id
225+
case y => ???
226+
209227
val env = summon[Env]
210-
val capturedArgs = args.map(_.symbol)
211-
val captureEnv = env.filter((k, v) => !capturedArgs.contains(v))
228+
val capturedIds = args.map(getCapturedIdent)
229+
val capturedSymbols = capturedIds.map(_.symbol)
230+
val captureEnv = env.filter((k, v) => !capturedSymbols.contains(v))
212231
withEnv(captureEnv) {
213232
scrutinee match
214-
case ClosedPatternTerm(scrutinee) => matchedOpen(scrutinee, pattern.tpe, args, env)
233+
case ClosedPatternTerm(scrutinee) => matchedOpen(scrutinee, pattern.tpe, capturedIds, env)
215234
case _ => notMatched
216235
}
217236

0 commit comments

Comments
 (0)