Skip to content

Commit ef3f7ed

Browse files
committed
Move unseal to avoid given import for extension method
1 parent b302842 commit ef3f7ed

File tree

95 files changed

+146
-152
lines changed

Some content is hidden

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

95 files changed

+146
-152
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 & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -419,22 +419,6 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
419419
// QUOTES //
420420
////////////////
421421

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 QuotedTypeAPI 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-
438422
extension TermToQuotedOps on (term: Term) {
439423
/** Convert `Term` to an `quoted.Expr[Any]` */
440424
def seal(using ctx: Context): scala.quoted.Expr[Any] =
@@ -1599,7 +1583,8 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
15991583
///////////////
16001584

16011585
/** Returns the type (Type) of T */
1602-
def typeOf[T](using qtype: scala.quoted.Type[T], ctx: Context): Type = qtype.unseal.tpe
1586+
def typeOf[T](using qtype: scala.quoted.Type[T], ctx: Context): Type =
1587+
internal.QuotedType_unseal(qtype).tpe
16031588

16041589
/** Members of `TypeOrBounds` */
16051590
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
'{}

tests/neg-macros/i6432/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Macro {
77
inline def (sc: => StringContext).foo(args: String*): Unit = ${ impl('sc) }
88

99
def impl(sc: Expr[StringContext])(using qctx: QuoteContext) : Expr[Unit] = {
10-
import qctx.tasty.{_, given _}
10+
import qctx.tasty._
1111
sc match {
1212
case '{ StringContext(${ExprSeq(parts)}: _*) } =>
1313
for (part @ Const(s) <- parts)

tests/neg-macros/i6432b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Macro {
77
inline def (sc: => StringContext).foo(args: String*): Unit = ${ impl('sc) }
88

99
def impl(sc: Expr[StringContext])(using qctx: QuoteContext) : Expr[Unit] = {
10-
import qctx.tasty.{_, given _}
10+
import qctx.tasty._
1111
sc match {
1212
case '{ StringContext(${ExprSeq(parts)}: _*) } =>
1313
for (part @ Const(s) <- parts)

tests/neg-macros/tasty-macro-assert-1/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Asserts {
1313
${impl('cond)}
1414

1515
def impl(cond: Expr[Boolean])(using qctx: QuoteContext) : Expr[Unit] = {
16-
import qctx.tasty.{_, given _}
16+
import qctx.tasty._
1717

1818
val tree = cond.unseal
1919

tests/neg-macros/tasty-macro-assert-2/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Asserts {
1313
${ impl('cond) }
1414

1515
def impl(cond: Expr[Boolean])(using qctx: QuoteContext) : Expr[Unit] = {
16-
import qctx.tasty.{_, given _}
16+
import qctx.tasty._
1717

1818
val tree = cond.unseal
1919

tests/neg-macros/tasty-macro-error/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Macros {
55
inline def fun(x: Any): Unit = ${ impl('x) }
66

77
def impl(x: Expr[Any])(using qctx: QuoteContext) : Expr[Unit] = {
8-
import qctx.tasty.{_, given _}
8+
import qctx.tasty._
99
error("here is the the argument is " + x.unseal.underlyingArgument.show, x.unseal.underlyingArgument.pos)
1010
'{}
1111
}

tests/neg-macros/tasty-macro-positions/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Macros {
55
inline def fun(x: Any): Unit = ${ impl('x) }
66

77
def impl(x: Expr[Any])(using qctx: QuoteContext) : Expr[Unit] = {
8-
import qctx.tasty.{_, given _}
8+
import qctx.tasty._
99
val pos = x.unseal.underlyingArgument.pos
1010
error("here is the the argument is " + x.unseal.underlyingArgument.show, pos)
1111
error("here (+5) is the the argument is " + x.unseal.underlyingArgument.show, pos.sourceFile, pos.start + 5, pos.end + 5)

tests/neg-macros/tasty-string-interpolator-position-a/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object Macro {
1010
object FIntepolator {
1111

1212
def apply(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext) : Expr[String] = {
13-
import qctx.tasty.{_, given _}
13+
import qctx.tasty._
1414
error("there are no parts", strCtxExpr.unseal.underlyingArgument.pos)
1515
'{ ($strCtxExpr).s($argsExpr: _*) }
1616
}

tests/neg-macros/tasty-string-interpolator-position-b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macro {
99

1010
object FIntepolator {
1111
def apply(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext) : Expr[String] = {
12-
import qctx.tasty.{_, given _}
12+
import qctx.tasty._
1313
error("there are no args", argsExpr.unseal.underlyingArgument.pos)
1414
'{ ($strCtxExpr).s($argsExpr: _*) }
1515
}

tests/neg-staging/i5941/macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Lens {
1313

1414
def impl[S: Type, T: Type](getter: Expr[S => T])(using qctx: QuoteContext): Expr[Lens[S, T]] = {
1515
implicit val toolbox: scala.quoted.staging.Toolbox = scala.quoted.staging.Toolbox.make(this.getClass.getClassLoader)
16-
import qctx.tasty.{_, given _}
16+
import qctx.tasty._
1717
import util._
1818
// obj.copy(field = value)
1919
def setterBody(obj: Expr[S], value: Expr[T], field: String): Expr[S] =

tests/neg/i7919.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted._
22

33
object Test {
44
def staged[T](using qctx: QuoteContext) = {
5-
import qctx.tasty.{_, given _}
5+
import qctx.tasty._
66
given typeT as quoted.Type[T] // error
77
val tTypeTree = typeT.unseal
88
val tt = typeOf[T]

tests/pending/run/tasty-comments/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ object Macros {
88
${ impl('t) }
99

1010
def impl[T](x: Expr[T])(using qctx: QuoteContext) : Expr[Unit] = {
11-
import qctx.tasty.{_, given _}
11+
import qctx.tasty._
1212

1313
val tree = x.unseal
1414
tree.symbol.comment.map(_.raw) match {

tests/pos-macros/i6171/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object scalatest {
55
inline def assert(x: => Any): Unit = ${ assertImpl('x) }
66

77
def assertImpl(x: Expr[Any])(using qctx: QuoteContext) : Expr[Unit] = {
8-
import qctx.tasty.{_, given _}
8+
import qctx.tasty._
99
x.unseal.underlyingArgument
1010
'{ () }
1111
}

tests/pos-macros/i6535/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object scalatest {
55
inline def assert(condition: => Boolean): Unit = ${ assertImpl('condition) }
66

77
def assertImpl(cond: Expr[Boolean])(using qctx: QuoteContext) : Expr[Unit] = {
8-
import qctx.tasty.{_, given _}
8+
import qctx.tasty._
99
import util._
1010

1111
cond.unseal.underlyingArgument match {

tests/pos-macros/i6803b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object AsObject {
1010
def unsafe(i: Int): LineNo = new LineNo(i)
1111
inline given x as LineNo = ${impl}
1212
private def impl(using qctx: QuoteContext) : Expr[LineNo] = {
13-
import qctx.tasty.{_, given _}
13+
import qctx.tasty._
1414
'{unsafe(${rootPosition.startLine})}
1515
}
1616
}

tests/pos-macros/tasty-constant-type/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Macro {
77
inline def ff[A <: Int, B <: Int]() <: AddInt[A, B] = ${ impl('[A], '[B]) }
88

99
def impl[A <: Int : Type, B <: Int : Type](a: Type[A], b: Type[B])(using qctx: QuoteContext) : Expr[AddInt[A, B]] = {
10-
import qctx.tasty.{_, given _}
10+
import qctx.tasty._
1111

1212
val ConstantType(Constant(v1: Int)) = a.unseal.tpe
1313
val ConstantType(Constant(v2: Int)) = b.unseal.tpe

tests/pos/i7204.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted._
22

33
object Foo {
44
def impl(using qctx: QuoteContext) : Unit = {
5-
import qctx.tasty.{_, given _}
5+
import qctx.tasty._
66
val Select(_, _) = (??? : Term)
77
}
88
}

tests/run-custom-args/Yretain-trees/tasty-definitions-2/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Foo {
77
${ inspectBodyImpl('i) }
88

99
def inspectBodyImpl(x: Expr[Int])(using qctx: QuoteContext) : Expr[String] = {
10-
import qctx.tasty.{_, given _}
10+
import qctx.tasty._
1111
x.unseal match {
1212
case Inlined(None, Nil, arg) => arg.symbol.tree.showExtractors
1313
case arg => arg.symbol.tree.showExtractors // TODO should all by name parameters be in an inline node?

tests/run-custom-args/Yretain-trees/tasty-definitions-3/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Foo {
77
${ inspectBodyImpl('i) }
88

99
def inspectBodyImpl(x: Expr[Int])(using qctx: QuoteContext) : Expr[String] = {
10-
import qctx.tasty.{_, given _}
10+
import qctx.tasty._
1111
x.unseal match {
1212
case Inlined(None, Nil, arg) => arg.symbol.tree.showExtractors
1313
case arg => arg.symbol.tree.showExtractors // TODO should all by name parameters be in an inline node?

tests/run-custom-args/Yretain-trees/tasty-extractors-owners/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Macros {
77
${ impl('x) }
88

99
def impl[T](x: Expr[T])(using qctx: QuoteContext) : Expr[Unit] = {
10-
import qctx.tasty.{_, given _}
10+
import qctx.tasty._
1111

1212
val buff = new StringBuilder
1313

tests/run-custom-args/Yretain-trees/tasty-load-tree-1/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object Foo {
77
${ inspectBodyImpl('i) }
88

99
def inspectBodyImpl(x: Expr[Int])(using qctx: QuoteContext) : Expr[String] = {
10-
import qctx.tasty.{_, given _}
10+
import qctx.tasty._
1111

1212
def definitionString(sym: Symbol): Expr[String] =
1313
if sym.isClassDef || sym.isDefDef || sym.isValDef then Expr(sym.tree.showExtractors)

tests/run-custom-args/Yretain-trees/tasty-load-tree-2/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object Foo {
66
${ inspectBodyImpl('i) }
77

88
def inspectBodyImpl(x: Expr[Int])(using qctx: QuoteContext) : Expr[String] = {
9-
import qctx.tasty.{_, given _}
9+
import qctx.tasty._
1010

1111
def definitionString(sym: Symbol): Expr[String] =
1212
if sym.isClassDef || sym.isDefDef || sym.isValDef then Expr(sym.tree.showExtractors)

0 commit comments

Comments
 (0)