Skip to content

Fix #7142: Detect escaped variables across macro expansion #8205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object Splicer {
case Quoted(quotedTree) => quotedTree
case _ =>
val interpreter = new Interpreter(pos, classLoader)
val macroOwner = ctx.newSymbol(ctx.owner, NameKinds.UniqueName.fresh(nme.MACROkw), Synthetic, defn.AnyType, coord = tree.span)
val macroOwner = ctx.newSymbol(ctx.owner, nme.MACROkw, Macro | Synthetic, defn.AnyType, coord = tree.span)
try {
given Context = ctx.withOwner(macroOwner)
// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
Expand Down Expand Up @@ -100,7 +100,10 @@ object Splicer {
traverseChildren(tree)
private def isEscapedVariable(sym: Symbol)(given ctx: Context): Boolean =
sym.exists && !sym.is(Package)
&& sym.owner.ownersIterator.contains(expansionOwner) // symbol was generated within the macro expansion
&& sym.owner.ownersIterator.exists(x =>
x == expansionOwner || // symbol was generated within this macro expansion
x.is(Macro, butNot = Method) && x.name == nme.MACROkw // symbol was generated within another macro expansion
)
&& !locals.contains(sym) // symbol is not in current scope
}.traverse(tree)
tree
Expand Down
11 changes: 11 additions & 0 deletions tests/neg-macros/i7142h/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package macros
import scala.quoted._

var saved = Option.empty[Expr[Int]]

def oops(given QuoteContext) = {
if (saved.isEmpty) '{ (x: Int) => ${ saved = Some('{x}); 'x } }
else saved.get
}

inline def test = ${oops}
4 changes: 4 additions & 0 deletions tests/neg-macros/i7142h/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Test {
macros.test
macros.test // error
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
foo
ValDef("macro$1", Inferred(), None)
ValDef("macro", Inferred(), None)

bar
DefDef("foo", Nil, Nil, Inferred(), None)
Expand All @@ -8,7 +8,7 @@ bar2
DefDef("foo", Nil, Nil, Inferred(), None)

foo2
ValDef("macro$1", Inferred(), None)
ValDef("macro", Inferred(), None)

baz
ValDef("foo2", Inferred(), None)
Expand Down
12 changes: 6 additions & 6 deletions tests/run-macros/tasty-location.check
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
foo Location(List(Test$, loc1, macro$1))
foo Location(List(Test$, main, macro$2))
foo Location(List(Test$, main, macro$3))
foo Location(List(Test$, main, bar, macro$4))
foo Location(List(Test$, main, bar, baz, macro$5))
foo Location(List(Test$, main, f, $anonfun, macro$6))
foo Location(List(Test$, loc1, macro))
foo Location(List(Test$, main, macro))
foo Location(List(Test$, main, macro))
foo Location(List(Test$, main, bar, macro))
foo Location(List(Test$, main, bar, baz, macro))
foo Location(List(Test$, main, f, $anonfun, macro))