Skip to content

Commit 3bb53d0

Browse files
committed
Fix placement of Inlined cancelation
1 parent 0f8ff8d commit 3bb53d0

File tree

6 files changed

+45
-5
lines changed

6 files changed

+45
-5
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
244244
// be duplicated
245245
// 2. To enable correct pickling (calls can share symbols with the inlined code, which
246246
// would trigger an assertion when pickling).
247+
//
248+
// This is the same trace that is inserted in ReifyQuotes.quotation
247249
val callTrace = Ident(call.symbol.topLevelClass.typeRef).withPos(call.pos)
248250
cpy.Inlined(tree)(callTrace, transformSub(bindings), transform(expansion)(inlineContext(call)))
249251
case tree: Template =>

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ class ReifyQuotes extends MacroTransformWithImplicits {
381381
// - the call's position
382382
// in the call field of an Inlined node.
383383
// The trace has enough info to completely reconstruct positions.
384-
val callTrace = Ident(ctx.owner.topLevelClass.typeRef).withPos(body1.pos)
384+
//
385+
// This is the same trace that is inserted in PostTyper.transform
386+
val callTrace = Ident(ctx.owner.topLevelClass.typeRef).withPos(quote.pos)
385387
Inlined(callTrace, Nil, body1)
386388
}
387389
pickledQuote(body2, splices, body.tpe, isType).withPos(quote.pos)
@@ -435,7 +437,8 @@ class ReifyQuotes extends MacroTransformWithImplicits {
435437
}
436438
else if (level == 1) {
437439
val (body1, quotes) = nested(isQuote = false).split(splice.qualifier)
438-
makeHole(body1, quotes, splice.tpe).withPos(splice.pos)
440+
val hole = makeHole(body1, quotes, splice.tpe).withPos(splice.pos)
441+
if (splice.isType) hole else Inlined(EmptyTree, Nil, hole)
439442
}
440443
else if (enclosingInlineds.nonEmpty) { // level 0 in an inlined call
441444
val spliceCtx = ctx.outer // drop the last `inlineContext`

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ object Splicer {
4141
try {
4242
// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
4343
val interpretedExpr = interpreter.interpret[scala.quoted.Expr[Any]](tree)
44-
val interpretedTree = interpretedExpr.fold(tree)(x => PickledQuotes.quotedExprToTree(x))
45-
// Cancel the current inline trace. The interpreted tree will start with it's own trace.
46-
Inlined(EmptyTree, Nil, interpretedTree)
44+
interpretedExpr.fold(tree)(x => PickledQuotes.quotedExprToTree(x))
4745
}
4846
catch {
4947
case ex: scala.quoted.QuoteError =>

tests/run/i4947f.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
assertImpl: Test$.main(Test_2.scala:7)
2+
true
3+
assertImpl: Test$.main(Test_2.scala:8)
4+
false
5+
assertImpl: Test$.main(Test_2.scala:9)
6+
hi: Test$.main(Test_2.scala:10)
7+
hi again: Test$.main(Test_2.scala:11)
8+
false

tests/run/i4947f/Macro_1.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.quoted._
2+
3+
object Macros {
4+
def printStack(tag: String): Unit = {
5+
println(tag + ": "+ new Exception().getStackTrace().apply(1))
6+
}
7+
def assertImpl(expr: Expr[Boolean]) = '{
8+
printStack("assertImpl")
9+
println(~expr)
10+
}
11+
12+
rewrite def assert2(expr: => Boolean): Unit = ~Macros.assertImpl('(expr))
13+
14+
}

tests/run/i4947f/Test_2.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Test {
2+
3+
import Macros._
4+
5+
def main(args: Array[String]): Unit = {
6+
val x = 1
7+
assert2(x != 0)
8+
assert2(x == 0)
9+
assert2 {
10+
Macros.printStack("hi")
11+
Macros.printStack("hi again")
12+
x == 0
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)