Skip to content

Fix owners of AsFunction applied expressions #4139

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

Merged
merged 3 commits into from
Mar 20, 2018
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
15 changes: 7 additions & 8 deletions compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dotty.tools.dotc.core.quoted

import dotty.tools.dotc.ast.Trees._
import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.ast.{TreeTypeMap, tpd}
import dotty.tools.dotc.config.Printers._
import dotty.tools.dotc.core.Constants.Constant
import dotty.tools.dotc.core.Contexts._
Expand Down Expand Up @@ -137,13 +137,12 @@ object PickledQuotes {
def x1Ref() = ref(x1.symbol)
def rec(f: Tree): Tree = f match {
case closureDef(ddef) =>
new TreeMap() {
private val paramSym = ddef.vparamss.head.head.symbol
override def transform(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
case tree: Ident if tree.symbol == paramSym => x1Ref().withPos(tree.pos)
case _ => super.transform(tree)
}
}.transform(ddef.rhs)
val paramSym = ddef.vparamss.head.head.symbol
new TreeTypeMap(
oldOwners = ddef.symbol :: Nil,
newOwners = ctx.owner :: Nil,
treeMap = tree => if (tree.symbol == paramSym) x1Ref().withPos(tree.pos) else tree
).transform(ddef.rhs)
case Block(stats, expr) =>
val applied = rec(expr)
if (stats.isEmpty) applied
Expand Down
4 changes: 4 additions & 0 deletions tests/run-with-compiler/quote-ackermann-1.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
13
29
61
125
25 changes: 25 additions & 0 deletions tests/run-with-compiler/quote-ackermann-1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import scala.quoted._

import dotty.tools.dotc.quoted.Toolbox._

object Test {

def main(args: Array[String]): Unit = {
val ack3 = ackermann(3).run
println(ack3(1))
println(ack3(2))
println(ack3(3))
println(ack3(4))
}

def ackermann(m: Int): Expr[Int => Int] = {
if (m == 0) '{ n => n + 1 }
else '{ n =>
def `ackermann(m-1)`(n: Int): Int = ~ackermann(m - 1)('(n)) // Expr[Int => Int] applied to Expr[Int]
def `ackermann(m)`(n: Int): Int =
if (n == 0) `ackermann(m-1)`(1) else `ackermann(m-1)`(`ackermann(m)`(n - 1))
`ackermann(m)`(n)
}
}

}