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
*

0 commit comments

Comments
 (0)