Skip to content

Commit ab03d87

Browse files
Merge pull request #11218 from dotty-staging/quotes-doc
Add some docs in Quotes.scala
2 parents 98bf4f8 + e8b69dc commit ab03d87

File tree

2 files changed

+150
-19
lines changed

2 files changed

+150
-19
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
153153

154154
object Import extends ImportModule:
155155
def apply(expr: Term, selectors: List[Selector]): Import =
156+
if selectors.isEmpty then throw IllegalArgumentException("Empty selectors")
156157
withDefaultPos(tpd.Import(expr, selectors))
157158
def copy(original: Tree)(expr: Term, selectors: List[Selector]): Import =
159+
if selectors.isEmpty then throw IllegalArgumentException("Empty selectors")
158160
tpd.cpy.Import(original)(expr, selectors)
159161
def unapply(tree: Import): (Term, List[Selector]) =
160162
(tree.expr, tree.selectors)

library/src/scala/quoted/Quotes.scala

Lines changed: 148 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ package scala.quoted
22

33
import scala.reflect.TypeTest
44

5-
/** Current Quotes in scope */
5+
/** Current Quotes in scope
6+
*
7+
* Usage:
8+
* ```scala
9+
* def myExpr[T](using Quotes): Expr[T] = {
10+
* import quotes.relect._
11+
* ...
12+
* }
13+
* ```
14+
*/
615
inline def quotes(using q: Quotes): q.type = q
716

817
/** Quotation context provided by a macro expansion or in the scope of `scala.quoted.staging.run`.
@@ -223,8 +232,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
223232
val Tree: TreeModule
224233

225234
/** Methods of the module object `val Tree` */
226-
trait TreeModule { this: Tree.type =>
227-
}
235+
trait TreeModule { this: Tree.type => }
228236

229237
/** Makes extension methods on `Tree` available without any imports */
230238
given TreeMethods: TreeMethods
@@ -260,7 +268,21 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
260268

261269
}
262270

263-
/** Tree representing a pacakage clause in the source code */
271+
/** Tree representing a pacakage clause in the source code
272+
*
273+
* ```scala
274+
* package foo {
275+
* // package stats
276+
* }
277+
* ```
278+
*
279+
* or
280+
*
281+
* ```scala
282+
* package foo.bar
283+
* // package stats
284+
* ```
285+
*/
264286
type PackageClause <: Tree
265287

266288
/** `TypeTest` that allows testing at runtime in a pattern match if a `Tree` is a `PackageClause` */
@@ -271,8 +293,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
271293

272294
/** Methods of the module object `val PackageClause` */
273295
trait PackageClauseModule { this: PackageClause.type =>
296+
/** Create a package clause `package pid { stats }` */
274297
def apply(pid: Ref, stats: List[Tree]): PackageClause
298+
/** Copy a package clause `package pid { stats }` */
275299
def copy(original: Tree)(pid: Ref, stats: List[Tree]): PackageClause
300+
/** Matches a package clause `package pid { stats }` and extracts the `pid` and `stats` */
276301
def unapply(tree: PackageClause): (Ref, List[Tree])
277302
}
278303

@@ -282,12 +307,17 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
282307
/** Extension methods of `PackageClause` */
283308
trait PackageClauseMethods:
284309
extension (self: PackageClause)
310+
/** Tree containing the package name */
285311
def pid: Ref
312+
/** Definitions, imports or exports within the package */
286313
def stats: List[Tree]
287314
end extension
288315
end PackageClauseMethods
289316

290-
/** Tree representing an import in the source code */
317+
/** Tree representing an import in the source code.
318+
*
319+
* See also documentation on `Selector`.
320+
*/
291321
type Import <: Statement
292322

293323
/** `TypeTest` that allows testing at runtime in a pattern match if a `Tree` is an `Import` */
@@ -298,8 +328,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
298328

299329
/** Methods of the module object `val Import` */
300330
trait ImportModule { this: Import.type =>
331+
/** Create an `Import` with the given qualifier and selectors */
301332
def apply(expr: Term, selectors: List[Selector]): Import
333+
/** Copy an `Import` with the given qualifier and selectors */
302334
def copy(original: Tree)(expr: Term, selectors: List[Selector]): Import
335+
/** Matches an `Import` and extracts the qualifier and selectors */
303336
def unapply(tree: Import): (Term, List[Selector])
304337
}
305338

@@ -309,7 +342,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
309342
/** Extension methods of `Import` */
310343
trait ImportMethods:
311344
extension (self: Import)
345+
/** Qualifier of the import */
312346
def expr: Term
347+
/** List selectors of the import
348+
*
349+
* See documentation on `Selector`
350+
*/
313351
def selectors: List[Selector]
314352
end extension
315353
end ImportMethods
@@ -327,6 +365,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
327365

328366
/** Methods of the module object `val Export` */
329367
trait ExportModule { this: Export.type =>
368+
/** Matches an `Export` and extracts the qualifier and selectors */
330369
def unapply(tree: Export): (Term, List[Selector])
331370
}
332371

