Skip to content

Remove cast from CompilerInterface #9191

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
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 @@ -1906,20 +1906,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
case _ => Some(new scala.internal.quoted.Expr(self, compilerId))
}

/** Checked cast to a `quoted.Expr[U]` */
def QuotedExpr_cast[U](self: scala.quoted.Expr[?])(using tp: scala.quoted.Type[U], ctx: Context): scala.quoted.Expr[U] = {
val tree = QuotedExpr_unseal(self)
val expectedType = QuotedType_unseal(tp).tpe
if (tree.tpe <:< expectedType)
self.asInstanceOf[scala.quoted.Expr[U]]
else
throw new scala.tasty.reflect.ExprCastError(
s"""Expr: ${tree.show}
|did not conform to type: ${expectedType.show}
|""".stripMargin
)
}

/** Convert `Type` to an `quoted.Type[?]` */
def QuotedType_seal(self: Type)(using ctx: Context): scala.quoted.Type[?] = {
val dummySpan = ctx.owner.span // FIXME
Expand Down
14 changes: 12 additions & 2 deletions library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,18 @@ class Expr[+T] private[scala] {
!scala.internal.quoted.Expr.unapply[EmptyTuple, EmptyTuple](this)(using that, false, qctx).isEmpty

/** Checked cast to a `quoted.Expr[U]` */
def cast[U](using tp: scala.quoted.Type[U])(using qctx: QuoteContext): scala.quoted.Expr[U] =
qctx.tasty.internal.QuotedExpr_cast[U](this)(using tp, qctx.tasty.rootContext)
def cast[U](using tp: scala.quoted.Type[U])(using qctx: QuoteContext): scala.quoted.Expr[U] = {
val tree = this.unseal
val expectedType = tp.unseal.tpe
if (tree.tpe <:< expectedType)
this.asInstanceOf[scala.quoted.Expr[U]]
else
throw new scala.tasty.reflect.ExprCastError(
s"""Expr: ${tree.show}
|did not conform to type: ${expectedType.show}
|""".stripMargin
)
}

/** View this expression `quoted.Expr[T]` as a `Term` */
def unseal(using qctx: QuoteContext): qctx.tasty.Term =
Expand Down
3 changes: 0 additions & 3 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1443,9 +1443,6 @@ trait CompilerInterface {
/** View this expression `quoted.Expr[Any]` as a `Term` */
def QuotedExpr_unseal(self: scala.quoted.Expr[Any])(using ctx: Context): Term

/** Checked cast to a `quoted.Expr[U]` */
def QuotedExpr_cast[U](self: scala.quoted.Expr[Any])(using tp: scala.quoted.Type[U], ctx: Context): scala.quoted.Expr[U]

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

Expand Down