From 7c924062375dae4097d64b9f6b22e1427ab53a13 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sun, 5 Nov 2017 12:21:46 -0800 Subject: Fix up owner chains to allow `Applyable#apply()` calls to work within lambdas --- core/src/main/scala/forge/define/Applicative.scala | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'core/src/main/scala') diff --git a/core/src/main/scala/forge/define/Applicative.scala b/core/src/main/scala/forge/define/Applicative.scala index ac7c6de5..23b5f776 100644 --- a/core/src/main/scala/forge/define/Applicative.scala +++ b/core/src/main/scala/forge/define/Applicative.scala @@ -52,7 +52,7 @@ object Applicative { import c.universe._ def rec(t: Tree): Iterator[c.Tree] = Iterator(t) ++ t.children.flatMap(rec(_)) - val bound = collection.mutable.Buffer.empty[(c.Tree, Symbol)] + val bound = collection.mutable.Buffer.empty[(c.Tree, ValDef)] val targetApplySym = typeOf[Applyable[_]].member(TermName("apply")) // Derived from @olafurpg's @@ -73,20 +73,26 @@ object Applicative { ) } val tempName = c.freshName(TermName("tmp")) - val tempSym = c.internal.newTermSymbol(api.currentOwner, tempName) + val tempSym = c.internal.newTermSymbol(c.internal.enclosingOwner, tempName) c.internal.setInfo(tempSym, t.tpe) val tempIdent = Ident(tempSym) c.internal.setType(tempIdent, t.tpe) - bound.append((q"${c.prefix}.underlying($fun)", tempSym)) + c.internal.setFlag(tempSym, (1L << 44).asInstanceOf[c.universe.FlagSet]) + bound.append((q"${c.prefix}.underlying($fun)", c.internal.valDef(tempSym))) tempIdent case (t, api) => api.default(t) } - val (exprs, symbols) = bound.unzip + val (exprs, bindings) = bound.unzip - val bindings = symbols.map(c.internal.valDef(_)) - wrapCached(c)(q"${c.prefix}.zipMap(..$exprs){ (..$bindings) => $transformed }") + val callback = c.typecheck(q"(..$bindings) => $transformed ") + + val res = q"${c.prefix}.zipMap(..$exprs){ $callback }" + + c.internal.changeOwner(transformed, c.internal.enclosingOwner, callback.symbol) + + wrapCached(c)(res) } def wrapCached[M[_], T](c: Context)(t: c.Tree) = { import c.universe._ -- cgit v1.2.3