Skip to content

Commit 50ad597

Browse files
Merge pull request #4139 from dotty-staging/fix-asFunction-owner
Fix owners of AsFunction applied expressions
2 parents 6826684 + 6b1432c commit 50ad597

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dotty.tools.dotc.core.quoted
22

33
import dotty.tools.dotc.ast.Trees._
4-
import dotty.tools.dotc.ast.tpd
4+
import dotty.tools.dotc.ast.{TreeTypeMap, tpd}
55
import dotty.tools.dotc.config.Printers._
66
import dotty.tools.dotc.core.Constants.Constant
77
import dotty.tools.dotc.core.Contexts._
@@ -137,13 +137,12 @@ object PickledQuotes {
137137
def x1Ref() = ref(x1.symbol)
138138
def rec(f: Tree): Tree = f match {
139139
case closureDef(ddef) =>
140-
new TreeMap() {
141-
private val paramSym = ddef.vparamss.head.head.symbol
142-
override def transform(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
143-
case tree: Ident if tree.symbol == paramSym => x1Ref().withPos(tree.pos)
144-
case _ => super.transform(tree)
145-
}
146-
}.transform(ddef.rhs)
140+
val paramSym = ddef.vparamss.head.head.symbol
141+
new TreeTypeMap(
142+
oldOwners = ddef.symbol :: Nil,
143+
newOwners = ctx.owner :: Nil,
144+
treeMap = tree => if (tree.symbol == paramSym) x1Ref().withPos(tree.pos) else tree
145+
).transform(ddef.rhs)
147146
case Block(stats, expr) =>
148147
val applied = rec(expr)
149148
if (stats.isEmpty) applied
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
13
2+
29
3+
61
4+
125
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import scala.quoted._
2+
3+
import dotty.tools.dotc.quoted.Toolbox._
4+
5+
object Test {
6+
7+
def main(args: Array[String]): Unit = {
8+
val ack3 = ackermann(3).run
9+
println(ack3(1))
10+
println(ack3(2))
11+
println(ack3(3))
12+
println(ack3(4))
13+
}
14+
15+
def ackermann(m: Int): Expr[Int => Int] = {
16+
if (m == 0) '{ n => n + 1 }
17+
else '{ n =>
18+
def `ackermann(m-1)`(n: Int): Int = ~ackermann(m - 1)('(n)) // Expr[Int => Int] applied to Expr[Int]
19+
def `ackermann(m)`(n: Int): Int =
20+
if (n == 0) `ackermann(m-1)`(1) else `ackermann(m-1)`(`ackermann(m)`(n - 1))
21+
`ackermann(m)`(n)
22+
}
23+
}
24+
25+
}

0 commit comments

Comments
 (0)