Skip to content

Commit 1292246

Browse files
Merge pull request #14203 from dotty-staging/fix-#14137
Add hint on -Xcheck-macro scope extrusion failure
2 parents 9821af0 + fde30ff commit 1292246

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ object ScopeException:
3333
|
3434
|Use stack:
3535
|${currentScope.stack.mkString("\t", "\n\t", "\n")}
36+
|
37+
|Hint: A common reason for this to happen is when a `def` that creates a `'{...}`
38+
| captures an outer instance of `Quotes`. If this `def` is called in a splice
39+
| it will not track the `Quotes` provided by that particular splice.
40+
| To fix it add a `given Quotes` to this `def`.
3641
""".stripMargin)

tests/neg-macros/i14137/Macro_1.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package x
2+
3+
import scala.quoted._
4+
5+
object Macro:
6+
7+
inline def genOp(inline f:Int): Int = ${
8+
genOpImpl('f)
9+
}
10+
11+
def genOpImpl(f: Expr[Int])(using Quotes): Expr[Int] = {
12+
13+
def firstOp(): Expr[Int] =
14+
'{
15+
var x=1
16+
${secondOp('x,f)}
17+
}
18+
19+
def secondOp(x:Expr[Int], y:Expr[Int]): Expr[Int] =
20+
'{ $x + $y }
21+
22+
firstOp()
23+
}

tests/neg-macros/i14137/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package x
2+
3+
object Main:
4+
5+
def main(args: Array[String]):Unit =
6+
Macro.genOp(10) // error

tests/pos-macros/i14137/Macro_1.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package x
2+
3+
import scala.quoted._
4+
5+
object Macro:
6+
7+
inline def genOp(inline f:Int): Int = ${
8+
genOpImpl('f)
9+
}
10+
11+
def genOpImpl(f: Expr[Int])(using Quotes): Expr[Int] = {
12+
13+
def firstOp()(using Quotes): Expr[Int] =
14+
'{
15+
var x=1
16+
${secondOp('x,f)}
17+
}
18+
19+
def secondOp(x:Expr[Int], y:Expr[Int])(using Quotes): Expr[Int] =
20+
'{ $x + $y }
21+
22+
firstOp()
23+
}

tests/pos-macros/i14137/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package x
2+
3+
object Main:
4+
5+
def main(args: Array[String]):Unit =
6+
Macro.genOp(10)

0 commit comments

Comments
 (0)