Skip to content

Split quoted.Expr casting from sealing #6189

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 1 commit into from
Apr 2, 2019
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
28 changes: 15 additions & 13 deletions compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1681,19 +1681,16 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
// QUOTED SEAL/UNSEAL
//

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

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

/** Convert `Term` to an `Expr[T]` and check that it conforms to `T` */
def QuotedExpr_seal[T](self: Term)(tpe: scala.quoted.Type[T])(implicit ctx: Context): scala.quoted.Expr[T] = {

val expectedType = QuotedType_unseal(tpe).tpe

/** Convert `Term` to an `quoted.Expr[Any]` */
def QuotedExpr_seal(self: Term)(implicit ctx: Context): scala.quoted.Expr[Any] = {
def etaExpand(term: Term): Term = term.tpe.widen match {
case mtpe: Types.MethodType if !mtpe.isParamDependent =>
val closureResType = mtpe.resType match {
Expand All @@ -1705,20 +1702,25 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
tpd.Closure(closureMethod, tss => etaExpand(new tpd.TreeOps(term).appliedToArgs(tss.head)))
case _ => term
}
new scala.quoted.Exprs.TastyTreeExpr(etaExpand(self))
}

val expanded = etaExpand(self)
if (expanded.tpe <:< expectedType) {
new scala.quoted.Exprs.TastyTreeExpr(expanded).asInstanceOf[scala.quoted.Expr[T]]
/** Checked cast to a `quoted.Expr[U]` */
def QuotedExpr_cast[U](self: scala.quoted.Expr[_])(implicit 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.TastyTypecheckError(
s"""Term: ${self.show}
throw new scala.tasty.reflect.ExprCastError(
s"""Expr: ${tree.show}
|did not conform to type: ${expectedType.show}
|""".stripMargin
)
}
}

/** Convert `Type` to an `quoted.Type[T]` */
/** Convert `Type` to an `quoted.Type[_]` */
def QuotedType_seal(self: Type)(implicit ctx: Context): scala.quoted.Type[_] = {
val dummySpan = ctx.owner.span // FIXME
new scala.quoted.Types.TreeType(tpd.TypeTree(self).withSpan(dummySpan))
Expand Down
16 changes: 10 additions & 6 deletions library/src-bootstrapped/scala/tasty/reflect/QuotedOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ package scala.tasty.reflect
trait QuotedOps extends Core {

implicit class QuotedExprAPI[T](expr: scala.quoted.Expr[T]) {
/** View this expression `Expr[T]` as a `Term` */
/** View this expression `quoted.Expr[T]` as a `Term` */
def unseal(implicit ctx: Context): Term =
kernel.QuotedExpr_unseal(expr)

/** Checked cast to a `quoted.Expr[U]` */
def cast[U: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[U] =
kernel.QuotedExpr_cast[U](expr)
}

implicit class QuotedTypeAPI[T <: AnyKind](tpe: scala.quoted.Type[T]) {
/** View this expression `Type[T]` as a `TypeTree` */
/** View this expression `quoted.Type[T]` as a `TypeTree` */
def unseal(implicit ctx: Context): TypeTree =
kernel.QuotedType_unseal(tpe)
}

implicit class TermToQuotedAPI(term: Term) {
/** Convert `Term` to an `Expr[T]` and check that it conforms to `T` */
def seal[T](implicit tpe: scala.quoted.Type[T], ctx: Context): scala.quoted.Expr[T] =
kernel.QuotedExpr_seal(term)(tpe)
/** Convert `Term` to an `quoted.Expr[Any]` */
def seal(implicit ctx: Context): scala.quoted.Expr[Any] =
kernel.QuotedExpr_seal(term)
}

implicit class TypeToQuotedAPI(tpe: Type) {
/** Convert `Type` to an `quoted.Type[T]` */
/** Convert `Type` to an `quoted.Type[_]` */
def seal(implicit ctx: Context): scala.quoted.Type[_] =
kernel.QuotedType_seal(tpe)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ trait TreeUtils {
def let(rhs: Term)(body: Ident => Term): Term = {
type T // TODO probably it is better to use the Sealed contruct rather than let the user create their own existential type
implicit val rhsTpe: quoted.Type[T] = rhs.tpe.seal.asInstanceOf[quoted.Type[T]]
val rhsExpr = rhs.seal[T]
val rhsExpr = rhs.seal.cast[T]
val expr = '{
val x = $rhsExpr
${
val id = ('x).unseal.asInstanceOf[Ident]
body(id).seal[Any]
body(id).seal
}
}
expr.unseal
Expand Down
16 changes: 10 additions & 6 deletions library/src-non-bootstrapped/scala/tasty/reflect/QuotedOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ package scala.tasty.reflect
trait QuotedOps extends Core {

implicit class QuotedExprAPI[T](expr: scala.quoted.Expr[T]) {
/** View this expression `Expr[T]` as a `Term` */
/** View this expression `quoted.Expr[T]` as a `Term` */
def unseal(implicit ctx: Context): Term =
kernel.QuotedExpr_unseal(expr)

/** Checked cast to a `quoted.Expr[U]` */
def cast[U: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[U] =
kernel.QuotedExpr_cast[U](expr)
}

implicit class QuotedTypeAPI[T](tpe: scala.quoted.Type[T]) {
/** View this expression `Type[T]` as a `TypeTree` */
/** View this expression `quoted.Type[T]` as a `TypeTree` */
def unseal(implicit ctx: Context): TypeTree =
kernel.QuotedType_unseal(tpe)
}

implicit class TermToQuotedAPI(term: Term) {
/** Convert `Term` to an `Expr[T]` and check that it conforms to `T` */
def seal[T](implicit tpe: scala.quoted.Type[T], ctx: Context): scala.quoted.Expr[T] =
kernel.QuotedExpr_seal(term)(tpe)
/** Convert `Term` to an `quoted.Expr[Any]` */
def seal(implicit ctx: Context): scala.quoted.Expr[Any] =
kernel.QuotedExpr_seal(term)
}

implicit class TypeToQuotedAPI(tpe: Type) {
/** Convert `Type` to an `quoted.Type[T]` */
/** Convert `Type` to an `quoted.Type[_]` */
def seal(implicit ctx: Context): scala.quoted.Type[_] =
kernel.QuotedType_seal(tpe)
}
Expand Down
3 changes: 0 additions & 3 deletions library/src/scala/tasty/TastyTypecheckError.scala

This file was deleted.

3 changes: 3 additions & 0 deletions library/src/scala/tasty/reflect/ExprCastError.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package scala.tasty.reflect

class ExprCastError(msg: String) extends Throwable(msg)
14 changes: 9 additions & 5 deletions library/src/scala/tasty/reflect/Kernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1375,16 +1375,20 @@ trait Kernel {
// QUOTED SEAL/UNSEAL
//

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

/** View this expression `Type[T]` as a `TypeTree` */
/** Checked cast to a `quoted.Expr[U]` */
def QuotedExpr_cast[U](self: scala.quoted.Expr[_])(implicit 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[_])(implicit ctx: Context): TypeTree

/** Convert `Term` to an `Expr[T]` and check that it conforms to `T` */
def QuotedExpr_seal[T](self: Term)(tpe: scala.quoted.Type[T])(implicit ctx: Context): scala.quoted.Expr[T]
/** Convert `Term` to an `quoted.Expr[Any]` */
def QuotedExpr_seal(self: Term)(implicit ctx: Context): scala.quoted.Expr[Any]


/** Convert `Type` to an `quoted.Type[T]` */
/** Convert `Type` to an `quoted.Type[_]` */
def QuotedType_seal(self: Type)(implicit ctx: Context): scala.quoted.Type[_]

//
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-with-compiler/i5941/macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object Lens {

// obj.copy(field = value)
def setterBody(obj: Expr[S], value: Expr[T], field: String): Expr[S] =
Select.overloaded(obj.unseal, "copy", Nil, NamedArg(field, value.unseal) :: Nil).seal[S]
Select.overloaded(obj.unseal, "copy", Nil, NamedArg(field, value.unseal) :: Nil).seal.cast[S]

// exception: getter.unseal.underlyingArgument
getter.unseal match {
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/tasty-macro-assert-1/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object Asserts {

tree match {
case Inlined(_, Nil, Apply(Select(OpsTree(left), op), right :: Nil)) =>
'{assertTrue(${left.seal[Boolean]})} // Buggy code. To generate the errors
'{assertTrue(${left.seal.cast[Boolean]})} // Buggy code. To generate the errors
case _ =>
'{assertTrue($cond)}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/tasty-macro-assert-2/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object Asserts {

tree match {
case Inlined(_, Nil, Apply(Select(OpsTree(left), op), right :: Nil)) =>
'{assertTrue(${left.seal[Boolean]})} // Buggy code. To generate the errors
'{assertTrue(${left.seal.cast[Boolean]})} // Buggy code. To generate the errors
case _ =>
'{assertTrue($cond)}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run-with-compiler/i5715/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object scalatest {
cond.unseal.underlyingArgument match {
case app @ Apply(sel @ Select(lhs, op), rhs :: Nil) =>
val IsSelect(select) = sel
val cond = Apply(Select.copy(select)(lhs, "exists"), rhs :: Nil).seal[Boolean]
val cond = Apply(Select.copy(select)(lhs, "exists"), rhs :: Nil).seal.cast[Boolean]
'{ scala.Predef.assert($cond) }
case _ =>
'{ scala.Predef.assert($cond) }
Expand Down
10 changes: 5 additions & 5 deletions tests/run-with-compiler/i5941/macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object Lens {
getter.unseal match {
case Function(param :: Nil, Path(o, parts)) if o.symbol == param.symbol =>
'{
val setter = (t: T) => (s: S) => ${ setterBody(('s).unseal, ('t).unseal, parts).seal[S] }
val setter = (t: T) => (s: S) => ${ setterBody(('s).unseal, ('t).unseal, parts).seal.cast[S] }
apply($getter)(setter)
}
case _ =>
Expand Down Expand Up @@ -122,9 +122,9 @@ object Iso {

'{
// (p: S) => p._1
val to = (p: S) => ${ Select.unique(('p).unseal, "_1").seal[A] }
val to = (p: S) => ${ Select.unique(('p).unseal, "_1").seal.cast[A] }
// (p: A) => S(p)
val from = (p: A) => ${ Select.overloaded(Ident(companion), "apply", Nil, ('p).unseal :: Nil).seal[S] }
val from = (p: A) => ${ Select.overloaded(Ident(companion), "apply", Nil, ('p).unseal :: Nil).seal.cast[S] }
apply(from)(to)
}
}
Expand All @@ -137,7 +137,7 @@ object Iso {
val tpS = typeOf[S]

if (tpS.isSingleton) {
val ident = Ident(tpS.asInstanceOf[TermRef]).seal[S]
val ident = Ident(tpS.asInstanceOf[TermRef]).seal.cast[S]
'{
Iso[S, 1](Function.const($ident))(Function.const(1))
}
Expand All @@ -153,7 +153,7 @@ object Iso {
case Type.TypeRef(name, prefix) => Type.TermRef(prefix, name)
}

val obj = Select.overloaded(Ident(companion), "apply", Nil, Nil).seal[S]
val obj = Select.overloaded(Ident(companion), "apply", Nil, Nil).seal.cast[S]

'{
Iso[S, 1](Function.const($obj))(Function.const(1))
Expand Down
16 changes: 8 additions & 8 deletions tests/run-with-compiler/i6171/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ object scalatest {
let(rhs) { right =>
val app = Select.overloaded(left, op, Nil, right :: Nil)
let(app) { result =>
val l = left.seal[Any]
val r = right.seal[Any]
val b = result.seal[Boolean]
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
}
}
}.seal[Unit]
}.seal.cast[Unit]
case Apply(f @ Apply(Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
if isImplicitMethodType(f.tpe) =>
let(lhs) { left =>
let(rhs) { right =>
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
let(Apply(app, implicits)) { result =>
val l = left.seal[Any]
val r = right.seal[Any]
val b = result.seal[Boolean]
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
}
}
}.seal[Unit]
}.seal.cast[Unit]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ object scalatest {
let(rhs) { right =>
val app = Select.overloaded(left, op, Nil, right :: Nil)
let(app) { result =>
val l = left.seal[Any]
val r = right.seal[Any]
val b = result.seal[Boolean]
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
}
}
}.seal[Unit]
}.seal.cast[Unit]
case Apply(f @ Apply(Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
if isImplicitMethodType(f.tpe) =>
let(lhs) { left =>
let(rhs) { right =>
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
let(Apply(app, implicits)) { result =>
val l = left.seal[Any]
val r = right.seal[Any]
val b = result.seal[Boolean]
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
}
}
}.seal[Unit]
}.seal.cast[Unit]
}
}

Expand Down
16 changes: 8 additions & 8 deletions tests/run-with-compiler/reflect-select-copy/assert_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ object scalatest {
let(lhs) { left =>
let(rhs) { right =>
let(Apply(Select.copy(sel)(left, op), right :: Nil)) { result =>
val l = left.seal[Any]
val r = right.seal[Any]
val b = result.seal[Boolean]
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert(${b}) }
code.unseal
}
}
}.seal[Unit]
}.seal.cast[Unit]
case Apply(f @ Apply(IsSelect(sel @ Select(Apply(qual, lhs :: Nil), op)), rhs :: Nil), implicits)
if isImplicitMethodType(f.tpe) =>
let(lhs) { left =>
let(rhs) { right =>
let(Apply(Apply(Select.copy(sel)(Apply(qual, left :: Nil), op), right :: Nil), implicits)) { result =>
val l = left.seal[Any]
val r = right.seal[Any]
val b = result.seal[Boolean]
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert(${b}) }
code.unseal
}
}
}.seal[Unit]
}.seal.cast[Unit]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ object scalatest {
let(rhs) { right =>
val app = Select.overloaded(left, op, Nil, right :: Nil)
let(app) { result =>
val l = left.seal[Any]
val r = right.seal[Any]
val b = result.seal[Boolean]
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
}
}
}.seal[Unit]
}.seal.cast[Unit]
case Apply(f @ Apply(Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
if isImplicitMethodType(f.tpe) =>
let(lhs) { left =>
let(rhs) { right =>
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
let(Apply(app, implicits)) { result =>
val l = left.seal[Any]
val r = right.seal[Any]
val b = result.seal[Boolean]
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
}
}
}.seal[Unit]
}.seal.cast[Unit]
}
}

Expand Down
Loading