Skip to content

Remove unseal from compiler interface #9192

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ object PickledQuotes {
}

/** Make sure that the owner of this tree is `ctx.owner` */
private def healOwner(tree: Tree)(implicit ctx: Context): Tree = {
def healOwner(tree: Tree)(implicit ctx: Context): Tree = {
val getCurrentOwner = new TreeAccumulator[Option[Symbol]] {
def apply(x: Option[Symbol], tree: tpd.Tree)(implicit ctx: Context): Option[Symbol] =
if (x.isDefined) x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1889,19 +1889,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
def Flags_Package: Flags = core.Flags.Package


////////////////////////
// QUOTED SEAL/UNSEAL //
////////////////////////

/** View this expression `quoted.Expr[?]` as a `Term` */
def QuotedExpr_unseal(self: scala.quoted.Expr[?])(using ctx: Context): Term =
PickledQuotes.quotedExprToTree(self)

/** View this expression `quoted.Type[?]` as a `TypeTree` */
def QuotedType_unseal(self: scala.quoted.Type[?])(using ctx: Context): TypeTree =
PickledQuotes.quotedTypeToTree(self)


/////////////////
// DEFINITIONS //
/////////////////
Expand Down
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ object Splicer {
def splice(tree: Tree, pos: SourcePosition, classLoader: ClassLoader)(using Context): Tree = tree match {
case Quoted(quotedTree) => quotedTree
case _ =>
val interpreter = new Interpreter(pos, classLoader)
val macroOwner = ctx.newSymbol(ctx.owner, nme.MACROkw, Macro | Synthetic, defn.AnyType, coord = tree.span)
try
inContext(ctx.withOwner(macroOwner)) {
val interpreter = new Interpreter(pos, classLoader)

// Some parts of the macro are evaluated during the unpickling performed in quotedExprToTree
val interpretedExpr = interpreter.interpret[scala.quoted.QuoteContext => scala.quoted.Expr[Any]](tree)
val interpretedTree = interpretedExpr.fold(tree)(macroClosure => PickledQuotes.quotedExprToTree(macroClosure(QuoteContext())))

checkEscapedVariables(interpretedTree, macroOwner)
}.changeOwner(macroOwner, ctx.owner)
catch {
Expand Down Expand Up @@ -295,10 +297,10 @@ object Splicer {
}

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

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

private def interpretLiteral(value: Any)(implicit env: Env): Object =
value.asInstanceOf[Object]
Expand Down
5 changes: 2 additions & 3 deletions library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.quoted
import scala.quoted.show.SyntaxHighlight

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

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

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

}

Expand Down
5 changes: 2 additions & 3 deletions library/src-bootstrapped/scala/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.quoted
import scala.quoted.show.SyntaxHighlight

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

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

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

}

Expand Down
5 changes: 2 additions & 3 deletions library/src-non-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package scala.quoted

class Expr[+T] private[scala]:
def unseal(using qctx: QuoteContext): qctx.tasty.Term =
throw new Exception("Non bootstrapped library")
abstract class Expr[+T] private[scala]:
def unseal(using qctx: QuoteContext): qctx.tasty.Term
5 changes: 2 additions & 3 deletions library/src-non-bootstrapped/scala/quoted/Type.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package scala.quoted

class Type[T <: AnyKind] private[scala]:
abstract class Type[T <: AnyKind] private[scala]:
type `$splice` = T
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
throw new Exception("Non bootstrapped library")
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree
6 changes: 6 additions & 0 deletions library/src/scala/internal/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import scala.quoted._
tree == that.tree && scopeId == that.scopeId
case _ => false
}

def unseal(using qctx: QuoteContext): qctx.tasty.Term =
if (qctx.tasty.internal.compilerId != scopeId)
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
tree.asInstanceOf[qctx.tasty.Term]

override def hashCode: Int = tree.hashCode
override def toString: String = "'{ ... }"
}
Expand Down
7 changes: 7 additions & 0 deletions library/src/scala/internal/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quote
that.typeTree && scopeId == that.scopeId
case _ => false
}

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

override def hashCode: Int = typeTree.hashCode
override def toString: String = "'[ ... ]"
}
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>

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

/** Members of `TypeOrBounds` */
extension TypeOrBoundsOps on (tpe: TypeOrBounds) {
Expand Down
11 changes: 0 additions & 11 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1436,17 +1436,6 @@ trait CompilerInterface {
def Flags_Package: Flags


////////////////////////
// QUOTED SEAL/UNSEAL //
////////////////////////

/** View this expression `quoted.Expr[Any]` as a `Term` */
def QuotedExpr_unseal(self: scala.quoted.Expr[Any])(using ctx: Context): Term

/** View this expression `quoted.Type[T]` as a `TypeTree` */
def QuotedType_unseal(self: scala.quoted.Type[_])(using ctx: Context): TypeTree


/////////////////
// DEFINITIONS //
/////////////////
Expand Down