Skip to content

Rename qctx to quotes #10442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion community-build/community-projects/PPrint
2 changes: 1 addition & 1 deletion community-build/community-projects/dotty-cps-async
Submodule dotty-cps-async updated 34 files
+4 −4 src/main/scala/cps/Async.scala
+7 −7 src/main/scala/cps/CpsExpr.scala
+2 −2 src/main/scala/cps/forest/ApplyTransform.scala
+5 −5 src/main/scala/cps/forest/ApplyTreeTransform.scala
+8 −8 src/main/scala/cps/forest/AssignTransform.scala
+1 −1 src/main/scala/cps/forest/AsyncTreeShifter.scala
+1 −1 src/main/scala/cps/forest/AwaitTreeTransform.scala
+5 −5 src/main/scala/cps/forest/BlockTransform.scala
+4 −4 src/main/scala/cps/forest/CpsTree.scala
+1 −1 src/main/scala/cps/forest/IdentTransform.scala
+1 −1 src/main/scala/cps/forest/IfTransform.scala
+2 −2 src/main/scala/cps/forest/ImportTransform.scala
+3 −3 src/main/scala/cps/forest/InlinedTransform.scala
+1 −1 src/main/scala/cps/forest/KnownTreeFragments.scala
+4 −4 src/main/scala/cps/forest/LambdaTreeTransform.scala
+2 −2 src/main/scala/cps/forest/MatchTreeTransform.scala
+1 −1 src/main/scala/cps/forest/NewTransform.scala
+1 −1 src/main/scala/cps/forest/ReturnTransform.scala
+7 −7 src/main/scala/cps/forest/RootTreeTransform.scala
+2 −2 src/main/scala/cps/forest/SelectTreeTransform.scala
+1 −1 src/main/scala/cps/forest/SuperTransform.scala
+1 −1 src/main/scala/cps/forest/ThisTransform.scala
+1 −1 src/main/scala/cps/forest/ThrowTransform.scala
+11 −11 src/main/scala/cps/forest/TransformUtil.scala
+6 −6 src/main/scala/cps/forest/TreeTransformScope.scala
+4 −4 src/main/scala/cps/forest/TryTransform.scala
+2 −2 src/main/scala/cps/forest/TypeApplyTransform.scala
+7 −7 src/main/scala/cps/forest/TypeApplyTreeTransform.scala
+2 −2 src/main/scala/cps/forest/TypedTransform.scala
+20 −20 src/main/scala/cps/forest/ValDefTransform.scala
+1 −1 src/main/scala/cps/forest/WhileTransform.scala
+1 −1 src/main/scala/cps/forest/application/ApplyArgRecordScope.scala
+1 −1 src/main/scala/cps/forest/application/MethodParamsDescriptorScope.scala
+1 −1 src/main/scala/cps/macroFlags/TypeMarkers.scala
8 changes: 4 additions & 4 deletions compiler/src/scala/quoted/runtime/impl/TypeImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ final class TypeImpl(val typeTree: tpd.Tree, val scopeId: Int) extends Type[?] {
case _ => false
}

/** View this expression `quoted.Type[T]` as a `TypeTree` */
def unseal(using Quotes): qctx.reflect.TypeTree =
checkScopeId(qctx.hashCode)
typeTree.asInstanceOf[qctx.reflect.TypeTree]
/** View this expression `q.Type[T]` as a `TypeTree` */
def unseal(using q: Quotes): q.reflect.TypeTree =
checkScopeId(q.hashCode)
typeTree.asInstanceOf[q.reflect.TypeTree]

def checkScopeId(expectedScopeId: Int): Unit =
if expectedScopeId != scopeId then
Expand Down
24 changes: 12 additions & 12 deletions compiler/src/scala/quoted/runtime/impl/printers/Extractors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import scala.quoted._

