Skip to content

Setup snippet compiler for scala.quoted and fix snippets #13521

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 2 commits into from
Sep 15, 2021
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 library/src/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object Expr {
* Otherwise returns `None`.
*
* Usage:
* ```scala
* ```scala sc:nocompile
* case '{ ... ${expr @ Expr(value)}: T ...} =>
* // expr: Expr[T]
* // value: T
Expand Down
84 changes: 54 additions & 30 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.reflect.TypeTest
/** Current Quotes in scope
*
* Usage:
* ```scala
* ```scala sc:nocompile
* def myExpr[T](using Quotes): Expr[T] = {
* import quotes.reflect._
* ...
Expand All @@ -31,7 +31,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Pattern matches `this` against `that`. Effectively performing a deep equality check.
* It does the equivalent of
* ```scala
* ```scala sc:nocompile
* this match
* case '{...} => true // where the contents of the pattern are the contents of `that`
* case _ => false
Expand Down Expand Up @@ -91,7 +91,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
* def f(expr: Expr[Int])(using Quotes) =
* import quotes.reflect._
* val ast: Term = expr.asTerm
* ...
* ???
* ```
*
* See `reflectModule` for full API.
Expand Down Expand Up @@ -294,15 +294,15 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

/** Tree representing a package clause in the source code
*
* ```scala
* ```scala sc:nocompile
* package foo {
* // package stats
* // package stats
* }
* ```
*
* or
*
* ```scala
* ```scala sc:nocompile
* package foo.bar
* // package stats
* ```
Expand Down Expand Up @@ -473,8 +473,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Self-type of the class
*
* ```scala
* //{
* type T
* //}
* class C { self: T =>
* ...
* ???
* }
* ```
* @syntax markdown
Expand All @@ -484,7 +487,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* ```scala
* class C {
* ... // statements
* ??? // statements
* }
* ```
* @syntax markdown
Expand Down Expand Up @@ -532,7 +535,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* Note: Non leading type parameters can be found in extension methods such as
* ```scala
* extension (a: A) def f[T]() = ...
* //{
* type A
* type T
* //}
* extension (a: A) def f[T]() = ???
* ```
* @syntax markdown
*/
Expand All @@ -543,7 +550,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* Non leading type parameters can be found in extension methods such as
* ```scala
* extension (a: A) def f[T]() = ...
* //{
* type T
* type A
* //}
* extension (a: A) def f[T]() = ???
* ```
* @syntax markdown
*/
Expand Down Expand Up @@ -1014,7 +1025,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* It may be a partially applied method:
* ```scala
* def f(x1: Int)(x2: Int) = ...
* def f(x1: Int)(x2: Int) = ???
* f(1)(2)
* ```
* - `fun` is `f(1)` in the `Apply` of `f(1)(2)`
Expand All @@ -1026,7 +1037,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* The `Apply` may be a partially applied method:
* ```scala
* def f(x1: Int)(x2: Int) = ...
* def f(x1: Int)(x2: Int) = ???
* f(1)(2)
* ```
* - `args` is `(2)` in the `Apply` of `f(1)(2)`
Expand Down Expand Up @@ -1068,9 +1079,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* It may be a partially applied method:
* ```scala
* extension (x: Int) def f[T](y: T) = ...
* //{
* type T
* //}
* extension (x: Int) def f[T](y: T) = ???
* // represented as
* // def f(x: Int)[T](y: T) = ...
* // def f(x: Int)[T](y: T) = ???
*
* 1.f[Int](2)
* // represented as
Expand All @@ -1084,9 +1098,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* The `TypeApply` may be a partially applied method:
* ```scala
* extension (x: Int) def f[T](y: T) = ...
* //{
* type T
* //}
* extension (x: Int) def f[T](y: T) = ???
* // represented as
* // def f(x: Int)[T](y: T) = ...
* // def f(x: Int)[T](y: T) = ???
*
* 1.f[Int](2)
* // represented as
Expand Down Expand Up @@ -1273,7 +1290,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** A lambda `(...) => ...` in the source code is represented as
* a local method and a closure:
*
* ```scala
* ```scala sc:nocompile
* {
* def m(...) = ...
* closure(m)
Expand All @@ -1289,7 +1306,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Methods of the module object `val Lambda` */
trait LambdaModule { this: Lambda.type =>
/** Matches a lambda definition of the form
* ```scala
* ```scala sc:nocompile
* Block((DefDef(_, _, params :: Nil, _, Some(body))) :: Nil, Closure(meth, _))
* ```
* Extracts the parameter definitions and body.
Expand All @@ -1298,7 +1315,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
def unapply(tree: Block): Option[(List[ValDef], Term)]

/** Generates a lambda with the given method type.
* ```scala
* ```scala sc:nocompile
* Block((DefDef(_, _, params :: Nil, _, Some(rhsFn(meth, paramRefs)))) :: Nil, Closure(meth, _))
* ```
* @param owner: owner of the generated `meth` symbol
Expand Down Expand Up @@ -2432,9 +2449,16 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* Usage:
* ```scala
* //{
* def f(using Quotes) = {
* val typeRepr: TypeRepr = ???
* //}
* typeRepr.asType match
* case '[t] =>
* '{ val x: t = ... }
* '{ val x: t = ??? }
* //{
* }
* //}
* ```
* @syntax markdown
*/
Expand Down Expand Up @@ -2809,12 +2833,15 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* May represent by-name parameter such as `thunk` in
* ```scala
* def log[T](thunk: =>T): T = ...
* //{
* type T
* //}
* def log[T](thunk: => T): T = ???
* ```
*
* May also represent a the return type of a parameterless method definition such as
* ```scala
* def foo: Int = ...
* def foo: Int = ???
* ```
* @syntax markdown
*/
Expand Down Expand Up @@ -3451,7 +3478,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Symbol of the definition that encloses the current splicing context.
*
* For example, the following call to `spliceOwner` would return the symbol `x`.
* ```scala
* ```scala sc:nocompile
* val x = ${ ... Symbol.spliceOwner ... }
* ```
*
Expand Down Expand Up @@ -4298,9 +4325,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* Usage:
* ```scala
* import quotes.reflect._
* class MyTreeAccumulator extends TreeAccumulator[X] {
* def foldTree(x: X, tree: Tree)(owner: Symbol): X = ...
* class MyTreeAccumulator[X] extends TreeAccumulator[X] {
* def foldTree(x: X, tree: Tree)(owner: Symbol): X = ???
* }
* ```
* @syntax markdown
Expand Down Expand Up @@ -4402,9 +4428,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* Usage:
* ```scala
* import quotes.reflect._
* class MyTraverser extends TreeTraverser {
* override def traverseTree(tree: Tree)(owner: Symbol): Unit = ...
* override def traverseTree(tree: Tree)(owner: Symbol): Unit = ???
* }
* ```
* @syntax markdown
Expand All @@ -4423,9 +4448,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*
* Usage:
* ```scala
* import quotes.reflect._
* class MyTreeMap extends TreeMap {
* override def transformTree(tree: Tree)(owner: Symbol): Tree = ...
* override def transformTree(tree: Tree)(owner: Symbol): Tree = ???
* }
* ```
* @syntax markdown
Expand Down
24 changes: 21 additions & 3 deletions library/src/scala/quoted/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ object Type:
*
* Example usage:
* ```scala
* ... match
* //{
* import scala.deriving.*
* def f(using Quotes) = {
* import quotes.reflect.*
* val expr: Expr[Any] = ???
* //}
* expr match {
* case '{ $mirrorExpr : Mirror.Sum { type MirroredLabel = label } } =>
* Type.valueOfConstant[label] // Option[String]
* }
* //{
* }
* //}
* ```
* @syntax markdown
*/
Expand All @@ -43,10 +52,19 @@ object Type:
*
* Example usage:
* ```scala
* ... match
* case '{ $mirrorExpr : Mirror.Sum { type MirroredElemLabels = label } } =>
* //{
* import scala.deriving.*
* def f(using Quotes) = {
* import quotes.reflect.*
* val expr: Expr[Any] = ???
* //}
* expr match {
* case '{ type label <: Tuple; $mirrorExpr : Mirror.Sum { type MirroredElemLabels = `label` } } =>
* Type.valueOfTuple[label] // Option[Tuple]
* }
* //{
* }
* //}
* ```
* @syntax markdown
*/
Expand Down
21 changes: 17 additions & 4 deletions library/src/scala/quoted/Varargs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ package scala.quoted
*/
object Varargs {

/** Lifts this sequence of expressions into an expression of a sequence
/**
* Lifts this sequence of expressions into an expression of a sequence
*
* Transforms a sequence of expression
* `Seq(e1, e2, ...)` where `ei: Expr[T]`
Expand All @@ -16,7 +17,14 @@ object Varargs {
*
* Usage:
* ```scala
* '{ List(${Varargs(List(1, 2, 3))}: _*) } // equivalent to '{ List(1, 2, 3) }
* //{
* def f(using Quotes) = {
* import quotes.reflect.*
* //}
* '{ List(${Varargs(List('{1}, '{2}, '{3}))}: _*) } // equivalent to '{ List(1, 2, 3) }
* //{
* }
* //}
* ```
* @syntax markdown
*/
Expand All @@ -28,13 +36,18 @@ object Varargs {
/** Matches a literal sequence of expressions and return a sequence of expressions.
*
* Usage:
* ```scala
* ```scala sc:nocompile
* //{
* object O {
* //}
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
* def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] = argsExpr match
* case Varargs(argVarargs) =>
* // argVarargs: Seq[Expr[Int]]
* ...
* ???
* //{
* }
* //}
* ```
* @syntax markdown
*/
Expand Down
3 changes: 2 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,8 @@ object Build {
"-doc-root-content", docRootFile.toString,
"-versions-dictionary-url",
"https://scala-lang.org/api/versions.json",
"-Ydocument-synthetic-types"
"-Ydocument-synthetic-types",
s"-snippet-compiler:${dottyLibRoot}/scala/quoted=compile"
) ++ (if (justAPI) Nil else Seq("-siteroot", "docs", "-Yapi-subdirectory")))

if (dottyJars.isEmpty) Def.task { streams.value.log.error("Dotty lib wasn't found") }
Expand Down
2 changes: 1 addition & 1 deletion scaladoc/src/dotty/tools/scaladoc/PathBased.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ case class PathBased[T](entries: List[PathBased.Entry[T]], projectRoot: Path):
if path.isAbsolute then
if path.startsWith(projectRoot) then get(projectRoot.relativize(path))
else None
else entries.find(_.path.forall(p => path.startsWith(p))).map(entry =>
else entries.filter(_.path.forall(p => path.startsWith(p))).maxByOption(_.path.map(_.toString.length)).map(entry =>
PathBased.Result(entry.path.fold(path)(_.relativize(path)), entry.elem)
)

Expand Down