Skip to content

Commit

Permalink
Merge pull request #14203 from dotty-staging/fix-#14137
Browse files Browse the repository at this point in the history
Add hint on -Xcheck-macro scope extrusion failure
  • Loading branch information
anatoliykmetyuk authored Jan 11, 2022
2 parents 9821af0 + fde30ff commit 1292246
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/ScopeException.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
23 changes: 23 additions & 0 deletions tests/neg-macros/i14137/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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()
}
6 changes: 6 additions & 0 deletions tests/neg-macros/i14137/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package x

object Main:

def main(args: Array[String]):Unit =
Macro.genOp(10) // error
23 changes: 23 additions & 0 deletions tests/pos-macros/i14137/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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()
}
6 changes: 6 additions & 0 deletions tests/pos-macros/i14137/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package x

object Main:

def main(args: Array[String]):Unit =
Macro.genOp(10)

0 comments on commit 1292246

Please sign in to comment.