object Extractors {

def showTree(using Quotes)(tree: qctx.reflect.Tree): String =
new ExtractorsPrinter[qctx.type]().visitTree(tree).result()
def showTree(using Quotes)(tree: quotes.reflect.Tree): String =
new ExtractorsPrinter[quotes.type]().visitTree(tree).result()

def showType(using Quotes)(tpe: qctx.reflect.TypeRepr): String =
new ExtractorsPrinter[qctx.type]().visitType(tpe).result()
def showType(using Quotes)(tpe: quotes.reflect.TypeRepr): String =
new ExtractorsPrinter[quotes.type]().visitType(tpe).result()

def showConstant(using Quotes)(const: qctx.reflect.Constant): String =
new ExtractorsPrinter[qctx.type]().visitConstant(const).result()
def showConstant(using Quotes)(const: quotes.reflect.Constant): String =
new ExtractorsPrinter[quotes.type]().visitConstant(const).result()

def showSymbol(using Quotes)(symbol: qctx.reflect.Symbol): String =
new ExtractorsPrinter[qctx.type]().visitSymbol(symbol).result()
def showSymbol(using Quotes)(symbol: quotes.reflect.Symbol): String =
new ExtractorsPrinter[quotes.type]().visitSymbol(symbol).result()

def showFlags(using Quotes)(flags: qctx.reflect.Flags): String = {
import qctx.reflect._
def showFlags(using Quotes)(flags: quotes.reflect.Flags): String = {
import quotes.reflect._
val flagList = List.newBuilder[String]
if (flags.is(Flags.Abstract)) flagList += "Flags.Abstract"
if (flags.is(Flags.Artifact)) flagList += "Flags.Artifact"
Expand Down Expand Up @@ -57,8 +57,8 @@ object Extractors {
flagList.result().mkString(" | ")
}

private class ExtractorsPrinter[QCtx <: Quotes & Singleton](using val qctx: QCtx) { self =>
import qctx.reflect._
private class ExtractorsPrinter[Q <: Quotes & Singleton](using val quotes: Q) { self =>
import quotes.reflect._

private val sb: StringBuilder = new StringBuilder

Expand Down
22 changes: 11 additions & 11 deletions compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import scala.annotation.switch
/** Printer for fully elaborated representation of the source code */
object SourceCode {

def showTree(using Quotes)(tree: qctx.reflect.Tree)(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[qctx.type](syntaxHighlight).printTree(tree).result()
def showTree(using Quotes)(tree: quotes.reflect.Tree)(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[quotes.type](syntaxHighlight).printTree(tree).result()

def showType(using Quotes)(tpe: qctx.reflect.TypeRepr)(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[qctx.type](syntaxHighlight).printType(tpe)(using None).result()
def showType(using Quotes)(tpe: quotes.reflect.TypeRepr)(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[quotes.type](syntaxHighlight).printType(tpe)(using None).result()

def showConstant(using Quotes)(const: qctx.reflect.Constant)(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[qctx.type](syntaxHighlight).printConstant(const).result()
def showConstant(using Quotes)(const: quotes.reflect.Constant)(syntaxHighlight: SyntaxHighlight): String =
new SourceCodePrinter[quotes.type](syntaxHighlight).printConstant(const).result()

def showSymbol(using Quotes)(symbol: qctx.reflect.Symbol)(syntaxHighlight: SyntaxHighlight): String =
def showSymbol(using Quotes)(symbol: quotes.reflect.Symbol)(syntaxHighlight: SyntaxHighlight): String =
symbol.fullName

def showFlags(using Quotes)(flags: qctx.reflect.Flags)(syntaxHighlight: SyntaxHighlight): String = {
import qctx.reflect._
def showFlags(using Quotes)(flags: quotes.reflect.Flags)(syntaxHighlight: SyntaxHighlight): String = {
import quotes.reflect._
val flagList = List.newBuilder[String]
if (flags.is(Flags.Abstract)) flagList += "abstract"
if (flags.is(Flags.Artifact)) flagList += "artifact"
Expand Down Expand Up @@ -58,9 +58,9 @@ object SourceCode {
flagList.result().mkString("/*", " ", "*/")
}

private class SourceCodePrinter[QCtx <: Quotes & Singleton](syntaxHighlight: SyntaxHighlight)(using val qctx: QCtx) {
private class SourceCodePrinter[Q <: Quotes & Singleton](syntaxHighlight: SyntaxHighlight)(using val quotes: Q) {
import syntaxHighlight._
import qctx.reflect._
import quotes.reflect._

private[this] val sb: StringBuilder = new StringBuilder

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/reference/contextual/derivation-macro.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ from the signature. The body of the `derived` method is shown below:

```scala
given derived[T: Type](using Quotes) as Expr[Eq[T]] = {
import qctx.reflect._
import quotes.reflect._

val ev: Expr[Mirror.Of[T]] = Expr.summon[Mirror.Of[T]].get

Expand Down Expand Up @@ -177,7 +177,7 @@ object Eq {
}

given derived[T: Type](using q: Quotes) as Expr[Eq[T]] = {
import qctx.reflect._
import quotes.reflect._

val ev: Expr[Mirror.Of[T]] = Expr.summon[Mirror.Of[T]].get

Expand Down
16 changes: 8 additions & 8 deletions docs/docs/reference/metaprogramming/tasty-reflect.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ guarantees and may fail at macro expansion time, hence additional explicit
checks must be done.

To provide reflection capabilities in macros we need to add an implicit
parameter of type `scala.quoted.Quotes` and import `qctx.reflect._` from it in
parameter of type `scala.quoted.Quotes` and import `quotes.reflect._` from it in
the scope where it is used.

```scala
Expand All @@ -29,19 +29,19 @@ import scala.quoted._
inline def natConst(inline x: Int): Int = ${natConstImpl('{x})}

def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] = {
import qctx.reflect._
import quotes.reflect._
...
}
```

### Extractors

`import qctx.reflect._` will provide all extractors and methods on TASTy Reflect
`import quotes.reflect._` will provide all extractors and methods on TASTy Reflect
trees. For example the `Literal(_)` extractor used below.

```scala
def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] = {
import qctx.reflect._
import quotes.reflect._
val xTree: Term = Term.of(x)
xTree match {
case Inlined(_, _, Literal(Constant(n: Int))) =>
Expand All @@ -59,9 +59,9 @@ def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] = {
```

To easily know which extractors are needed, the `showExtractors` method on a
`qctx.reflect.Term` returns the string representation of the extractors.
`quotes.reflect.Term` returns the string representation of the extractors.

The methods `qctx.reflect.Term.{asExpr, asExprOf}` provide a way to go back to a `quoted.Expr`.
The methods `quotes.reflect.Term.{asExpr, asExprOf}` provide a way to go back to a `quoted.Expr`.
Note that `asExpr` returns a `Expr[Any]`.
On the other hand `asExprOf[T]` returns a `Expr[T]`, if the type does not conform to it an exception will be thrown at runtime.

Expand All @@ -75,8 +75,8 @@ such as the start line, the end line or even the source code at the expansion
point.

```scala
def macroImpl()(qctx: Quotes): Expr[Unit] = {
import qctx.reflect._
def macroImpl()(quotes: Quotes): Expr[Unit] = {
import quotes.reflect._
val pos = rootPosition

val path = pos.sourceFile.jpath.toString
Expand Down
10 changes: 4 additions & 6 deletions library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object Expr {
* Some bindings may be elided as an early optimization.
*/
def betaReduce[T](expr: Expr[T])(using Quotes): Expr[T] =
import qctx.reflect._
import quotes.reflect._
Term.betaReduce(Term.of(expr)) match
case Some(expr1) => expr1.asExpr.asInstanceOf[Expr[T]]
case _ => expr
Expand All @@ -24,7 +24,7 @@ object Expr {
* will be equivalent to `'{ $s1; $s2; ...; $e }`.
*/
def block[T](statements: List[Expr[Any]], expr: Expr[T])(using Quotes): Expr[T] = {
import qctx.reflect._
import quotes.reflect._
Block(statements.map(Term.of), Term.of(expr)).asExpr.asInstanceOf[Expr[T]]
}

Expand All @@ -40,7 +40,7 @@ object Expr {
* `'{ Seq($e1, $e2, ...) }` typed as an `Expr[Seq[T]]`
* ```
*/
def ofSeq[T](xs: Seq[Expr[T]])(using tp: Type[T], qctx: Quotes): Expr[Seq[T]] =
def ofSeq[T](xs: Seq[Expr[T]])(using Type[T])(using Quotes): Expr[Seq[T]] =
Varargs(xs)

/** Lifts this list of expressions into an expression of a list
Expand Down Expand Up @@ -208,11 +208,9 @@ object Expr {
* `None` if implicit resolution failed.
*
* @tparam T type of the implicit parameter
* @param tpe quoted type of the implicit parameter
* @param qctx current context
*/
def summon[T](using Type[T])(using Quotes): Option[Expr[T]] = {
import qctx.reflect._
import quotes.reflect._
Implicits.search(TypeRepr.of[T]) match {
case iss: ImplicitSearchSuccess => Some(iss.tree.asExpr.asInstanceOf[Expr[T]])
case isf: ImplicitSearchFailure => None
Expand Down
4 changes: 2 additions & 2 deletions library/src-bootstrapped/scala/quoted/ExprMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trait ExprMap:

/** Map subexpressions an expression `e` with a type `T` */
def transformChildren[T](e: Expr[T])(using Quotes, Type[T]): Expr[T] = {
import qctx.reflect._
import quotes.reflect._
final class MapChildren() {

def transformStatement(tree: Statement)(owner: Symbol): Statement = {
Expand Down Expand Up @@ -102,7 +102,7 @@ trait ExprMap:
type X
val expr = tree.asExpr.asInstanceOf[Expr[X]]
val t = tpe.asType.asInstanceOf[Type[X]]
val transformedExpr = transform(expr)(using qctx, t)
val transformedExpr = transform(expr)(using quotes, t)
Term.of(transformedExpr)
case _ =>
transformTermChildren(tree, tpe)(owner)
Expand Down
20 changes: 10 additions & 10 deletions library/src-bootstrapped/scala/quoted/Liftable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,70 +24,70 @@ object Liftable {
/** Default liftable for Boolean */
given BooleanLiftable[T <: Boolean] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
import quotes.reflect._
Literal(Constant.Boolean(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Byte */
given ByteLiftable[T <: Byte] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
import quotes.reflect._
Literal(Constant.Byte(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Short */
given ShortLiftable[T <: Short] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
import quotes.reflect._
Literal(Constant.Short(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Int */
given IntLiftable[T <: Int] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
import quotes.reflect._
Literal(Constant.Int(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Long */
given LongLiftable[T <: Long] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
import quotes.reflect._
Literal(Constant.Long(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Float */
given FloatLiftable[T <: Float] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
import quotes.reflect._
Literal(Constant.Float(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Double */
given DoubleLiftable[T <: Double] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
import quotes.reflect._
Literal(Constant.Double(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Char */
given CharLiftable[T <: Char] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
import quotes.reflect._
Literal(Constant.Char(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for String */
given StringLiftable[T <: String] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
import quotes.reflect._
Literal(Constant.String(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Class[T] */
given ClassLiftable[T] as Liftable[Class[T]] = new Liftable[Class[T]] {
def toExpr(x: Class[T]) = {
import qctx.reflect._
import quotes.reflect._
Ref(defn.Predef_classOf).appliedToType(TypeRepr.typeConstructorOf(x)).asExpr.asInstanceOf[Expr[Class[T]]]
}
}
Expand Down
4 changes: 2 additions & 2 deletions library/src-bootstrapped/scala/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ object Type:

/** Show a source code like representation of this type without syntax highlight */
def show[T](using Type[T])(using Quotes): String =
qctx.reflect.TypeTree.of[T].show
quotes.reflect.TypeTree.of[T].show

/** Shows the tree as fully typed source code colored with ANSI */
def showAnsiColored[T](using Type[T])(using Quotes): String =
qctx.reflect.TypeTree.of[T].showAnsiColored
quotes.reflect.TypeTree.of[T].showAnsiColored

/** Return a quoted.Type with the given type */
@compileTimeOnly("Reference to `scala.quoted.Type.of` was not handled by PickleQuotes")
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/quoted/Const.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Const {
* ```
*/
def unapply[T](expr: Expr[T])(using Quotes): Option[T] = {
import qctx.reflect._
import quotes.reflect._
def rec(tree: Term): Option[T] = tree match {
case Literal(c) => Some(c.value.asInstanceOf[T])
case Block(Nil, e) => rec(e)
Expand Down
7 changes: 5 additions & 2 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package scala.quoted

import scala.reflect.TypeTest

/** Current Quotes in scope */
inline def quotes(using q: Quotes): q.type = q

/** Quotation context provided by a macro expansion or in the scope of `scala.quoted.run`.
* Used to perform all operations on quoted `Expr` or `Type`.
*
* It contains the low-level Typed AST API metaprogramming API.
* This API does not have the static type guarantiees that `Expr` and `Type` provide.
*
* @param tasty Typed AST API. Usage: `def f(qctx: Quotes) = { import qctx.reflect._; ... }`.
* @param tasty Typed AST API. Usage: `def f(qctx: Quotes) = { import quotes.reflect._; ... }`.
*/
trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

Expand Down Expand Up @@ -3534,7 +3537,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* Usage:
* ```
* import qctx.reflect._
* import quotes.reflect._
* class MyTreeMap extends TreeMap {
* override def transformTree(tree: Tree)(owner: Symbol): Tree = ...
* }
Expand Down
Loading