Skip to content

Commit 83e6270

Browse files
Merge pull request #8441 from dotty-staging/simplify-import-of-tasty-unseal
Simplify import of tasty unseal
2 parents 5cb511a + 34c62f9 commit 83e6270

File tree

100 files changed

+152
-162
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+152
-162
lines changed

docs/docs/reference/contextual/derivation-macro.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ from the signature. The body of the `derived` method is shown below:
4242

4343
```scala
4444
given derived[T: Type](using qctx: QuoteContext) as Expr[Eq[T]] = {
45-
import qctx.tasty.{_, given _}
45+
import qctx.tasty._
4646

4747
val ev: Expr[Mirror.Of[T]] = summonExpr(using '[Mirror.Of[T]]).get
4848

@@ -176,7 +176,7 @@ object Eq {
176176
}
177177

178178
given derived[T: Type](using qctx: QuoteContext) as Expr[Eq[T]] = {
179-
import qctx.tasty.{_, given _}
179+
import qctx.tasty._
180180

181181
val ev: Expr[Mirror.Of[T]] = summonExpr(using '[Mirror.Of[T]]).get
182182

docs/docs/reference/metaprogramming/tasty-reflect.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,19 @@ import scala.quoted._
2929
inline def natConst(x: => Int): Int = ${natConstImpl('{x})}
3030

3131
def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
32-
import qctx.tasty.{_, given _}
32+
import qctx.tasty._
3333
...
3434
}
3535
```
3636

37-
### Sealing and Unsealing
37+
### Extractors
3838

39-
`import qctx.tasty.{_, given _}` will provide an `unseal` extension method on `quoted.Expr`
40-
and `quoted.Type` which returns a `qctx.tasty.Term` that represents the tree of
41-
the expression and `qctx.tasty.TypeTree` that represents the tree of the type
42-
respectively. It will also import all extractors and methods on TASTy Reflect
39+
`import qctx.tasty._` will provide all extractors and methods on TASTy Reflect
4340
trees. For example the `Literal(_)` extractor used below.
4441

4542
```scala
4643
def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
47-
import qctx.tasty.{_, given _}
44+
import qctx.tasty._
4845
val xTree: Term = x.unseal
4946
xTree match {
5047
case Inlined(_, _, Literal(Constant(n: Int))) =>
@@ -64,7 +61,7 @@ def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
6461
To easily know which extractors are needed, the `showExtractors` method on a
6562
`qctx.tasty.Term` returns the string representation of the extractors.
6663

67-
The method `qctx.tasty.Term.seal[T]` provides a way to go back to a
64+
The method `qctx.tasty.Term.seal` provides a way to go back to a
6865
`quoted.Expr[Any]`. Note that the type is `Expr[Any]`. Consequently, the type
6966
must be set explicitly with a checked `cast` call. If the type does not conform
7067
to it an exception will be thrown. In the code above, we could have replaced
@@ -81,7 +78,7 @@ operation expression passed while calling the `macro` below.
8178
inline def macro(param: => Boolean): Unit = ${ macroImpl('param) }
8279

8380
def macroImpl(param: Expr[Boolean])(using qctx: QuoteContext): Expr[Unit] = {
84-
import qctx.tasty.{_, given _}
81+
import qctx.tasty._
8582
import util._
8683

8784
param.unseal.underlyingArgument match {
@@ -103,7 +100,7 @@ point.
103100

104101
```scala
105102
def macroImpl()(qctx: QuoteContext): Expr[Unit] = {
106-
import qctx.tasty.{_, given _}
103+
import qctx.tasty._
107104
val pos = rootPosition
108105

109106
val path = pos.sourceFile.jpath.toString

library/src/scala/quoted/Expr.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ class Expr[+T] private[scala] {
3737
final def matches(that: Expr[Any])(using qctx: QuoteContext): Boolean =
3838
!scala.internal.quoted.Expr.unapply[Unit, Unit](this)(using that, false, qctx).isEmpty
3939

40+
/** Checked cast to a `quoted.Expr[U]` */
41+
def cast[U](using tp: scala.quoted.Type[U])(using qctx: QuoteContext): scala.quoted.Expr[U] =
42+
qctx.tasty.internal.QuotedExpr_cast[U](this)(using tp, qctx.tasty.rootContext)
43+
44+
/** View this expression `quoted.Expr[T]` as a `Term` */
45+
def unseal(using qctx: QuoteContext): qctx.tasty.Term =
46+
qctx.tasty.internal.QuotedExpr_unseal(this)(using qctx.tasty.rootContext)
47+
4048
}
4149

4250
object Expr {
@@ -108,7 +116,7 @@ object Expr {
108116
*/
109117
def ofSeq[T](xs: Seq[Expr[T]])(using tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
110118
import qctx.tasty.{_, given _}
111-
Repeated(xs.map(_.unseal).toList, tp.unseal).seal.asInstanceOf[Expr[Seq[T]]]
119+
Repeated(xs.map[Term](_.unseal).toList, tp.unseal).seal.asInstanceOf[Expr[Seq[T]]]
112120
}
113121

114122

library/src/scala/quoted/QuoteContext.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ class QuoteContext(val tasty: scala.tasty.Reflection) { self =>
3232
/** Show the fully elaborated source code representation of an expression */
3333
def show(expr: Expr[_], syntaxHighlight: SyntaxHighlight): String = {
3434
import tasty.{_, given _}
35-
expr.unseal.showWith(syntaxHighlight)
35+
expr.unseal(using this).showWith(syntaxHighlight)
3636
}
3737

3838
/** Show the fully elaborated source code representation of a type */
3939
def show(tpe: Type[_], syntaxHighlight: SyntaxHighlight): String = {
4040
import tasty.{_, given _}
41-
tpe.unseal.showWith(syntaxHighlight)
41+
tpe.unseal(using this).showWith(syntaxHighlight)
4242
}
4343

4444
/** Report an error at the position of the macro expansion */
@@ -50,7 +50,7 @@ class QuoteContext(val tasty: scala.tasty.Reflection) { self =>
5050
/** Report an error at the on the position of `expr` */
5151
def error(msg: => String, expr: Expr[Any]): Unit = {
5252
import tasty.{_, given _}
53-
tasty.error(msg, expr.unseal.pos)
53+
tasty.error(msg, expr.unseal(using this).pos)
5454
}
5555

5656
/** Report an error at the position of the macro expansion and throws a StopQuotedContext */
@@ -73,7 +73,7 @@ class QuoteContext(val tasty: scala.tasty.Reflection) { self =>
7373
/** Report a warning at the on the position of `expr` */
7474
def warning(msg: => String, expr: Expr[_]): Unit = {
7575
import tasty.{_, given _}
76-
tasty.warning(msg, expr.unseal.pos)
76+
tasty.warning(msg, expr.unseal(using this).pos)
7777
}
7878

7979
}

library/src/scala/quoted/Type.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class Type[T <: AnyKind] private[scala] {
1212
/** Show a source code like representation of this type */
1313
def show(syntaxHighlight: SyntaxHighlight)(using qctx: QuoteContext): String = qctx.show(this, syntaxHighlight)
1414

15+
/** View this expression `quoted.Type[T]` as a `TypeTree` */
16+
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
17+
qctx.tasty.internal.QuotedType_unseal(this)(using qctx.tasty.rootContext)
18+
1519
}
1620

1721
/** Some basic type tags, currently incomplete */

0 commit comments

Comments
 (0)