Skip to content

Avoid uses of quoted.Expr[_] #8913

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 @@ -256,7 +256,7 @@ For precise rules, semantics and a larger example of `BigFloat`, see [the docume
## Metaprogramming Progress
We are making steady progress with the language metaprogramming features. The metaprogramming spotlights of this release are as follows:

- `toExprOfTuple` method which allows converting a `Seq[Expr[_]]` to `Expr[Tuple]`. The types of the expressions will be preserved in the tuple. See [#7037](https://github.com/lampepfl/dotty/pull/7037) and [#7076](https://github.com/lampepfl/dotty/pull/7076) for the details.
- `toExprOfTuple` method which allows converting a `Seq[Expr[Any]]` to `Expr[Tuple]`. The types of the expressions will be preserved in the tuple. See [#7037](https://github.com/lampepfl/dotty/pull/7037) and [#7076](https://github.com/lampepfl/dotty/pull/7076) for the details.
- `toExprOfTuple` method that converts a tuple of expressions to an expression of tuple – see [#7047](https://github.com/lampepfl/dotty/pull/7047).
- `toExprOfSeq` which converts an `Seq[Expr[A]]` to `Expr[Seq[A]]` – see [#6935](https://github.com/lampepfl/dotty/pull/6935).
- More `Liftable` instances – for Tuples of arity greater than 22, `BigInt` and `BigDecimal` – see [#6947](https://github.com/lampepfl/dotty/pull/6947) and [#6944](https://github.com/lampepfl/dotty/pull/6944).
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/reference/metaprogramming/macros-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,13 @@ through quotes. Most likely, those constructors would work over `Expr`
types which lack a known type argument. For instance, an `Apply`
constructor could be typed as follows:
```scala
def Apply(fn: Expr[_], args: List[Expr[_]]): Expr[_]
def Apply(fn: Expr[Any], args: List[Expr[Any]]): Expr[Any]
```
This would allow constructing applications from lists of arguments
without having to match the arguments one-by-one with the
corresponding formal parameter types of the function. We then need "at
the end" a method to convert an `Expr[_]` to an `Expr[T]` where `T` is
given from the outside. E.g. if `code` yields a `Expr[_]`, then
the end" a method to convert an `Expr[Any]` to an `Expr[T]` where `T` is
given from the outside. E.g. if `code` yields a `Expr[Any]`, then
`code.atType[T]` yields an `Expr[T]`. The `atType` method has to be
implemented as a primitive; it would check that the computed type
structure of `Expr` is a subtype of the type structure representing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def foo(x: Expr[Int])(using tasty.Reflect): Expr[Int] = x match {
At runtime to a `quoted.Expr` can be matched to another using `scala.internal.quoted.Expr.unapply`.

```scala
def unapply[Tup <: Tuple](scrutineeExpr: Expr[_])(implicit patternExpr: Expr[_], reflection: Reflection): Option[Tup]
def unapply[Tup <: Tuple](scrutineeExpr: Expr[Any])(implicit patternExpr: Expr[Any], reflection: Reflection): Option[Tup]
```

The `scrutineeExpr` is a normal quoted expression while `patternExpr` may contain holes representing splices.
Expand Down
6 changes: 3 additions & 3 deletions library/src/scala/internal/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ object Expr {
* - scala.internal.Quoted.patternHole[T]: hole that matches an expression `x` of type `Expr[U]`
* if `U <:< T` and returns `x` as part of the match.
*
* @param scrutineeExpr `Expr[_]` on which we are pattern matching
* @param patternExpr `Expr[_]` containing the pattern tree
* @param scrutineeExpr `Expr[Any]` on which we are pattern matching
* @param patternExpr `Expr[Any]` containing the pattern tree
* @param hasTypeSplices `Boolean` notify if the pattern has type splices (if so we use a GADT context)
* @param qctx the current QuoteContext
* @return None if it did not match, `Some(tup)` if it matched where `tup` contains `Expr[Ti]``
*/
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: scala.quoted.Expr[_])(using patternExpr: scala.quoted.Expr[_],
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: scala.quoted.Expr[Any])(using patternExpr: scala.quoted.Expr[Any],
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] = {
new Matcher.QuoteMatcher[qctx.type].termMatch(scrutineeExpr.unseal, patternExpr.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
}
Expand Down
12 changes: 6 additions & 6 deletions library/src/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ object Expr {
* ```
*/
def betaReduce[F, Args <: Tuple, R, G](f: Expr[F])(using tf: TupledFunction[F, Args => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G =
tg.untupled(args => qctx.tasty.internal.betaReduce(f.unseal, args.toArray.toList.map(_.asInstanceOf[QuoteContext => Expr[_]](qctx).unseal)).seal.asInstanceOf[Expr[R]])
tg.untupled(args => qctx.tasty.internal.betaReduce(f.unseal, args.toArray.toList.map(_.asInstanceOf[QuoteContext => Expr[Any]](qctx).unseal)).seal.asInstanceOf[Expr[R]])

/** `Expr.betaReduceGiven(f)(x1, ..., xn)` is functionally the same as `'{($f)(using $x1, ..., $xn)}`, however it optimizes this call
* by returning the result of beta-reducing `f(using x1, ..., xn)` if `f` is a known lambda expression.
Expand All @@ -91,7 +91,7 @@ object Expr {
* ```
*/
def betaReduceGiven[F, Args <: Tuple, R, G](f: Expr[F])(using tf: TupledFunction[F, Args ?=> R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G =
tg.untupled(args => qctx.tasty.internal.betaReduce(f.unseal, args.toArray.toList.map(_.asInstanceOf[QuoteContext => Expr[_]](qctx).unseal)).seal.asInstanceOf[Expr[R]])
tg.untupled(args => qctx.tasty.internal.betaReduce(f.unseal, args.toArray.toList.map(_.asInstanceOf[QuoteContext => Expr[Any]](qctx).unseal)).seal.asInstanceOf[Expr[R]])

/** Returns a null expresssion equivalent to `'{null}` */
def nullExpr: QuoteContext ?=> Expr[Null] = qctx ?=> {
Expand All @@ -109,7 +109,7 @@ object Expr {
* Given list of statements `s1 :: s2 :: ... :: Nil` and an expression `e` the resulting expression
* will be equivalent to `'{ $s1; $s2; ...; $e }`.
*/
def block[T](statements: List[Expr[_]], expr: Expr[T])(using qctx: QuoteContext): Expr[T] = {
def block[T](statements: List[Expr[Any]], expr: Expr[T])(using qctx: QuoteContext): Expr[T] = {
import qctx.tasty._
Block(statements.map(_.unseal), expr.unseal).seal.asInstanceOf[Expr[T]]
}
Expand Down Expand Up @@ -140,11 +140,11 @@ object Expr {
/** Lifts this sequence of expressions into an expression of a tuple
*
* Transforms a sequence of expression
* `Seq(e1, e2, ...)` where `ei: Expr[_]`
* `Seq(e1, e2, ...)` where `ei: Expr[Any]`
* to an expression equivalent to
* `'{ ($e1, $e2, ...) }` typed as an `Expr[Tuple]`
*/
def ofTuple(seq: Seq[Expr[_]])(using qctx: QuoteContext): Expr[Tuple] = {
def ofTuple(seq: Seq[Expr[Any]])(using qctx: QuoteContext): Expr[Tuple] = {
seq match {
case Seq() =>
unitExpr
Expand Down Expand Up @@ -199,7 +199,7 @@ object Expr {

/** Given a tuple of the form `(Expr[A1], ..., Expr[An])`, outputs a tuple `Expr[(A1, ..., An)]`. */
def ofTuple[T <: Tuple: Tuple.IsMappedBy[Expr]: Type](tup: T)(using qctx: QuoteContext): Expr[Tuple.InverseMap[T, Expr]] = {
val elems: Seq[Expr[_]] = tup.asInstanceOf[Product].productIterator.toSeq.asInstanceOf[Seq[Expr[_]]]
val elems: Seq[Expr[Any]] = tup.asInstanceOf[Product].productIterator.toSeq.asInstanceOf[Seq[Expr[Any]]]
ofTuple(elems).cast[Tuple.InverseMap[T, Expr]]
}

Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/quoted/QuoteContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class QuoteContext(val tasty: scala.tasty.Reflection) { self =>
tasty.warning(msg, tasty.rootPosition)

/** Report a warning at the on the position of `expr` */
def warning(msg: => String, expr: Expr[_]): Unit =
def warning(msg: => String, expr: Expr[Any]): Unit =
tasty.warning(msg, expr.unseal(using this).pos)

}
8 changes: 4 additions & 4 deletions library/src/scala/tasty/reflect/CompilerInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ trait CompilerInterface {
/** Unpickle `repr` which represents a pickled `Expr` tree,
* replacing splice nodes with `args`
*/
def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Expr[_]
def unpickleExpr(repr: Unpickler.PickledQuote, args: Unpickler.PickledArgs): scala.quoted.Expr[Any]

/** Unpickle `repr` which represents a pickled `Type` tree,
* replacing splice nodes with `args`
Expand Down Expand Up @@ -1425,11 +1425,11 @@ trait CompilerInterface {
// QUOTED SEAL/UNSEAL //
////////////////////////

/** View this expression `quoted.Expr[_]` as a `Term` */
def QuotedExpr_unseal(self: scala.quoted.Expr[_])(using ctx: Context): Term
/** 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[_])(using tp: scala.quoted.Type[U], ctx: Context): scala.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