Skip to content

Do not allow running Expr that came as macro arguments #4576

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 2 commits into from
May 24, 2018
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
15 changes: 1 addition & 14 deletions compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@ object PickledQuotes {

/** Pickle the tree of the quoted.Expr */
def pickleExpr(tree: Tree)(implicit ctx: Context): scala.quoted.Expr[Any] = {
// Check that there are no free variables
new TreeTraverser {
private val definedHere = scala.collection.mutable.Set.empty[Symbol]
def traverse(tree: tpd.Tree)(implicit ctx: Context): Unit = tree match {
case tree: Ident if tree.symbol.exists && !definedHere(tree.symbol) =>
throw new scala.quoted.FreeVariableError(tree.name.toString)
case tree: DefTree =>
definedHere += tree.symbol
traverseChildren(tree)
case _ =>
traverseChildren(tree)
}
}.traverse(tree)
val pickled = pickleQuote(tree)
scala.runtime.quoted.Unpickler.unpickleExpr(pickled, Nil)
}
Expand Down Expand Up @@ -64,7 +51,7 @@ object PickledQuotes {
case value: Class[_] => ref(defn.Predef_classOf).appliedToType(classToType(value))
case value => Literal(Constant(value))
}
case expr: TreeExpr[Tree] @unchecked => expr.tree
case expr: TastyTreeExpr[Tree] @unchecked => expr.tree
case expr: FunctionAppliedTo[_, _] =>
functionAppliedTo(quotedExprToTree(expr.f), quotedExprToTree(expr.x))
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import config.Config
import core.quoted.PickledQuotes
import scala.quoted
import scala.quoted.Types.TreeType
import scala.quoted.Exprs.TreeExpr
import scala.quoted.Exprs.TastyTreeExpr

/** Unpickler for typed trees
* @param reader the reader from which to unpickle
Expand Down Expand Up @@ -1150,7 +1150,7 @@ class TreeUnpickler(reader: TastyReader,
val args = until(end)(readTerm())
val splice = splices(idx)
def wrap(arg: Tree) =
if (arg.isTerm) new TreeExpr(arg, PickledQuotes.pickleExpr(arg))
if (arg.isTerm) new TastyTreeExpr(arg)
else new TreeType(arg)
val reifiedArgs = args.map(wrap)
if (isType) {
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/quoted/Toolbox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import dotty.tools.dotc.printing.RefinedPrinter

import scala.quoted.Expr
import scala.runtime.BoxedUnit
import scala.quoted.Exprs.{LiftedExpr, TreeExpr}
import scala.quoted.Exprs.{LiftedExpr, TastyTreeExpr}
import scala.runtime.quoted._

/** Default runners for quoted expressions */
Expand All @@ -24,8 +24,8 @@ object Toolbox {
def run(expr: Expr[T]): T = expr match {
case expr: LiftedExpr[T] =>
expr.value
case expr: TreeExpr[Tree] @unchecked =>
new QuoteDriver().run(expr.pickled, runSettings)
case expr: TastyTreeExpr[Tree] @unchecked =>
throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from an inline macro argument.")
case _ =>
new QuoteDriver().run(expr, runSettings)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ object Splicer {
assert(!tp.hasAnnotation(defn.InlineParamAnnot))
// Replace argument by its binding
val arg1 = bindMap.getOrElse(arg, arg)
new scala.quoted.Exprs.TreeExpr(arg1, PickledQuotes.pickleExpr(arg1))
new scala.quoted.Exprs.TastyTreeExpr(arg1)
}
args1 ::: liftArgs(tp.resType, args.tail)
case tp: PolyType =>
Expand Down
9 changes: 4 additions & 5 deletions library/src/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object Expr {
object Exprs {
/** An Expr backed by a pickled TASTY tree */
final class TastyExpr[T](val tasty: Pickled, val args: Seq[Any]) extends Expr[T] {
override def toString: String = s"Expr(<pickled>)"
override def toString: String = s"Expr(<pickled tasty>)"
}

/** An Expr backed by a lifted value.
Expand All @@ -48,11 +48,10 @@ object Exprs {
* These expressions are used for arguments of inline macros. They contain and actual tree
* from the program that is being expanded by the macro.
*
* May contain references to code defined outside this Expr instance.
* May contain references to code defined outside this TastyTreeExpr instance.
*/
final class TreeExpr[Tree](val tree: Tree, pickle: => Expr[_]) extends quoted.Expr[Any] {
def pickled[T]: Expr[T] = pickle.asInstanceOf[Expr[T]]
override def toString: String = s"Expr(<raw>)"
final class TastyTreeExpr[Tree](val tree: Tree) extends quoted.Expr[Any] {
override def toString: String = s"Expr(<tasty tree>)"
}

/** An Expr representing `'{(~f).apply(~x)}` but it is beta-reduced when the closure is known */
Expand Down
8 changes: 0 additions & 8 deletions library/src/scala/quoted/FreeVariableError.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Macros._
object Test {
def main(args: Array[String]): Unit = {
println(foo(1))
println(foo(1 + 3))
println(foo(1)) // error
println(foo(1 + 3)) // error
val x = 3
println(foo {
println(foo { // error
val x = 5
x
})
Expand Down
3 changes: 0 additions & 3 deletions tests/run/quote-run-in-macro-1.check

This file was deleted.

3 changes: 0 additions & 3 deletions tests/run/quote-run-in-macro-2.check

This file was deleted.

18 changes: 0 additions & 18 deletions tests/run/quote-run-in-macro-2/quoted_1.scala

This file was deleted.

9 changes: 0 additions & 9 deletions tests/run/quote-run-in-macro-2/quoted_2.scala

This file was deleted.