Skip to content

Commit 684d5cd

Browse files
committed
Streamline CCState handling
1 parent c64de22 commit 684d5cd

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

compiler/src/dotty/tools/dotc/cc/CaptureOps.scala

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,26 @@ def allowUniversalInBoxed(using Context) =
3636
/** An exception thrown if a @retains argument is not syntactically a CaptureRef */
3737
class IllegalCaptureRef(tpe: Type) extends Exception
3838

39-
/** Capture checking state, consisting of
40-
* - nestingLevels: A map associating certain symbols (the nesting level owners)
41-
8 with their ccNestingLevel
42-
* - localRoots: A map associating nesting level owners with the local roots valid
43-
* in their scopes.
44-
* - levelError: Optionally, the last pair of capture reference and capture set where
45-
* the reference could not be added to the set due to a level conflict.
46-
* The capture checking state is stored in a context property.
47-
*/
39+
/** Capture checking state, which is stored in a context property */
4840
class CCState:
41+
42+
/** Associates certain symbols (the nesting level owners) with their ccNestingLevel */
4943
val nestingLevels: mutable.HashMap[Symbol, Int] = new mutable.HashMap
44+
45+
/** Associates nesting level owners with the local roots valid in their scopes. */
5046
val localRoots: mutable.HashMap[Symbol, Symbol] = new mutable.HashMap
47+
48+
/** The last pair of capture reference and capture set where
49+
* the reference could not be added to the set due to a level conflict.
50+
*/
5151
var levelError: Option[(CaptureRef, CaptureSet)] = None
52+
end CCState
5253

5354
/** Property key for capture checking state */
54-
val ccState: Key[CCState] = Key()
55+
val ccStateKey: Key[CCState] = Key()
56+
57+
/** The currently valid CCState */
58+
def ccState(using Context) = ctx.property(ccStateKey).get
5559

5660
trait FollowAliases extends TypeMap:
5761
def mapOverFollowingAliases(t: Type): Type = t match
@@ -348,7 +352,7 @@ extension (sym: Symbol)
348352
else if sym.is(Method) then
349353
if sym.isAnonymousFunction then
350354
// Setup added anonymous functions counting as level owners to nestingLevels
351-
ctx.property(ccState).get.nestingLevels.contains(sym)
355+
ccState.nestingLevels.contains(sym)
352356
else
353357
!sym.isConstructor && !isCaseClassSynthetic
354358
else false
@@ -371,21 +375,18 @@ extension (sym: Symbol)
371375
def ccNestingLevel(using Context): Int =
372376
if sym.exists then
373377
val lowner = sym.levelOwner
374-
val cache = ctx.property(ccState).get.nestingLevels
375-
cache.getOrElseUpdate(lowner,
378+
ccState.nestingLevels.getOrElseUpdate(lowner,
376379
if lowner.isRoot then 0 else lowner.owner.ccNestingLevel + 1)
377380
else -1
378381

379382
/** Optionally, the nesting level of `sym` for the purposes of `cc`, provided
380383
* a capture checker is running.
381384
*/
382385
def ccNestingLevelOpt(using Context): Option[Int] =
383-
if ctx.property(ccState).isDefined then
384-
Some(ccNestingLevel)
385-
else None
386+
if ctx.property(ccStateKey).isDefined then Some(ccNestingLevel) else None
386387

387388
def setNestingLevel(level: Int)(using Context): Unit =
388-
ctx.property(ccState).get.nestingLevels(sym) = level
389+
ccState.nestingLevels(sym) = level
389390

390391
/** The parameter with type caps.Root in the leading term parameter section,
391392
* or NoSymbol, if none exists.
@@ -403,7 +404,7 @@ extension (sym: Symbol)
403404
def lclRoot =
404405
if owner.isTerm then owner.definedLocalRoot.orElse(newRoot)
405406
else newRoot
406-
ctx.property(ccState).get.localRoots.getOrElseUpdate(owner, lclRoot)
407+
ccState.localRoots.getOrElseUpdate(owner, lclRoot)
407408

408409
def maxNested(other: Symbol)(using Context): Symbol =
409410
if sym.ccNestingLevel < other.ccNestingLevel then other else sym

compiler/src/dotty/tools/dotc/cc/CaptureSet.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ object CaptureSet:
484484

485485
private def recordLevelError()(using Context): Unit =
486486
for elem <- triedElem do
487-
ctx.property(ccState).get.levelError = Some((elem, this))
487+
ccState.levelError = Some((elem, this))
488488

489489
private def levelOK(elem: CaptureRef)(using Context): Boolean = elem match
490490
case elem: (TermRef | ThisType) => elem.ccNestingLevel <= ownLevel
@@ -1024,7 +1024,7 @@ object CaptureSet:
10241024
def levelErrors: Addenda = new Addenda:
10251025
override def toAdd(using Context) =
10261026
for
1027-
state <- ctx.property(ccState).toList
1027+
state <- ctx.property(ccStateKey).toList
10281028
(ref, cs) <- state.levelError
10291029
yield
10301030
val levelStr = ref match

compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ object CheckCaptures:
4646

4747
enum EnvKind:
4848
case Regular // normal case
49-
case NestedInOwner // environment is a temporary one nested in the owner's environment,
49+
case NestedInOwner // environment is a temporary one nested in the owner's environment,
5050
// and does not have a different actual owner symbol
5151
// (this happens when doing box adaptation).
5252
case ClosureResult // environment is for the result of a closure
53-
case Boxed // envrionment is inside a box (in which case references are not counted)
53+
case Boxed // environment is inside a box (in which case references are not counted)
5454

5555
/** A class describing environments.
5656
* @param owner the current owner
@@ -211,7 +211,7 @@ class CheckCaptures extends Recheck, SymTransformer:
211211
if Synthetics.needsTransform(sym) then Synthetics.transform(sym, toCC = false)
212212
else super.transformSym(sym)
213213

214-
override def printingContext(ctx: Context) = ctx.withProperty(ccState, Some(new CCState))
214+
override def printingContext(ctx: Context) = ctx.withProperty(ccStateKey, Some(new CCState))
215215

216216
class CaptureChecker(ictx: Context) extends Rechecker(ictx):
217217
import ast.tpd.*
@@ -1010,7 +1010,7 @@ class CheckCaptures extends Recheck, SymTransformer:
10101010

10111011
override def checkUnit(unit: CompilationUnit)(using Context): Unit =
10121012
setup = Setup(preRecheckPhase, thisPhase, recheckDef)
1013-
inContext(ctx.withProperty(ccState, Some(state))):
1013+
inContext(ctx.withProperty(ccStateKey, Some(state))):
10141014
setup(ctx.compilationUnit.tpdTree)
10151015
//println(i"SETUP:\n${Recheck.addRecheckedTypes.transform(ctx.compilationUnit.tpdTree)}")
10161016
withCaptureSetsExplained:

0 commit comments

Comments
 (0)