Skip to content

Commit d9a5409

Browse files
Merge pull request #7399 from dotty-staging/use-extension-methods-in-tasty-reflect
Use extension methods in TASTy Reflect
2 parents 87f7a14 + deb5692 commit d9a5409

File tree

127 files changed

+357
-372
lines changed

Some content is hidden

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

127 files changed

+357
-372
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
157157
if (self.symbol.is(core.Flags.JavaDefined)) Nil // FIXME should also support java packages
158158
else self.symbol.info.decls.iterator.map(definitionFromSym).toList
159159

160-
def PackageDef_symbol(self: PackageDef)(given Context): Symbol = self.symbol
161-
162160
type ClassDef = tpd.TypeDef
163161

164162
def matchClassDef(tree: Tree)(given Context): Option[ClassDef] = tree match {
@@ -171,7 +169,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
171169
def ClassDef_derived(self: ClassDef)(given Context): List[TypeTree] = ClassDef_rhs(self).derived.asInstanceOf[List[TypeTree]]
172170
def ClassDef_self(self: ClassDef)(given Context): Option[ValDef] = optional(ClassDef_rhs(self).self)
173171
def ClassDef_body(self: ClassDef)(given Context): List[Statement] = ClassDef_rhs(self).body
174-
def ClassDef_symbol(self: ClassDef)(given Context): Symbol = self.symbol.asClass
175172
private def ClassDef_rhs(self: ClassDef) = self.rhs.asInstanceOf[tpd.Template]
176173

177174
def ClassDef_copy(original: ClassDef)(name: String, constr: DefDef, parents: List[Term | TypeTree], derived: List[TypeTree], selfOpt: Option[ValDef], body: List[Statement])(given Context): ClassDef = {
@@ -187,7 +184,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
187184
}
188185

189186
def TypeDef_rhs(self: TypeDef)(given Context): TypeTree | TypeBoundsTree = self.rhs
190-
def TypeDef_symbol(self: TypeDef)(given Context): Symbol = self.symbol.asType
191187

192188
def TypeDef_apply(symbol: Symbol)(given Context): TypeDef = withDefaultPos(tpd.TypeDef(symbol.asType))
193189
def TypeDef_copy(original: TypeDef)(name: String, rhs: TypeTree | TypeBoundsTree)(given Context): TypeDef =
@@ -204,7 +200,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
204200
def DefDef_paramss(self: DefDef)(given Context): List[List[ValDef]] = self.vparamss
205201
def DefDef_returnTpt(self: DefDef)(given Context): TypeTree = self.tpt
206202
def DefDef_rhs(self: DefDef)(given Context): Option[Tree] = optional(self.rhs)
207-
def DefDef_symbol(self: DefDef)(given Context): Symbol = self.symbol.asTerm
208203

209204
def DefDef_apply(symbol: Symbol, rhsFn: List[Type] => List[List[Term]] => Option[Term])(given Context): DefDef =
210205
withDefaultPos(tpd.polyDefDef(symbol.asTerm, tparams => vparamss => rhsFn(tparams)(vparamss).getOrElse(tpd.EmptyTree)))
@@ -221,7 +216,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
221216

222217
def ValDef_tpt(self: ValDef)(given Context): TypeTree = self.tpt
223218
def ValDef_rhs(self: ValDef)(given Context): Option[Tree] = optional(self.rhs)
224-
def ValDef_symbol(self: ValDef)(given Context): Symbol = self.symbol.asTerm
225219

226220
def ValDef_apply(symbol: Symbol, rhs: Option[Term])(given Context): ValDef =
227221
tpd.ValDef(symbol.asTerm, rhs.getOrElse(tpd.EmptyTree))
@@ -238,7 +232,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
238232
case _ => None
239233
}
240234

241-
def Term_pos(self: Term)(given Context): Position = self.sourcePos
242235
def Term_tpe(self: Term)(given Context): Type = self.tpe
243236
def Term_underlyingArgument(self: Term)(given Context): Term = self.underlyingArgument
244237
def Term_underlying(self: Term)(given Context): Term = self.underlying
@@ -629,7 +622,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
629622
val NameKinds.OuterSelectName(_, levels) = self.name
630623
levels
631624
}
632-
def SelectOuter_tpe(self: SelectOuter)(given Context): Type = self.tpe.stripTypeVar
633625

634626
def SelectOuter_apply(qualifier: Term, name: String, levels: Int)(given Context): SelectOuter =
635627
withDefaultPos(tpd.Select(qualifier, NameKinds.OuterSelectName(name.toTermName, levels)))
@@ -660,8 +652,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
660652
case _ => if (x.isType) Some(x) else None
661653
}
662654

663-
def TypeTree_pos(self: TypeTree)(given Context): Position = self.sourcePos
664-
def TypeTree_symbol(self: TypeTree)(given Context): Symbol = self.symbol
665655
def TypeTree_tpe(self: TypeTree)(given Context): Type = self.tpe.stripTypeVar
666656

667657
type Inferred = tpd.TypeTree

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionImpl.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ object ReflectionImpl {
1818
val syntaxHighlight =
1919
if (ctx.settings.color.value == "always") SyntaxHighlight.ANSI
2020
else SyntaxHighlight.plain
21-
new refl.SourceCodePrinter(syntaxHighlight).showTree(reflTree)(given reflCtx)
21+
val printers = new scala.tasty.reflect.Printers(refl)
22+
new printers.SourceCodePrinter(syntaxHighlight).showTree(reflTree)(given reflCtx)
2223
}
2324
}
2425

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)

0 commit comments

Comments
 (0)