Skip to content

Commit 0967522

Browse files
Merge pull request #13733 from dotty-staging/fix-#13557
Add missing position to closure tree
2 parents 583a5e5 + 29ef620 commit 0967522

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
794794
object Lambda extends LambdaModule:
795795
def apply(owner: Symbol, tpe: MethodType, rhsFn: (Symbol, List[Tree]) => Tree): Block =
796796
val meth = dotc.core.Symbols.newSymbol(owner, nme.ANON_FUN, Synthetic | Method, tpe)
797-
tpd.Closure(meth, tss => xCheckMacroedOwners(xCheckMacroValidExpr(rhsFn(meth, tss.head.map(withDefaultPos))), meth))
797+
withDefaultPos(tpd.Closure(meth, tss => xCheckMacroedOwners(xCheckMacroValidExpr(rhsFn(meth, tss.head.map(withDefaultPos))), meth)))
798798

799799
def unapply(tree: Block): Option[(List[ValDef], Term)] = tree match {
800800
case Block((ddef @ DefDef(_, tpd.ValDefs(params) :: Nil, _, Some(body))) :: Nil, Closure(meth, _))

tests/pos-macros/i13557/Macro_1.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package x
2+
3+
import scala.quoted.*
4+
5+
def fun(x:Int): Int = ???
6+
7+
transparent inline def in1[T](inline expr: Int => Int): Int => Int =
8+
${
9+
M.transformLambdaImpl('expr)
10+
}
11+
12+
object M:
13+
14+
def transformLambdaImpl(cexpr: Expr[Int => Int])(using Quotes): Expr[Int => Int] =
15+
import quotes.reflect.*
16+
17+
def extractLambda(f:Term): (ValDef, Term, Term => Term ) =
18+
f match
19+
case Inlined(call, bindings, body) =>
20+
val inner = extractLambda(body)
21+
(inner._1, inner._2, t => Inlined(call, bindings, t) )
22+
case Lambda(params,body) =>
23+
params match
24+
case List(vd) => (vd, body, identity)
25+
case _ => report.throwError(s"lambda with one argument expected, we have ${params}",cexpr)
26+
case Block(Nil,nested@Lambda(params,body)) => extractLambda(nested)
27+
case _ =>
28+
report.throwError(s"lambda expected, have: ${f}", cexpr)
29+
30+
val (oldValDef, body, inlineBack) = extractLambda(cexpr.asTerm)
31+
val mt = MethodType(List(oldValDef.name))( _ => List(oldValDef.tpt.tpe), _ => TypeRepr.of[Int])
32+
val nLambda = Lambda(Symbol.spliceOwner, mt, (owner, params) => {
33+
val argTransformer = new TreeMap() {
34+
override def transformTerm(tree: Term)(owner: Symbol): Term =
35+
tree match
36+
case Ident(name) if (tree.symbol == oldValDef.symbol) => Ref(params.head.symbol)
37+
case _ => super.transformTerm(tree)(owner)
38+
}
39+
argTransformer.transformTerm('{ fun(${body.asExprOf[Int]}) }.asTerm )(owner)
40+
})
41+
inlineBack(nLambda).asExprOf[Int => Int]

tests/pos-macros/i13557/Test_2.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package x
2+
3+
object Main:
4+
5+
def testSimpleContext(): Unit =
6+
var x = 0
7+
val c = in1{ scope =>
8+
1
9+
}

0 commit comments

Comments
 (0)