Skip to content

Commit bf478d0

Browse files
Merge pull request #9192 from dotty-staging/remove-unseal-from-compiler-interface
Remove unseal from compiler interface
2 parents 4f1dd8f + 2874610 commit bf478d0

File tree

11 files changed

+28
-41
lines changed

11 files changed

+28
-41
lines changed

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ object PickledQuotes {
196196
}
197197

198198
/** Make sure that the owner of this tree is `ctx.owner` */
199-
private def healOwner(tree: Tree)(implicit ctx: Context): Tree = {
199+
def healOwner(tree: Tree)(implicit ctx: Context): Tree = {
200200
val getCurrentOwner = new TreeAccumulator[Option[Symbol]] {
201201
def apply(x: Option[Symbol], tree: tpd.Tree)(implicit ctx: Context): Option[Symbol] =
202202
if (x.isDefined) x

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,19 +1889,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
18891889
def Flags_Package: Flags = core.Flags.Package
18901890

18911891

1892-
////////////////////////
1893-
// QUOTED SEAL/UNSEAL //
1894-
////////////////////////
1895-
1896-
/** View this expression `quoted.Expr[?]` as a `Term` */
1897-
def QuotedExpr_unseal(self: scala.quoted.Expr[?])(using ctx: Context): Term =
1898-
PickledQuotes.quotedExprToTree(self)
1899-
1900-
/** View this expression `quoted.Type[?]` as a `TypeTree` */
1901-
def QuotedType_unseal(self: scala.quoted.Type[?])(using ctx: Context): TypeTree =
1902-
PickledQuotes.quotedTypeToTree(self)
1903-
1904-
19051892
/////////////////
19061893
// DEFINITIONS //
19071894
/////////////////

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ object Splicer {
4040
def splice(tree: Tree, pos: SourcePosition, classLoader: ClassLoader)(using Context): Tree = tree match {
4141
case Quoted(quotedTree) => quotedTree
4242
case _ =>
43-
val interpreter = new Interpreter(pos, classLoader)
4443
val macroOwner = ctx.newSymbol(ctx.owner, nme.MACROkw, Macro | Synthetic, defn.AnyType, coord = tree.span)
4544
try
4645
inContext(ctx.withOwner(macroOwner)) {
46+
val interpreter = new Interpreter(pos, classLoader)
47+
4748
// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
4849
val interpretedExpr = interpreter.interpret[scala.quoted.QuoteContext => scala.quoted.Expr[Any]](tree)
4950
val interpretedTree = interpretedExpr.fold(tree)(macroClosure => PickledQuotes.quotedExprToTree(macroClosure(QuoteContext())))
51+
5052
checkEscapedVariables(interpretedTree, macroOwner)
5153
}.changeOwner(macroOwner, ctx.owner)
5254
catch {
@@ -295,10 +297,10 @@ object Splicer {
295297
}
296298

297299
private def interpretQuote(tree: Tree)(implicit env: Env): Object =
298-
new scala.internal.quoted.Expr(Inlined(EmptyTree, Nil, tree).withSpan(tree.span), QuoteContext.scopeId)
300+
new scala.internal.quoted.Expr(Inlined(EmptyTree, Nil, PickledQuotes.healOwner(tree)).withSpan(tree.span), QuoteContext.scopeId)
299301

300302
private def interpretTypeQuote(tree: Tree)(implicit env: Env): Object =
301-
new scala.internal.quoted.Type(tree, QuoteContext.scopeId)
303+
new scala.internal.quoted.Type(PickledQuotes.healOwner(tree), QuoteContext.scopeId)
302304

303305
private def interpretLiteral(value: Any)(implicit env: Env): Object =
304306
value.asInstanceOf[Object]

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package scala.quoted
33
import scala.quoted.show.SyntaxHighlight
44

55
/** Quoted expression of type `T` */
6-
class Expr[+T] private[scala] {
6+
abstract class Expr[+T] private[scala] {
77

88
/** Show a source code like representation of this expression without syntax highlight */
99
def show(using qctx: QuoteContext): String =
@@ -55,8 +55,7 @@ class Expr[+T] private[scala] {
5555
}
5656

5757
/** View this expression `quoted.Expr[T]` as a `Term` */
58-
def unseal(using qctx: QuoteContext): qctx.tasty.Term =
59-
qctx.tasty.internal.QuotedExpr_unseal(this)(using qctx.tasty.rootContext)
58+
def unseal(using qctx: QuoteContext): qctx.tasty.Term
6059

6160
}
6261

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package scala.quoted
33
import scala.quoted.show.SyntaxHighlight
44

55
/** Quoted type (or kind) `T` */
6-
class Type[T <: AnyKind] private[scala] {
6+
abstract class Type[T <: AnyKind] private[scala] {
77
type `$splice` = T
88

99
/** Show a source code like representation of this type without syntax highlight */
@@ -15,8 +15,7 @@ class Type[T <: AnyKind] private[scala] {
1515
this.unseal.showWith(syntaxHighlight)
1616

1717
/** View this expression `quoted.Type[T]` as a `TypeTree` */
18-
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
19-
qctx.tasty.internal.QuotedType_unseal(this)(using qctx.tasty.rootContext)
18+
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree
2019

2120
}
2221

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package scala.quoted
22

3-
class Expr[+T] private[scala]:
4-
def unseal(using qctx: QuoteContext): qctx.tasty.Term =
5-
throw new Exception("Non bootstrapped library")
3+
abstract class Expr[+T] private[scala]:
4+
def unseal(using qctx: QuoteContext): qctx.tasty.Term
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package scala.quoted
22

3-
class Type[T <: AnyKind] private[scala]:
3+
abstract class Type[T <: AnyKind] private[scala]:
44
type `$splice` = T
5-
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
6-
throw new Exception("Non bootstrapped library")
5+
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ import scala.quoted._
1717
tree == that.tree && scopeId == that.scopeId
1818
case _ => false
1919
}
20+
21+
def unseal(using qctx: QuoteContext): qctx.tasty.Term =
22+
if (qctx.tasty.internal.compilerId != scopeId)
23+
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
24+
tree.asInstanceOf[qctx.tasty.Term]
25+
2026
override def hashCode: Int = tree.hashCode
2127
override def toString: String = "'{ ... }"
2228
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quote
1111
that.typeTree && scopeId == that.scopeId
1212
case _ => false
1313
}
14+
15+
/** View this expression `quoted.Type[T]` as a `TypeTree` */
16+
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
17+
if (qctx.tasty.internal.compilerId != scopeId)
18+
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
19+
typeTree.asInstanceOf[qctx.tasty.TypeTree]
20+
1421
override def hashCode: Int = typeTree.hashCode
1522
override def toString: String = "'[ ... ]"
1623
}

library/src/scala/tasty/Reflection.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
15961596

15971597
/** Returns the type (Type) of T */
15981598
def typeOf[T](using qtype: scala.quoted.Type[T], ctx: Context): Type =
1599-
internal.QuotedType_unseal(qtype).tpe
1599+
qtype.asInstanceOf[scala.internal.quoted.Type[T]].typeTree.asInstanceOf[TypeTree].tpe
16001600

16011601
/** Members of `TypeOrBounds` */
16021602
extension TypeOrBoundsOps on (tpe: TypeOrBounds) {

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,17 +1436,6 @@ trait CompilerInterface {
14361436
def Flags_Package: Flags
14371437

14381438

1439-
////////////////////////
1440-
// QUOTED SEAL/UNSEAL //
1441-
////////////////////////
1442-
1443-
/** View this expression `quoted.Expr[Any]` as a `Term` */
1444-
def QuotedExpr_unseal(self: scala.quoted.Expr[Any])(using ctx: Context): Term
1445-
1446-
/** View this expression `quoted.Type[T]` as a `TypeTree` */
1447-
def QuotedType_unseal(self: scala.quoted.Type[_])(using ctx: Context): TypeTree
1448-
1449-
14501439
/////////////////
14511440
// DEFINITIONS //
14521441
/////////////////

0 commit comments

Comments
 (0)