Skip to content

Commit 67aab09

Browse files
committed
Encode QuoteContext hashing directly in hashCode
This hash is used to detect scope extrusions across compiler instances and runs.
1 parent be6bcaf commit 67aab09

File tree

7 files changed

+12
-13
lines changed

7 files changed

+12
-13
lines changed

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
8585
extension [T](tree: Tree)
8686
def asExprOf(using scala.quoted.Type[T])(using QuoteContext): scala.quoted.Expr[T] =
8787
if tree.isExpr then
88-
new scala.internal.quoted.Expr(tree, compilerId).asExprOf[T]
88+
new scala.internal.quoted.Expr(tree, QuoteContextImpl.this.hashCode).asExprOf[T]
8989
else tree match
9090
case TermTypeTest(tree) => throw new Exception("Expected an expression. This is a partially applied Term. Try eta-expanding the term first.")
9191
case _ => throw new Exception("Expected a Term but was: " + tree)
@@ -300,11 +300,11 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
300300
object TermMethodsImpl extends TermMethods:
301301
extension (self: Term):
302302
def seal: scala.quoted.Expr[Any] =
303-
if self.isExpr then new scala.internal.quoted.Expr(self, compilerId)
303+
if self.isExpr then new scala.internal.quoted.Expr(self, QuoteContextImpl.this.hashCode)
304304
else throw new Exception("Cannot seal a partially applied Term. Try eta-expanding the term first.")
305305

306306
def sealOpt: Option[scala.quoted.Expr[Any]] =
307-
if self.isExpr then Some(new scala.internal.quoted.Expr(self, compilerId))
307+
if self.isExpr then Some(new scala.internal.quoted.Expr(self, QuoteContextImpl.this.hashCode))
308308
else None
309309

310310
def tpe: Type = self.tpe
@@ -1581,7 +1581,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
15811581
new SourceCodePrinter[tasty.type](tasty)(syntaxHighlight).showType(self)
15821582

15831583
def seal: scala.quoted.Type[_] =
1584-
new scala.internal.quoted.Type(Inferred(self), compilerId)
1584+
new scala.internal.quoted.Type(Inferred(self), QuoteContextImpl.this.hashCode)
15851585

15861586
def =:=(that: Type): Boolean = self =:= that
15871587
def <:<(that: Type): Boolean = self <:< that
@@ -2633,8 +2633,9 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
26332633
rec(fn, identity)
26342634
}
26352635

2636-
def compilerId: Int = rootContext.outersIterator.toList.last.hashCode()
26372636

26382637
end tasty
26392638

2639+
override def hashCode: Int = QuoteContextImpl.scopeId(using ctx)
2640+
26402641
end QuoteContextImpl

library/src-bootstrapped/scala/internal/quoted/Expr.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
2020
}
2121

2222
def unseal(using qctx: QuoteContext): qctx.tasty.Term =
23-
if (quoteContextWithCompilerInterface(qctx).tasty.compilerId != scopeId)
23+
if (qctx.hashCode != scopeId)
2424
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
2525
tree.asInstanceOf[qctx.tasty.Term]
2626

library/src-bootstrapped/scala/internal/quoted/Type.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quote
1515

1616
/** View this expression `quoted.Type[T]` as a `TypeTree` */
1717
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
18-
if (quoteContextWithCompilerInterface(qctx).tasty.compilerId != scopeId)
18+
if (qctx.hashCode != scopeId)
1919
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
2020
typeTree.asInstanceOf[qctx.tasty.TypeTree]
2121

library/src-bootstrapped/scala/internal/quoted/Unpickler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ object Unpickler {
1515
def unpickleExpr[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Expr[T] =
1616
val qctx = quoteContextWithCompilerInterface(summon[QuoteContext])
1717
val tree = qctx.tasty.unpickleExpr(repr, args)
18-
new scala.internal.quoted.Expr(tree, qctx.tasty.compilerId).asInstanceOf[Expr[T]]
18+
new scala.internal.quoted.Expr(tree, qctx.hashCode).asInstanceOf[Expr[T]]
1919

2020
/** Unpickle `repr` which represents a pickled `Type` tree,
2121
* replacing splice nodes with `args`
2222
*/
2323
def unpickleType[T](repr: PickledQuote, args: PickledArgs): QuoteContext ?=> Type[T] =
2424
val qctx = quoteContextWithCompilerInterface(summon[QuoteContext])
2525
val tree = qctx.tasty.unpickleType(repr, args)
26-
new scala.internal.quoted.Type(tree, qctx.tasty.compilerId).asInstanceOf[Type[T]]
26+
new scala.internal.quoted.Type(tree, qctx.hashCode).asInstanceOf[Type[T]]
2727

2828
}

library/src-non-bootstrapped/scala/internal/quoted/Expr.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
2020
}
2121

2222
def unseal(using qctx: QuoteContext): qctx.tasty.Term =
23-
if (quoteContextWithCompilerInterface(qctx).tasty.compilerId != scopeId)
23+
if (qctx.hashCode != scopeId)
2424
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
2525
tree.asInstanceOf[qctx.tasty.Term]
2626

library/src-non-bootstrapped/scala/internal/quoted/Type.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quote
1515

1616
/** View this expression `quoted.Type[T]` as a `TypeTree` */
1717
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
18-
if (quoteContextWithCompilerInterface(qctx).tasty.compilerId != scopeId)
18+
if (qctx.hashCode != scopeId)
1919
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
2020
typeTree.asInstanceOf[qctx.tasty.TypeTree]
2121

library/src/scala/internal/tasty/CompilerInterface.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ trait CompilerInterface { self: scala.tasty.Reflection =>
6666

6767
def lambdaExtractor(term: Term, paramTypes: List[Type]): Option[List[Term] => Term]
6868

69-
def compilerId: Int
70-
7169
}
7270

7371

0 commit comments

Comments
 (0)