Skip to content

Commit 0cb392f

Browse files
committed
Use extension method in TASTy Refection
1 parent cd12e32 commit 0cb392f

File tree

118 files changed

+199
-200
lines changed

Some content is hidden

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

118 files changed

+199
-200
lines changed

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

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

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

3737
### Sealing and Unsealing
3838

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

4545
```scala
4646
def natConstImpl(x: Expr[Int])(given qctx: QuoteContext): Expr[Int] = {
47-
import qctx.tasty._
47+
import qctx.tasty.{_, given}
4848
val xTree: Term = x.unseal
4949
xTree match {
5050
case Inlined(_, _, Literal(Constant(n: Int))) =>
@@ -81,7 +81,7 @@ operation expression passed while calling the `macro` below.
8181
inline def macro(param: => Boolean): Unit = ${ macroImpl('param) }
8282

8383
def macroImpl(param: Expr[Boolean])(given qctx: QuoteContext): Expr[Unit] = {
84-
import qctx.tasty._
84+
import qctx.tasty.{_, given}
8585
import util._
8686

8787
param.unseal.underlyingArgument match {
@@ -103,7 +103,7 @@ point.
103103

104104
```scala
105105
def macroImpl()(qctx: QuoteContext): Expr[Unit] = {
106-
import qctx.tasty._
106+
import qctx.tasty.{_, given}
107107
val pos = rootPosition
108108

109109
val path = pos.sourceFile.jpath.toString

library/src-bootstrapped/dotty/internal/StringContextMacro.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ object StringContextMacro {
8282
* quotes an error if the given Expr does not contain a list of arguments
8383
*/
8484
def getArgsExprs(argsExpr: Expr[Seq[Any]])(given qctx: QuoteContext): Option[List[Expr[Any]]] = {
85-
import qctx.tasty._
85+
import qctx.tasty.{_, given}
8686
argsExpr.unseal.underlyingArgument match {
8787
case Typed(Repeated(args, _), _) =>
8888
Some(args.map(_.seal))
@@ -99,7 +99,7 @@ object StringContextMacro {
9999
* @return the Expr containing the formatted and interpolated String or an error/warning if the parameters are not correct
100100
*/
101101
private def interpolate(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(given qctx: QuoteContext): Expr[String] = {
102-
import qctx.tasty._
102+
import qctx.tasty.{_, given}
103103
val sourceFile = strCtxExpr.unseal.pos.sourceFile
104104

105105
val (partsExpr, parts) = getPartsExprs(strCtxExpr) match {
@@ -165,7 +165,7 @@ object StringContextMacro {
165165
* @return the Expr containing the formatted and interpolated String or an error/warning report if the parameters are not correct
166166
*/
167167
def interpolate(parts0 : List[String], args : List[Expr[Any]], argsExpr: Expr[Seq[Any]], reporter : Reporter)(given qctx: QuoteContext) : Expr[String] = {
168-
import qctx.tasty._
168+
import qctx.tasty.{_, given}
169169

170170
/** Checks if the number of arguments are the same as the number of formatting strings
171171
*

library/src-bootstrapped/scala/quoted/Liftable.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ object Liftable {
3232
private class PrimitiveLiftable[T <: Unit | Null | Int | Boolean | Byte | Short | Int | Long | Float | Double | Char | String] extends Liftable[T] {
3333
/** Lift a primitive value `n` into `'{ n }` */
3434
def toExpr(x: T) = (given qctx) => {
35-
import qctx.tasty._
35+
import qctx.tasty.{_, given}
3636
Literal(Constant(x)).seal.asInstanceOf[Expr[T]]
3737
}
3838
}
3939

4040
given ClassIsLiftable[T] : Liftable[Class[T]] = new Liftable[Class[T]] {
4141
/** Lift a `Class[T]` into `'{ classOf[T] }` */
4242
def toExpr(x: Class[T]) = (given qctx) => {
43-
import qctx.tasty._
43+
import qctx.tasty.{_, given}
4444
Ref(defn.Predef_classOf).appliedToType(Type(x)).seal.asInstanceOf[Expr[Class[T]]]
4545
}
4646
}

library/src/scala/internal/quoted/Expr.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object Expr {
2727
*/
2828
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: Expr[_])(implicit patternExpr: Expr[_],
2929
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] = {
30-
import qctx.tasty._
30+
import qctx.tasty.{_, given}
3131
new Matcher.QuoteMatcher[qctx.type].termMatch(scrutineeExpr.unseal, patternExpr.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
3232
}
3333

library/src/scala/internal/quoted/Matcher.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private[quoted] object Matcher {
1212

1313
private final val debug = false
1414

15-
import qctx.tasty._
15+
import qctx.tasty.{_, given}
1616
import Matching._
1717

1818
private type Env = Set[(Symbol, Symbol)]

library/src/scala/internal/quoted/Type.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object Type {
1919
*/
2020
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeType: Type[_])(implicit patternType: Type[_],
2121
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] = {
22-
import qctx.tasty._
22+
import qctx.tasty.{_, given}
2323
new Matcher.QuoteMatcher[qctx.type].typeTreeMatch(scrutineeType.unseal, patternType.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
2424
}
2525

library/src/scala/quoted/Expr.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ package quoted {
3737
* ```
3838
*/
3939
def betaReduce[F, Args <: Tuple, R, G](f: Expr[F])(given tf: TupledFunction[F, Args => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = {
40-
import qctx.tasty._
40+
import qctx.tasty.{_, given}
4141
tg.untupled(args => qctx.tasty.internal.betaReduce(f.unseal, args.toArray.toList.map(_.asInstanceOf[QuoteContext => Expr[_]](qctx).unseal)).seal.asInstanceOf[Expr[R]])
4242
}
4343

@@ -51,19 +51,19 @@ package quoted {
5151
* Note: The
5252
*/
5353
def betaReduceGiven[F, Args <: Tuple, R, G](f: Expr[F])(given tf: TupledFunction[F, (given Args) => R], tg: TupledFunction[G, TupleOfExpr[Args] => Expr[R]], qctx: QuoteContext): G = {
54-
import qctx.tasty._
54+
import qctx.tasty.{_, given}
5555
tg.untupled(args => qctx.tasty.internal.betaReduce(f.unseal, args.toArray.toList.map(_.asInstanceOf[QuoteContext => Expr[_]](qctx).unseal)).seal.asInstanceOf[Expr[R]])
5656
}
5757

5858
/** Returns a null expresssion equivalent to `'{null}` */
5959
def nullExpr: (given QuoteContext) => Expr[Null] = (given qctx) => {
60-
import qctx.tasty._
60+
import qctx.tasty.{_, given}
6161
Literal(Constant(null)).seal.asInstanceOf[Expr[Null]]
6262
}
6363

6464
/** Returns a unit expresssion equivalent to `'{}` or `'{()}` */
6565
def unitExpr: (given QuoteContext) => Expr[Unit] = (given qctx) => {
66-
import qctx.tasty._
66+
import qctx.tasty.{_, given}
6767
Literal(Constant(())).seal.asInstanceOf[Expr[Unit]]
6868
}
6969

@@ -72,7 +72,7 @@ package quoted {
7272
* will be equivalent to `'{ $s1; $s2; ...; $e }`.
7373
*/
7474
def block[T](statements: List[Expr[_]], expr: Expr[T])(given qctx: QuoteContext): Expr[T] = {
75-
import qctx.tasty._
75+
import qctx.tasty.{_, given}
7676
Block(statements.map(_.unseal), expr.unseal).seal.asInstanceOf[Expr[T]]
7777
}
7878

@@ -92,7 +92,7 @@ package quoted {
9292
* ```
9393
*/
9494
def ofSeq[T](xs: Seq[Expr[T]])(given tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
95-
import qctx.tasty._
95+
import qctx.tasty.{_, given}
9696
Repeated(xs.map(_.unseal).toList, tp.unseal).seal.asInstanceOf[Expr[Seq[T]]]
9797
}
9898

@@ -168,8 +168,8 @@ package quoted {
168168
}
169169

170170
/** Given a tuple of the form `(Expr[A1], ..., Expr[An])`, outputs a tuple `Expr[(A1, ..., An)]`. */
171-
def ofTuple[T <: Tuple: Tuple.IsMappedBy[Expr]: Type](tup: T) (given ctx: QuoteContext): Expr[Tuple.InverseMap[T, Expr]] = {
172-
import ctx.tasty._
171+
def ofTuple[T <: Tuple: Tuple.IsMappedBy[Expr]: Type](tup: T) (given qctx: QuoteContext): Expr[Tuple.InverseMap[T, Expr]] = {
172+
import qctx.tasty.{_, given}
173173
val elems: Seq[Expr[_]] = tup.asInstanceOf[Product].productIterator.toSeq.asInstanceOf[Seq[Expr[_]]]
174174
ofTuple(elems).cast[Tuple.InverseMap[T, Expr]]
175175
}

library/src/scala/quoted/QuoteContext.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,41 @@ import scala.quoted.show.SyntaxHighlight
88
* It contains the low-level Typed AST API `tasty` meta-programming API.
99
* This API does not have the static type guarantiees that `Expr` and `Type` provide.
1010
*
11-
* @param tasty Typed AST API. Usage: `def f(qctx: QuoteContext) = { import qctx.tasty._; ... }`.
11+
* @param tasty Typed AST API. Usage: `def f(qctx: QuoteContext) = { import qctx.tasty.{_, given}; ... }`.
1212
*/
1313
class QuoteContext(val tasty: scala.tasty.Reflection) {
1414

1515
def show(expr: Expr[_], syntaxHighlight: SyntaxHighlight): String = {
16-
import tasty._
16+
import tasty.{_, given}
1717
expr.unseal.show(syntaxHighlight)
1818
}
1919

2020
def show(tpe: Type[_], syntaxHighlight: SyntaxHighlight): String = {
21-
import tasty._
21+
import tasty.{_, given}
2222
tpe.unseal.show(syntaxHighlight)
2323
}
2424

2525
/** Report an error */
2626
def error(msg: => String): Unit = {
27-
import tasty._
27+
import tasty.{_, given}
2828
tasty.error(msg, rootPosition)(given rootContext)
2929
}
3030

3131
/** Report an error at the on the position of `expr` */
3232
def error(msg: => String, expr: Expr[_]): Unit = {
33-
import tasty._
33+
import tasty.{_, given}
3434
tasty.error(msg, expr.unseal.pos)(given rootContext)
3535
}
3636

3737
/** Report a warning */
3838
def warning(msg: => String): Unit = {
39-
import tasty._
39+
import tasty.{_, given}
4040
tasty.warning(msg, rootPosition)(given rootContext)
4141
}
4242

4343
/** Report a warning at the on the position of `expr` */
4444
def warning(msg: => String, expr: Expr[_]): Unit = {
45-
import tasty._
45+
import tasty.{_, given}
4646
tasty.warning(msg, expr.unseal.pos)(given rootContext)
4747
}
4848

library/src/scala/quoted/Type.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,47 @@ package quoted {
1818
object Type {
1919

2020
given UnitTag(given qctx: QuoteContext): Type[Unit] = {
21-
import qctx.tasty._
21+
import qctx.tasty.{_, given}
2222
defn.UnitType.seal.asInstanceOf[quoted.Type[Unit]]
2323
}
2424

2525
given BooleanTag(given qctx: QuoteContext): Type[Boolean] = {
26-
import qctx.tasty._
26+
import qctx.tasty.{_, given}
2727
defn.BooleanType.seal.asInstanceOf[quoted.Type[Boolean]]
2828
}
2929

3030
given ByteTag(given qctx: QuoteContext): Type[Byte] = {
31-
import qctx.tasty._
31+
import qctx.tasty.{_, given}
3232
defn.ByteType.seal.asInstanceOf[quoted.Type[Byte]]
3333
}
3434

3535
given CharTag(given qctx: QuoteContext): Type[Char] = {
36-
import qctx.tasty._
36+
import qctx.tasty.{_, given}
3737
defn.CharType.seal.asInstanceOf[quoted.Type[Char]]
3838
}
3939

4040
given ShortTag(given qctx: QuoteContext): Type[Short] = {
41-
import qctx.tasty._
41+
import qctx.tasty.{_, given}
4242
defn.ShortType.seal.asInstanceOf[quoted.Type[Short]]
4343
}
4444

4545
given IntTag(given qctx: QuoteContext): Type[Int] = {
46-
import qctx.tasty._
46+
import qctx.tasty.{_, given}
4747
defn.IntType.seal.asInstanceOf[quoted.Type[Int]]
4848
}
4949

5050
given LongTag(given qctx: QuoteContext): Type[Long] = {
51-
import qctx.tasty._
51+
import qctx.tasty.{_, given}
5252
defn.LongType.seal.asInstanceOf[quoted.Type[Long]]
5353
}
5454

5555
given FloatTag(given qctx: QuoteContext): Type[Float] = {
56-
import qctx.tasty._
56+
import qctx.tasty.{_, given}
5757
defn.FloatType.seal.asInstanceOf[quoted.Type[Float]]
5858
}
5959

6060
given DoubleTag(given qctx: QuoteContext): Type[Double] = {
61-
import qctx.tasty._
61+
import qctx.tasty.{_, given}
6262
defn.DoubleType.seal.asInstanceOf[quoted.Type[Double]]
6363
}
6464

library/src/scala/quoted/matching/Const.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package matching
1515
object Const {
1616

1717
def unapply[T](expr: Expr[T])(given qctx: QuoteContext): Option[T] = {
18-
import qctx.tasty._
18+
import qctx.tasty.{_, given}
1919
def rec(tree: Term): Option[T] = tree match {
2020
case Literal(c) => Some(c.value.asInstanceOf[T])
2121
case Block(Nil, e) => rec(e)

library/src/scala/quoted/matching/ExprSeq.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object ExprSeq {
66

77
/** Matches a literal sequence of expressions */
88
def unapply[T](expr: Expr[Seq[T]])(given qctx: QuoteContext): Option[Seq[Expr[T]]] = {
9-
import qctx.tasty._
9+
import qctx.tasty.{_, given}
1010
def rec(tree: Term): Option[Seq[Expr[T]]] = tree match {
1111
case Typed(Repeated(elems, _), _) => Some(elems.map(x => x.seal.asInstanceOf[Expr[T]]))
1212
case Block(Nil, e) => rec(e)

library/src/scala/quoted/matching/Sym.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Sym[T <: AnyKind] private[scala](val name: String, private[Sym] val id: Ob
2020
object Sym {
2121

2222
def unapply[T](expr: Expr[T])(given qctx: QuoteContext): Option[Sym[T]] = {
23-
import qctx.tasty._
23+
import qctx.tasty.{_, given}
2424
expr.unseal match {
2525
case IsIdent(ref) =>
2626
val sym = ref.symbol

library/src/scala/quoted/matching/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package object matching {
1111
* @param qctx current context
1212
*/
1313
def searchImplicitExpr[T](given tpe: Type[T], qctx: QuoteContext): Option[Expr[T]] = {
14-
import qctx.tasty._
14+
import qctx.tasty.{_, given}
1515
searchImplicit(tpe.unseal.tpe) match {
1616
case IsImplicitSearchSuccess(iss) => Some(iss.tree.seal.asInstanceOf[Expr[T]])
1717
case IsImplicitSearchFailure(isf) => None

library/src/scala/tasty/reflect/CommentOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scala.tasty.reflect
22

33
trait CommentOps extends Core {
44

5-
implicit class CommentAPI(self: Comment) {
5+
given (self: Comment) {
66

77
/** Raw comment string */
88
def raw: String = internal.Comment_raw(self)

library/src/scala/tasty/reflect/ConstantOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package reflect
33

44
trait ConstantOps extends Core {
55

6-
implicit class ConstantAPI(const: Constant) {
6+
given (const: Constant) {
77
def value: Any = internal.Constant_value(const)
88
}
99

library/src/scala/tasty/reflect/ContextOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package reflect
33

44
trait ContextOps extends Core {
55

6-
implicit class ContextAPI(self: Context) {
6+
given (self: Context) {
77
/** Returns the owner of the context */
88
def owner: Symbol = internal.Context_owner(self)
99

library/src/scala/tasty/reflect/FlagsOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scala.tasty.reflect
22

33
trait FlagsOps extends Core {
44

5-
implicit class FlagsAPI(self: Flags) {
5+
given (self: Flags) {
66

77
/** Is the given flag set a subset of this flag sets */
88
def is(that: Flags): Boolean = internal.Flags_is(self)(that)

library/src/scala/tasty/reflect/IdOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package reflect
33

44
trait IdOps extends Core {
55

6-
implicit class IdAPI(id: Id) {
6+
given (id: Id) {
77

88
/** Position in the source code */
99
def pos(given ctx: Context): Position = internal.Id_pos(id)

library/src/scala/tasty/reflect/ImplicitsOps.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trait ImplicitsOps extends Core {
1010
internal.matchImplicitSearchSuccess(isr)
1111
}
1212

13-
implicit class IsImplicitSearchSuccessAPI(self: ImplicitSearchSuccess) {
13+
given (self: ImplicitSearchSuccess) {
1414
def tree(given ctx: Context): Term = internal.ImplicitSearchSuccess_tree(self)
1515
}
1616

@@ -19,7 +19,7 @@ trait ImplicitsOps extends Core {
1919
internal.matchImplicitSearchFailure(isr)
2020
}
2121

22-
implicit class ImplicitSearchFailureAPI(self: ImplicitSearchFailure) {
22+
given (self: ImplicitSearchFailure) {
2323
def explanation(given ctx: Context): String = internal.ImplicitSearchFailure_explanation(self)
2424
}
2525

0 commit comments

Comments
 (0)