Skip to content

Commit ba25250

Browse files
committed
Add docs
1 parent 533ebc5 commit ba25250

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

compiler/src/dotty/tools/dotc/core/StagingContext.scala

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ object StagingContext {
1111
/** A key to be used in a context property that tracks the quoteation level */
1212
private val QuotationLevel = new Property.Key[Int]
1313

14-
private val QuotationContexts = new Property.Key[List[tpd.Tree]]
14+
/** A key to be used in a context property that tracks the quoteation stack.
15+
* Stack containing the QuoteContext references recieved by the surrounding quotes.
16+
*/
17+
private val QuoteQontextStack = new Property.Key[List[tpd.Tree]]
1518

1619
/** All enclosing calls that are currently inlined, from innermost to outermost. */
1720
def level(implicit ctx: Context): Int =
@@ -21,25 +24,27 @@ object StagingContext {
2124
def quoteContext(implicit ctx: Context): Context =
2225
ctx.fresh.setProperty(QuotationLevel, level + 1)
2326

24-
/** Context with an incremented quotation level. */
27+
/** Context with an incremented quotation level and pushes a refecence to a QuoteContext on the quote context stack */
2528
def pushQuoteContext(qctxRef: tpd.Tree)(implicit ctx: Context): Context =
26-
val old = ctx.property(QuotationContexts).getOrElse(List.empty)
29+
val old = ctx.property(QuoteQontextStack).getOrElse(List.empty)
2730
ctx.fresh.setProperty(QuotationLevel, level + 1)
28-
.setProperty(QuotationContexts, qctxRef :: old)
31+
.setProperty(QuoteQontextStack, qctxRef :: old)
2932

3033
/** Context with a decremented quotation level. */
3134
def spliceContext(implicit ctx: Context): Context =
3235
ctx.fresh.setProperty(QuotationLevel, level - 1)
3336

37+
/** Context with a decremented quotation level and pops the Some of top of the quote context stack or None if the stack is empty.
38+
* The quotation stack could be empty if we are in a top level splice or an eroneous splice directly witin a top level splice.
39+
*/
3440
def popQuoteContext()(implicit ctx: Context): (Option[tpd.Tree], Context) =
3541
val ctx1 = ctx.fresh.setProperty(QuotationLevel, level - 1)
3642
val head =
37-
ctx.property(QuotationContexts) match
43+
ctx.property(QuoteQontextStack) match
3844
case Some(x :: xs) =>
39-
ctx1.setProperty(QuotationContexts, xs)
45+
ctx1.setProperty(QuoteQontextStack, xs)
4046
Some(x)
4147
case _ =>
4248
None // Splice at level 0 or lower
4349
(head, ctx1)
4450
}
45-

library/src/scala/quoted/QuoteContext.scala

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,30 @@ import scala.quoted.show.SyntaxHighlight
1212
*/
1313
class QuoteContext(val tasty: scala.tasty.Reflection) { self =>
1414

15-
// TODO
16-
type NestedContext = QuoteContext { val tasty: self.tasty.type }
15+
/** Type of a QuoteContext profided by a splice within a quote that took this context.
16+
* It is only required if working with the reflection API.
17+
*
18+
* Usually it is infered by the quotes an splices typing. But sometimes it is necessary
19+
* to explicitly state that a context is nested as in the following example:
20+
*
21+
* ```scala
22+
* def run(using qctx: QuoteContext)(tree: qctx.tasty.Tree): Unit =
23+
* def nested()(using qctx.NestedContext): Expr[Int] = '{ ${ makeExpr(tree) } + 1 }
24+
* '{ ${ nested() } + 2 }
25+
* def makeExpr(using qctx: QuoteContext)(tree: qctx.tasty.Tree): Expr[Int] = ???
26+
* ```
27+
*/
28+
type NestedContext = QuoteContext {
29+
val tasty: self.tasty.type
30+
}
1731

32+
/** Show the fully elaborated source code representation of an expression */
1833
def show(expr: Expr[_], syntaxHighlight: SyntaxHighlight): String = {
1934
import tasty.{_, given}
2035
expr.unseal.showWith(syntaxHighlight)
2136
}
2237

38+
/** Show the fully elaborated source code representation of a type */
2339
def show(tpe: Type[_], syntaxHighlight: SyntaxHighlight): String = {
2440
import tasty.{_, given}
2541
tpe.unseal.showWith(syntaxHighlight)

tests/pos/i8045b.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted._
2+
object Test
3+
def run(using qctx: QuoteContext)(tree: qctx.tasty.Tree): Unit =
4+
def nested()(using qctx.NestedContext): Expr[Int] =
5+
'{ ${ makeExpr(tree) } + 1 }
6+
'{ ${ nested() } + 2 }
7+
8+
def makeExpr(using qctx: QuoteContext)(tree: qctx.tasty.Tree): Expr[Int] = ???

0 commit comments

Comments
 (0)