diff --git a/compiler/src/scala/quoted/runtime/impl/ScopeException.scala b/compiler/src/scala/quoted/runtime/impl/ScopeException.scala index ae16d7434a18..d65328bb5405 100644 --- a/compiler/src/scala/quoted/runtime/impl/ScopeException.scala +++ b/compiler/src/scala/quoted/runtime/impl/ScopeException.scala @@ -33,4 +33,9 @@ object ScopeException: | |Use stack: |${currentScope.stack.mkString("\t", "\n\t", "\n")} + | + |Hint: A common reason for this to happen is when a `def` that creates a `'{...}` + | captures an outer instance of `Quotes`. If this `def` is called in a splice + | it will not track the `Quotes` provided by that particular splice. + | To fix it add a `given Quotes` to this `def`. """.stripMargin) diff --git a/tests/neg-macros/i14137/Macro_1.scala b/tests/neg-macros/i14137/Macro_1.scala new file mode 100644 index 000000000000..3b3d23fa34ee --- /dev/null +++ b/tests/neg-macros/i14137/Macro_1.scala @@ -0,0 +1,23 @@ +package x + +import scala.quoted._ + +object Macro: + + inline def genOp(inline f:Int): Int = ${ + genOpImpl('f) + } + + def genOpImpl(f: Expr[Int])(using Quotes): Expr[Int] = { + + def firstOp(): Expr[Int] = + '{ + var x=1 + ${secondOp('x,f)} + } + + def secondOp(x:Expr[Int], y:Expr[Int]): Expr[Int] = + '{ $x + $y } + + firstOp() + } diff --git a/tests/neg-macros/i14137/Test_2.scala b/tests/neg-macros/i14137/Test_2.scala new file mode 100644 index 000000000000..917e68dc7583 --- /dev/null +++ b/tests/neg-macros/i14137/Test_2.scala @@ -0,0 +1,6 @@ +package x + +object Main: + + def main(args: Array[String]):Unit = + Macro.genOp(10) // error diff --git a/tests/pos-macros/i14137/Macro_1.scala b/tests/pos-macros/i14137/Macro_1.scala new file mode 100644 index 000000000000..e4a6fdc4a7be --- /dev/null +++ b/tests/pos-macros/i14137/Macro_1.scala @@ -0,0 +1,23 @@ +package x + +import scala.quoted._ + +object Macro: + + inline def genOp(inline f:Int): Int = ${ + genOpImpl('f) + } + + def genOpImpl(f: Expr[Int])(using Quotes): Expr[Int] = { + + def firstOp()(using Quotes): Expr[Int] = + '{ + var x=1 + ${secondOp('x,f)} + } + + def secondOp(x:Expr[Int], y:Expr[Int])(using Quotes): Expr[Int] = + '{ $x + $y } + + firstOp() + } diff --git a/tests/pos-macros/i14137/Test_2.scala b/tests/pos-macros/i14137/Test_2.scala new file mode 100644 index 000000000000..4d9c893f5477 --- /dev/null +++ b/tests/pos-macros/i14137/Test_2.scala @@ -0,0 +1,6 @@ +package x + +object Main: + + def main(args: Array[String]):Unit = + Macro.genOp(10)