@@ -336,7 +375,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
336375
/** Extension methods of `Export` */
337376
trait ExportMethods:
338377
extension (self: Export)
378+
/** Qualifier of the export */
339379
def expr: Term
380+
/** List selectors of the export
381+
*
382+
* See documentation on `Selector`
383+
*/
340384
def selectors: List[Selector]
341385
end extension
342386
end ExportMethods
@@ -367,6 +411,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
367411
/** Extension methods of `Definition` */
368412
trait DefinitionMethods:
369413
extension (self: Definition)
414+
/** Name of the definition */
370415
def name: String
371416
end extension
372417
end DefinitionMethods
@@ -395,10 +440,31 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
395440
/** Extension methods of `ClassDef` */
396441
trait ClassDefMethods:
397442
extension (self: ClassDef)
443+
/** The primary constructor of this class */
398444
def constructor: DefDef
445+
/** List of extended parent classes or traits.
446+
* The first parent is always a class.
447+
*/
399448
def parents: List[Tree /* Term | TypeTree */]
400-
def derived: List[TypeTree]
449+
/** List of derived type classes */
450+
def derived: List[TypeTree] // TODO remove? It seems these don't exist after desugaring
451+
/** Self-type of the class
452+
*
453+
* ```scala
454+
* class C { self: T =>
455+
* ...
456+
* }
457+
* ```
458+
*/
401459
def self: Option[ValDef]
460+
/** Statements within the class
461+
*
462+
* ```scala
463+
* class C {
464+
* ... // statemets
465+
* }
466+
* ```
467+
*/
402468
def body: List[Statement]
403469
end extension
404470
end ClassDefMethods
@@ -452,7 +518,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
452518
/** List of term parameter clauses */
453519
def termParamss: List[TermParamClause]
454520

455-
/** The tree of the return type of the method */
521+
/** The tree of the return type of this `def` definition */
456522
def returnTpt: TypeTree
457523

458524
/** The tree of the implementation of the method.
@@ -496,7 +562,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
496562
/** Extension methods of `ValDef` */
497563
trait ValDefMethods:
498564
extension (self: ValDef)
565+
/** The type tree of this `val` definition */
499566
def tpt: TypeTree
567+
/** The right-hand side of this `val` definition */
500568
def rhs: Option[Term]
501569
end extension
502570
end ValDefMethods
@@ -515,8 +583,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
515583
/** Methods of the module object `val TypeDef` */
516584
trait TypeDefModule { this: TypeDef.type =>
517585
def apply(symbol: Symbol): TypeDef
518-
def copy(original: Tree)(name: String, rhs: Tree /*TypeTree | TypeBoundsTree*/): TypeDef
519-
def unapply(tdef: TypeDef): (String, Tree /*TypeTree | TypeBoundsTree*/ /* TypeTree | TypeBoundsTree */)
586+
def copy(original: Tree)(name: String, rhs: Tree): TypeDef
587+
def unapply(tdef: TypeDef): (String, Tree)
520588
}
521589

522590
/** Makes extension methods on `TypeDef` available without any imports */
@@ -525,7 +593,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
525593
/** Extension methods of `TypeDef` */
526594
trait TypeDefMethods:
527595
extension (self: TypeDef)
528-
def rhs: Tree /*TypeTree | TypeBoundsTree*/
596+
/** The type bounds on the right-hand side of this `type` definition */
597+
def rhs: Tree
529598
end extension
530599
end TypeDefMethods
531600

@@ -666,6 +735,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
666735
/** Extension methods of `Ident` */
667736
trait IdentMethods:
668737
extension (self: Ident)
738+
/** Name of this `Ident` */
669739
def name: String
670740
end extension
671741
end IdentMethods
@@ -710,8 +780,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
710780
/** Extension methods of `Select` */
711781
trait SelectMethods:
712782
extension (self: Select)
783+
/** Qualifier of the `qualifier.name` */
713784
def qualifier: Term
785+
/** Name of this `Select` */
714786
def name: String
787+
/** Signature of this method */
715788
def signature: Option[Signature]
716789
end extension
717790
end SelectMethods
@@ -743,11 +816,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
743816
/** Extension methods of `Literal` */
744817
trait LiteralMethods:
745818
extension (self: Literal)
819+
/** Value of this literal */
746820
def constant: Constant
747821
end extension
748822
end LiteralMethods
749823

750-
/** Tree representing `this` in the source code */
824+
/** Tree representing `this` or `C.this` in the source code */
751825
type This <: Term
752826

753827
/** `TypeTest` that allows testing at runtime in a pattern match if a `Tree` is a `This` */
@@ -759,12 +833,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
759833
/** Methods of the module object `val This` */
760834
trait ThisModule { this: This.type =>
761835

762-
/** Create a `this[<id: String]>` */
836+
/** Create a `C.this` for `C` pointing to `cls` */
763837
def apply(cls: Symbol): This
764838

765839
def copy(original: Tree)(qual: Option[String]): This
766840

767-
/** Matches `this[<id: Option[String]>` */
841+
/** Matches `this` or `qual.this` and returns the name of `qual` */
768842
def unapply(x: This): Some[Option[String]]
769843
}
770844

