Skip to content

Commit 540277b

Browse files
committed
Move level tracking into StagingLevel
1 parent 915c44c commit 540277b

File tree

9 files changed

+40
-33
lines changed

9 files changed

+40
-33
lines changed

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,21 @@ import dotty.tools.dotc.core.Contexts._
55
import dotty.tools.dotc.ast.tpd
66
import dotty.tools.dotc.util.Property
77
import dotty.tools.dotc.staging.PCPCheckAndHeal
8+
import dotty.tools.dotc.staging.StagingLevel.*
89

910
object StagingContext {
1011

11-
/** A key to be used in a context property that tracks the quotation level */
12-
private val QuotationLevel = new Property.Key[Int]
13-
1412
/** A key to be used in a context property that tracks the quotation stack.
1513
* Stack containing the Quotes references received by the surrounding quotes.
1614
*/
1715
private val QuotesStack = new Property.Key[List[tpd.Tree]]
1816

1917
private val TaggedTypes = new Property.Key[PCPCheckAndHeal.QuoteTypeTags]
2018

21-
/** All enclosing calls that are currently inlined, from innermost to outermost. */
22-
def level(using Context): Int =
23-
ctx.property(QuotationLevel).getOrElse(0)
24-
25-
/** Context with an incremented quotation level. */
26-
def quoteContext(using Context): Context =
27-
ctx.fresh.setProperty(QuotationLevel, level + 1)
28-
2919
/** Context with an incremented quotation level and pushes a reference to a Quotes on the quote context stack */
3020
def pushQuotes(qctxRef: tpd.Tree)(using Context): Context =
3121
val old = ctx.property(QuotesStack).getOrElse(List.empty)
32-
ctx.fresh.setProperty(QuotationLevel, level + 1)
33-
.setProperty(QuotesStack, qctxRef :: old)
34-
35-
/** Context with a decremented quotation level. */
36-
def spliceContext(using Context): Context =
37-
ctx.fresh.setProperty(QuotationLevel, level - 1)
22+
quoteContext.setProperty(QuotesStack, qctxRef :: old)
3823

3924
def contextWithQuoteTypeTags(taggedTypes: PCPCheckAndHeal.QuoteTypeTags)(using Context) =
4025
ctx.fresh.setProperty(TaggedTypes, taggedTypes)
@@ -46,7 +31,7 @@ object StagingContext {
4631
* The quotation stack could be empty if we are in a top level splice or an erroneous splice directly within a top level splice.
4732
*/
4833
def popQuotes()(using Context): (Option[tpd.Tree], Context) =
49-
val ctx1 = ctx.fresh.setProperty(QuotationLevel, level - 1)
34+
val ctx1 = spliceContext
5035
val head =
5136
ctx.property(QuotesStack) match
5237
case Some(x :: xs) =>

compiler/src/dotty/tools/dotc/inlines/Inliner.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import util.Spans.Span
2323
import dotty.tools.dotc.transform.Splicer
2424
import dotty.tools.dotc.transform.BetaReduce
2525
import quoted.QuoteUtils
26+
import staging.StagingLevel
2627
import scala.annotation.constructorOnly
2728

2829
/** General support for inlining */
@@ -814,7 +815,7 @@ class Inliner(val call: tpd.Tree)(using Context):
814815
val locked = ctx.typerState.ownedVars
815816
val res = cancelQuotes(constToLiteral(BetaReduce(super.typedApply(tree, pt)))) match {
816817
case res: Apply if res.symbol == defn.QuotedRuntime_exprSplice
817-
&& StagingContext.level == 0
818+
&& StagingLevel.level == 0
818819
&& !hasInliningErrors =>
819820
val expanded = expandMacro(res.args.head, tree.srcPos)
820821
transform.TreeChecker.checkMacroGeneratedTree(res, expanded)
@@ -1026,7 +1027,7 @@ class Inliner(val call: tpd.Tree)(using Context):
10261027
}
10271028

10281029
private def expandMacro(body: Tree, splicePos: SrcPos)(using Context) = {
1029-
assert(StagingContext.level == 0)
1030+
assert(StagingLevel.level == 0)
10301031
val inlinedFrom = enclosingInlineds.last
10311032
val dependencies = macroDependencies(body)
10321033
val suspendable = ctx.compilationUnit.isSuspendable

compiler/src/dotty/tools/dotc/inlines/Inlines.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import ErrorReporting.errorTree
1414
import dotty.tools.dotc.util.{SourceFile, SourcePosition, SrcPos}
1515
import parsing.Parsers.Parser
1616
import transform.{PostTyper, Inlining, CrossVersionChecks}
17+
import staging.StagingLevel
1718

1819
import collection.mutable
1920
import reporting.trace
@@ -56,7 +57,7 @@ object Inlines:
5657
case _ =>
5758
isInlineable(tree.symbol)
5859
&& !tree.tpe.widenTermRefExpr.isInstanceOf[MethodOrPoly]
59-
&& StagingContext.level == 0
60+
&& StagingLevel.level == 0
6061
&& (
6162
ctx.phase == Phases.inliningPhase
6263
|| (ctx.phase == Phases.typerPhase && needsTransparentInlining(tree))

compiler/src/dotty/tools/dotc/inlines/PrepareInlineable.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import staging.PCPCheckAndHeal
2222
import transform.SymUtils.*
2323
import config.Printers.inlining
2424
import util.Property
25+
import staging.StagingLevel
2526

2627
object PrepareInlineable {
2728
import tpd._
@@ -73,7 +74,7 @@ object PrepareInlineable {
7374
!sym.isContainedIn(inlineSym) &&
7475
!(sym.isStableMember && sym.info.widenTermRefExpr.isInstanceOf[ConstantType]) &&
7576
!sym.isInlineMethod &&
76-
(Inlines.inInlineMethod || StagingContext.level > 0)
77+
(Inlines.inInlineMethod || StagingLevel.level > 0)
7778

7879
def preTransform(tree: Tree)(using Context): Tree
7980

@@ -90,8 +91,8 @@ object PrepareInlineable {
9091
}
9192

9293
private def stagingContext(tree: Tree)(using Context): Context = tree match
93-
case tree: Apply if tree.symbol.isQuote => StagingContext.quoteContext
94-
case tree: Apply if tree.symbol.isExprSplice => StagingContext.spliceContext
94+
case tree: Apply if tree.symbol.isQuote => StagingLevel.quoteContext
95+
case tree: Apply if tree.symbol.isExprSplice => StagingLevel.spliceContext
9596
case _ => ctx
9697
}
9798

compiler/src/dotty/tools/dotc/staging/StagingLevel.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,24 @@ import scala.collection.mutable
1414

1515
object StagingLevel {
1616

17+
/** A key to be used in a context property that tracks the staging level */
18+
private val LevelKey = new Property.Key[Int]
19+
1720
/** A key to be used in a context property that caches the `levelOf` mapping */
1821
private val LevelOfKey = new Property.Key[Map[Symbol, Int]]
1922

23+
/** All enclosing calls that are currently inlined, from innermost to outermost. */
24+
def level(using Context): Int =
25+
ctx.property(LevelKey).getOrElse(0)
26+
27+
/** Context with an incremented staging level. */
28+
def quoteContext(using Context): FreshContext =
29+
ctx.fresh.setProperty(LevelKey, level + 1)
30+
31+
/** Context with a decremented staging level. */
32+
def spliceContext(using Context): FreshContext =
33+
ctx.fresh.setProperty(LevelKey, level - 1)
34+
2035
/** The quotation level of the definition of the locally defined symbol */
2136
def levelOf(sym: Symbol)(using Context): Int =
2237
ctx.property(LevelOfKey) match

compiler/src/dotty/tools/dotc/transform/Inlining.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import dotty.tools.dotc.core.StagingContext._
1313
import dotty.tools.dotc.inlines.Inlines
1414
import dotty.tools.dotc.ast.TreeMapWithImplicits
1515
import dotty.tools.dotc.core.DenotTransformers.IdentityDenotTransformer
16+
import dotty.tools.dotc.staging.StagingLevel
1617

1718
import scala.collection.mutable.ListBuffer
1819

@@ -46,10 +47,10 @@ class Inlining extends MacroTransform {
4647
def traverse(tree: Tree)(using Context): Unit =
4748
tree match
4849
case _: GenericApply if tree.symbol.isQuote =>
49-
traverseChildren(tree)(using StagingContext.quoteContext)
50+
traverseChildren(tree)(using StagingLevel.quoteContext)
5051
case _: GenericApply if tree.symbol.isExprSplice =>
51-
traverseChildren(tree)(using StagingContext.spliceContext)
52-
case tree: RefTree if !Inlines.inInlineMethod && StagingContext.level == 0 =>
52+
traverseChildren(tree)(using StagingLevel.spliceContext)
53+
case tree: RefTree if !Inlines.inInlineMethod && StagingLevel.level == 0 =>
5354
assert(!tree.symbol.isInlineMethod, tree.show)
5455
case _ =>
5556
traverseChildren(tree)
@@ -76,7 +77,7 @@ class Inlining extends MacroTransform {
7677
else if tree.symbol.is(Param) then super.transform(tree)
7778
else if
7879
!tree.symbol.isPrimaryConstructor
79-
&& StagingContext.level == 0
80+
&& StagingLevel.level == 0
8081
&& MacroAnnotations.hasMacroAnnotation(tree.symbol)
8182
then
8283
val trees = (new MacroAnnotations).expandAnnotations(tree)
@@ -98,9 +99,9 @@ class Inlining extends MacroTransform {
9899
if tree1.tpe.isError then tree1
99100
else Inlines.inlineCall(tree1)
100101
case _: GenericApply if tree.symbol.isQuote =>
101-
super.transform(tree)(using StagingContext.quoteContext)
102+
super.transform(tree)(using StagingLevel.quoteContext)
102103
case _: GenericApply if tree.symbol.isExprSplice =>
103-
super.transform(tree)(using StagingContext.spliceContext)
104+
super.transform(tree)(using StagingLevel.spliceContext)
104105
case _: PackageDef =>
105106
super.transform(tree) match
106107
case tree1: PackageDef =>

compiler/src/dotty/tools/dotc/transform/Splicing.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import dotty.tools.dotc.core.StdNames._
2323
import dotty.tools.dotc.quoted._
2424
import dotty.tools.dotc.config.ScalaRelease.*
2525
import dotty.tools.dotc.staging.PCPCheckAndHeal
26+
import dotty.tools.dotc.staging.StagingLevel.*
2627

2728
import scala.annotation.constructorOnly
2829

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import dotty.tools.dotc.core.Decorators._
1111
import dotty.tools.dotc.core.Flags._
1212
import dotty.tools.dotc.core.NameKinds.PatMatGivenVarName
1313
import dotty.tools.dotc.core.Names._
14-
import dotty.tools.dotc.core.StagingContext._
14+
import dotty.tools.dotc.core.StagingContext.*
1515
import dotty.tools.dotc.core.StdNames._
1616
import dotty.tools.dotc.core.Symbols._
1717
import dotty.tools.dotc.core.Types._
1818
import dotty.tools.dotc.inlines.PrepareInlineable
19+
import dotty.tools.dotc.staging.StagingLevel.*
1920
import dotty.tools.dotc.transform.SymUtils._
2021
import dotty.tools.dotc.typer.Implicits._
2122
import dotty.tools.dotc.typer.Inferencing._
@@ -91,7 +92,7 @@ trait QuotesAndSplices {
9192
tree.withType(UnspecifiedErrorType)
9293
}
9394
else {
94-
if (StagingContext.level == 0) {
95+
if (level == 0) {
9596
// Mark the first inline method from the context as a macro
9697
def markAsMacro(c: Context): Unit =
9798
if (c.owner eq c.outer.owner) markAsMacro(c.outer)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import config.Feature
4444
import config.Feature.{sourceVersion, migrateTo3}
4545
import config.SourceVersion._
4646
import rewrites.Rewrites.patch
47+
import staging.StagingLevel
4748
import transform.SymUtils._
4849
import transform.TypeUtils._
4950
import reporting._
@@ -2410,7 +2411,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
24102411
else typedExpr(ddef.rhs, tpt1.tpe.widenExpr)(using rhsCtx))
24112412

24122413
if sym.isInlineMethod then
2413-
if StagingContext.level > 0 then
2414+
if StagingLevel.level > 0 then
24142415
report.error("inline def cannot be within quotes", sym.sourcePos)
24152416
if sym.is(Given)
24162417
&& untpd.stripBlock(untpd.unsplice(ddef.rhs)).isInstanceOf[untpd.Function]

0 commit comments

Comments
 (0)