Skip to content

Check that the quotes represented as trees did not escape #5429

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

Closed
Closed
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
13 changes: 12 additions & 1 deletion compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ object PickledQuotes {
case value: Class[_] => ref(defn.Predef_classOf).appliedToType(classToType(value))
case value => Literal(Constant(value))
}
case expr: TastyTreeExpr[Tree] @unchecked => healOwner(expr.tree)
case expr: TastyTreeExpr[Tree] @unchecked =>
if (contextId != expr.ctxId)
throw new scala.quoted.QuoteError("Scope extrusion!")
healOwner(expr.tree)
case expr: FunctionAppliedTo[_, _] =>
functionAppliedTo(quotedExprToTree(expr.f), quotedExprToTree(expr.x))
}
Expand Down Expand Up @@ -187,4 +190,12 @@ object PickledQuotes {
case _ => tree
}
}

/** Id of the current compilation (macros) or Expr[_] run */
def contextId(implicit ctx: Context): Int = {
def root(ctx: Context): Context =
if (ctx.outer != NoContext) root(ctx.outer) else ctx
root(ctx).hashCode
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ class TreeUnpickler(reader: TastyReader,
val args = until(end)(readTerm())
val splice = splices(idx)
def wrap(arg: Tree) =
if (arg.isTerm) new TastyTreeExpr(arg)
if (arg.isTerm) new TastyTreeExpr(arg, PickledQuotes.contextId)
else new TreeType(arg)
val reifiedArgs = args.map(wrap)
if (isType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with CoreImpl {

def reify[T: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[T] = {
typecheck(ctx)
new scala.quoted.Exprs.TastyTreeExpr(term).asInstanceOf[scala.quoted.Expr[T]]
new scala.quoted.Exprs.TastyTreeExpr(term, PickledQuotes.contextId).asInstanceOf[scala.quoted.Expr[T]]
}

private def typecheck[T: scala.quoted.Type](ctx: Context): Unit = {
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 @@ -95,7 +95,7 @@ object Splicer {
}

protected def interpretQuote(tree: Tree)(implicit env: Env): Object =
new scala.quoted.Exprs.TastyTreeExpr(tree)
new scala.quoted.Exprs.TastyTreeExpr(tree, PickledQuotes.contextId)

protected def interpretTypeQuote(tree: Tree)(implicit env: Env): Object =
new scala.quoted.Types.TreeType(tree)
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object Exprs {
*
* May contain references to code defined outside this TastyTreeExpr instance.
*/
final class TastyTreeExpr[Tree](val tree: Tree) extends quoted.Expr[Any] {
final class TastyTreeExpr[Tree](val tree: Tree, val ctxId: Int) extends quoted.Expr[Any] {
override def toString: String = s"Expr(<tasty tree>)"
}

Expand Down