Skip to content

Commit fff7050

Browse files
committed
Move unseal to avoid given import for extension method
1 parent c5a3b5c commit fff7050

File tree

96 files changed

+147
-157
lines changed

Some content is hidden

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

96 files changed

+147
-157
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: 6 additions & 9 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))) =>
@@ -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 */

library/src/scala/tasty/Reflection.scala

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -415,26 +415,6 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
415415
type AmbiguousImplicits = internal.AmbiguousImplicits
416416

417417

418-
////////////////
419-
// QUOTES //
420-
////////////////
421-
422-
extension QuotedExprOps on (expr: scala.quoted.Expr[?]) {
423-
/** View this expression `quoted.Expr[T]` as a `Term` */
424-
def unseal(using ctx: Context): Term =
425-
internal.QuotedExpr_unseal(expr)
426-
427-
/** Checked cast to a `quoted.Expr[U]` */
428-
def cast[U](using tp: scala.quoted.Type[U], ctx: Context): scala.quoted.Expr[U] =
429-
internal.QuotedExpr_cast[U](expr)
430-
}
431-
432-
extension QuotedTypeOps on [T <: AnyKind](tpe: scala.quoted.Type[T]) {
433-
/** View this expression `quoted.Type[T]` as a `TypeTree` */
434-
def unseal(using ctx: Context): TypeTree =
435-
internal.QuotedType_unseal(tpe)
436-
}
437-
438418
//////////////
439419
// CONTEXTS //
440420
//////////////
@@ -1592,7 +1572,8 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
15921572
///////////////
15931573

15941574
/** Returns the type (Type) of T */
1595-
def typeOf[T](using qtype: scala.quoted.Type[T], ctx: Context): Type = qtype.unseal.tpe
1575+
def typeOf[T](using qtype: scala.quoted.Type[T], ctx: Context): Type =
1576+
internal.QuotedType_unseal(qtype).tpe
15961577

15971578
/** Members of `TypeOrBounds` */
15981579
extension TypeOrBoundsOps on (tpe: TypeOrBounds) {

tests/neg-macros/delegate-match-1/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.quoted.matching._
44
inline def f: Any = ${ fImpl }
55

66
private def fImpl(using qctx: QuoteContext): Expr[Unit] = {
7-
import qctx.tasty.{_, given _}
7+
import qctx.tasty._
88
searchImplicit(('[A]).unseal.tpe) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}

tests/neg-macros/delegate-match-2/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.quoted.matching._
44
inline def f: Any = ${ fImpl }
55

66
private def fImpl (using qctx: QuoteContext) : Expr[Unit] = {
7-
import qctx.tasty.{_, given _}
7+
import qctx.tasty._
88
searchImplicit(('[A]).unseal.tpe) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}

tests/neg-macros/delegate-match-3/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.quoted.matching._
44
inline def f: Any = ${ fImpl }
55

66
private def fImpl(using qctx: QuoteContext) : Expr[Unit] = {
7-
import qctx.tasty.{_, given _}
7+
import qctx.tasty._
88
searchImplicit(('[A]).unseal.tpe) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}

0 commit comments

Comments
 (0)