@@ -774,6 +848,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
774848
/** Extension methods of `This` */
775849
trait ThisMethods:
776850
extension (self: This)
851+
/** Returns `C` if the underlying tree is of the form `C.this`
852+
*
853+
* Otherwise, return `None`.
854+
*/
777855
def id: Option[String]
778856
end extension
779857
end ThisMethods
@@ -795,7 +873,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
795873

796874
def copy(original: Tree)(tpt: TypeTree): New
797875

798-
/** Matches a `new <tpt: TypeTree>` */
876+
/** Matches `new <tpt: TypeTree>` */
799877
def unapply(x: New): Some[TypeTree]
800878
}
801879

@@ -805,6 +883,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
805883
/** Extension methods of `New` */
806884
trait NewMethods:
807885
extension (self: New)
886+
/** Returns the type tree of this `new` */
808887
def tpt: TypeTree
809888
end extension
810889
end NewMethods
@@ -836,7 +915,9 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
836915
/** Extension methods of `NamedArg` */
837916
trait NamedArgMethods:
838917
extension (self: NamedArg)
918+
/** The name part of `name = arg` */
839919
def name: String
920+
/** The argument part of `name = arg` */
840921
def value: Term
841922
end extension
842923
end NamedArgMethods
@@ -868,7 +949,27 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
868949
/** Extension methods of `Apply` */
869950
trait ApplyMethods:
870951
extension (self: Apply)
952+
/** The `fun` part of an (implicit) application like `fun(args)`
953+
*
954+
* It maybe a partially applied method:
955+
* ```scala
956+
* def f(x1: Int)(x2: Int) = ...
957+
* f(1)(2)
958+
* ```
959+
* - `fun` is `f(1)` in the `Apply` of `f(1)(2)`
960+
* - `fun` is `f` in the `Apply` of `f(1)`
961+
*/
871962
def fun: Term
963+
/** The arguments (implicitly) passed to the method
964+
*
965+
* The `Apply` maybe a partially applied method:
966+
* ```scala
967+
* def f(x1: Int)(x2: Int) = ...
968+
* f(1)(2)
969+
* ```
970+
* - `args` is `(2)` in the `Apply` of `f(1)(2)`
971+
* - `args` is `(1)` in the `Apply` of `f(1)`
972+
*/
872973
def args: List[Term]
873974
end extension
874975
end ApplyMethods
@@ -900,7 +1001,35 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
9001001
/** Extension methods of `TypeApply` */
9011002
trait TypeApplyMethods:
9021003
extension (self: TypeApply)
1004+
/** The `fun` part of an (inferred) type application like `fun[Args]`
1005+
*
1006+
* It maybe a partially applied method:
1007+
* ```scala
1008+
* extension (x: Int) def f[T](y: T) = ...
1009+
* // represented as
1010+
* // def f(x: Int)[T](y: T) = ...
1011+
*
1012+
* 1.f[Int](2)
1013+
* // represented as
1014+
* // f(1)[Int](2)
1015+
* ```
1016+
* - `fun` is `f(1)` in the `TypeApply` of `f(1)[Int]`
1017+
*/
9031018
def fun: Term
1019+
/** The (inferred) type arguments passed to the method
1020+
*
1021+
* The `TypeApply` maybe a partially applied method:
1022+
* ```scala
1023+
* extension (x: Int) def f[T](y: T) = ...
1024+
* // represented as
1025+
* // def f(x: Int)[T](y: T) = ...
1026+
*
1027+
* 1.f[Int](2)
1028+
* // represented as
1029+
* // f(1)[Int](2)
1030+
* ```
1031+
* - `fun` is `[Int]` in the `TypeApply` of `f(1)[Int]`
1032+
*/
9041033
def args: List[TypeTree]
9051034
end extension
9061035
end TypeApplyMethods
@@ -2040,11 +2169,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
20402169
/////////////////////
20412170

20422171
/** Import/Export selectors:
2043-
* * SimpleSelector: `.bar` in `import foo.bar`
2044-
* * RenameSelector: `.{bar => baz}` in `export foo.{bar => baz}`
2045-
* * OmitSelector: `.{bar => _}` in `import foo.{bar => _}`
2046-
* * GivenSelector: `.given`/`.{given T}` in `export foo.given`/`import foo.{given T}`
2047-
*/
2172+
* - SimpleSelector: `.bar` in `import foo.bar`
2173+
* - RenameSelector: `.{bar => baz}` in `export foo.{bar => baz}`
2174+
* - OmitSelector: `.{bar => _}` in `import foo.{bar => _}`
2175+
* - GivenSelector: `.given`/`.{given T}` in `export foo.given`/`import foo.{given T}`
2176+
*/
20482177
type Selector <: AnyRef
20492178

20502179
/** Module object of `type Selector` */

0 commit comments

Comments
 (0)