Skip to content

Allow generically sealed expressions #5651

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

Closed
Closed
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
17 changes: 15 additions & 2 deletions compiler/src/dotty/tools/dotc/tastyreflect/QuotedOpsImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Symbols.defn
import dotty.tools.dotc.core.StdNames.nme
import dotty.tools.dotc.core.quoted.PickledQuotes
import dotty.tools.dotc.core.Types
import dotty.tools.dotc.core.{Contexts, Types}

trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with CoreImpl {

Expand All @@ -20,9 +20,20 @@ trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with CoreImpl {

def TermToQuoteDeco(term: Term): TermToQuotedAPI = new TermToQuotedAPI {

def seal[T: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[T] = {
def seal(implicit ctx: Contexts.Context): scala.quoted.Sealed = {
val expr = new scala.quoted.Exprs.TastyTreeExpr(term)
val typeTree = tpd.TypeTree(term.tpe).withPos(term.pos)
val tpe = new scala.quoted.Types.TreeType(typeTree)
scala.quoted.Sealed(expr)(tpe)
}

}

def SealedDeco(seal: quoted.Sealed): SealedAPI = new SealedAPI {

def asExprOf[T: quoted.Type](implicit ctx: Context): quoted.Expr[T] = {
val expectedType = QuotedTypeDeco(implicitly[scala.quoted.Type[T]]).unseal.tpe
val term = QuotedExprDeco(seal.expr).unseal

def etaExpand(term: Term): Term = term.tpe.widen match {
case mtpe: Types.MethodType if !mtpe.isParamDependent =>
Expand All @@ -47,5 +58,7 @@ trait QuotedOpsImpl extends scala.tasty.reflect.QuotedOps with CoreImpl {
)
}
}

}

}
25 changes: 25 additions & 0 deletions library/src/scala/quoted/SealedExpr.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package scala.quoted

/** A container for an `Expr[Tpe]` with its `Type[Tpe]` where `Tpe` might be unkown.
*
* Example usage:
* ```
* def let[T](s: Sealed)(body: Expr[s.Tpe] => Expr[T]): Expr[T] = '{
* val x: ~s.tpe = ~s.expr
* ~body('(x))
* }
* ```
*/
final class Sealed private (e: Expr[_], t: Type[_]) {

type Tpe

val expr: Expr[Tpe] = e.asInstanceOf[Expr[Tpe]]

val tpe: Type[Tpe] = t.asInstanceOf[Type[Tpe]]

}

object Sealed {
def apply[T](expr: Expr[T])(implicit tpe: Type[T]): Sealed { type Tpe = T } = new Sealed(expr, tpe).asInstanceOf[Sealed { type Tpe = T }]
}
9 changes: 7 additions & 2 deletions library/src/scala/tasty/reflect/QuotedOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ trait QuotedOps extends Core {
implicit def QuotedTypeDeco[T](tpe: quoted.Type[T]): QuotedTypeAPI

trait TermToQuotedAPI {
/** Convert `Term` to an `Expr[T]` and check that it conforms to `T` */
def seal[T: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[T]
/** Convert `Term` to an `Expr[Tpe]` with its `Type[Tpe]` for some unknown `Tpe` */
def seal(implicit ctx: Context): quoted.Sealed
}
implicit def TermToQuoteDeco(term: Term): TermToQuotedAPI

trait SealedAPI {
/** Return the `expr` as an `Expr[T]` and check that it conforms to a known `T` */
def asExprOf[T: quoted.Type](implicit ctx: Context): quoted.Expr[T]
}
implicit def SealedDeco(seal: quoted.Sealed): SealedAPI
}
15 changes: 15 additions & 0 deletions tests/neg/quoted-sealed.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

import scala.quoted._

class Foo {

def let[T](s1: Sealed, s2: Sealed)(body: Expr[s1.Tpe] => Expr[T]): Expr[T] = '{
val x: ~s2.tpe = ~s1.expr // error
val y: ~s1.tpe = ~s2.expr // error
val z: Any = ~s1.expr
~body('(x)) // error
~body('(y))
~body('(z)) // error
}

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

tree match {
case Term.Inlined(_, Nil, Term.Apply(Term.Select(OpsTree(left), op), right :: Nil)) =>
'(assertTrue(~left.seal[Boolean])) // Buggy code. To generate the errors
'(assertTrue(~left.seal.asExprOf[Boolean])) // Buggy code. To generate the errors
case _ =>
'(assertTrue(~cond))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run/f-interpolation-1/FQuote_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object FQuote {

def liftListOfAny(lst: List[Term]): Expr[List[Any]] = lst match {
case x :: xs =>
val head = x.seal[Any]
val head = x.seal.asExprOf[Any]
val tail = liftListOfAny(xs)
'{ ~head :: ~tail }
case Nil => '(Nil)
Expand Down
2 changes: 1 addition & 1 deletion tests/run/i5533/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object scalatest {

val tree = condition.unseal

val expr = tree.seal[Boolean]
val expr = tree.seal.asExprOf[Boolean]

'(println(~expr))
}
Expand Down
4 changes: 2 additions & 2 deletions tests/run/i5533b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ object scalatest {

tree.underlyingArgument match {
case Term.Apply(Term.Select(lhs, op), rhs :: Nil) =>
val left = lhs.seal[Any]
val right = rhs.seal[Any]
val left = lhs.seal.asExprOf[Any]
val right = rhs.seal.asExprOf[Any]
op match {
case "==" =>
'{
Expand Down
4 changes: 2 additions & 2 deletions tests/run/i5536/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ object scalatest {

tree.underlyingArgument match {
case Term.Apply(Term.Select(lhs, op), rhs :: Nil) =>
val left = lhs.seal[Any]
val right = rhs.seal[Any]
val left = lhs.seal.asExprOf[Any]
val right = rhs.seal.asExprOf[Any]
op match {
case "===" =>
'{
Expand Down
68 changes: 68 additions & 0 deletions tests/run/quote-extractors-1.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
a.>(5)
{
val pre: a = a
val arg: 5 = 5
pre.>(arg)
}

a.>(5.0)
{
val pre: a = a
val arg: 5.0 = 5.0
pre.>(arg)
}

a.>('a')
{
val pre: a = a
val arg: 'a' = 'a'
pre.>(arg)
}

b1.>(4)
{
val pre: b1 = b1
val arg: 4 = 4
pre.>(arg)
}

b1.>(b2)
{
val pre: b1 = b1
val arg: b2 = b2
pre.>(arg)
}

b.unary_!
{
val pre: b = b
pre.unary_!
}

true
true

false
false

if (b.unary_!) a.>(5) else b1.>(4)
if ({
val pre: b = b
pre.unary_!
}) {
val pre$2: a = a
val arg: 5 = 5
pre$2.>(arg)
} else {
val pre$3: b1 = b1
val arg$2: 4 = 4
pre$3.>(arg$2)
}

while (b3) {
scala.Predef.println(b3)
b3 = false
}
while (b3) scala.Predef.???

end
Loading