Skip to content

Commit 86970ef

Browse files
committed
Refactor: Move previously @sharable data to ccState
This might address the Timeout problems we were seeing.
1 parent 053341d commit 86970ef

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

Diff for: compiler/src/dotty/tools/dotc/cc/CCState.scala

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33
package cc
44

55
import core.*
6-
import CaptureSet.{CompareResult, CompareFailure}
6+
import CaptureSet.{CompareResult, CompareFailure, VarState}
77
import collection.mutable
88
import reporting.Message
99
import Contexts.Context
@@ -96,6 +96,22 @@ class CCState:
9696
iterCount += 1
9797
try op finally iterCount -= 1
9898

99+
// ------ Global counters -----------------------
100+
101+
/** Next CaptureSet.Var id */
102+
var varId = 0
103+
104+
/** Next root id */
105+
var rootId = 0
106+
107+
// ------ VarState singleton objects ------------
108+
// See CaptureSet.VarState creation methods for documentation
109+
110+
object Separate extends VarState.Separating
111+
object HardSeparate extends VarState.Separating
112+
object Unrecorded extends VarState.Unrecorded
113+
object ClosedUnrecorded extends VarState.ClosedUnrecorded
114+
99115
// ------ Context info accessed from companion object when isCaptureCheckingOrSetup is true
100116

101117
private var openExistentialScopes: List[MethodType] = Nil

Diff for: compiler/src/dotty/tools/dotc/cc/CaptureOps.scala

+3-7
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ import Decorators.*, NameOps.*
1010
import config.Printers.capt
1111
import util.Property.Key
1212
import tpd.*
13-
import StdNames.nme
14-
import collection.mutable
15-
import CCState.*
16-
import reporting.Message
17-
import CaptureSet.{VarState, CompareResult, CompareFailure}
13+
import CaptureSet.VarState
1814

1915
/** Attachment key for capturing type trees */
2016
private val Captures: Key[CaptureSet] = Key()
@@ -501,11 +497,11 @@ extension (tp: Type)
501497
foldOver(x, t)
502498
acc(false, tp)
503499

504-
def level(using Context): Level =
500+
def level(using Context): CCState.Level =
505501
tp match
506502
case tp: TermRef => ccState.symLevel(tp.symbol)
507503
case tp: ThisType => ccState.symLevel(tp.cls).nextInner
508-
case _ => undefinedLevel
504+
case _ => CCState.undefinedLevel
509505

510506
extension (tp: MethodType)
511507
/** A method marks an existential scope unless it is the prefix of a curried method */

Diff for: compiler/src/dotty/tools/dotc/cc/CaptureRef.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ trait CaptureRef extends TypeProxy, ValueType:
170170
* Y: CapSet^c1...CapSet^c2, x subsumes (CapSet^c2) ==> x subsumes Y
171171
* Contains[X, y] ==> X subsumes y
172172
*/
173-
final def subsumes(y: CaptureRef)(using ctx: Context, vs: VarState = VarState.Separate): Boolean =
173+
final def subsumes(y: CaptureRef)(using ctx: Context)(using vs: VarState = VarState.Separate): Boolean =
174174

175175
def subsumingRefs(x: Type, y: Type): Boolean = x match
176176
case x: CaptureRef => y match
@@ -255,7 +255,7 @@ trait CaptureRef extends TypeProxy, ValueType:
255255
* the test again with canAddHidden = true as a last effort before we
256256
* fail a comparison.
257257
*/
258-
def maxSubsumes(y: CaptureRef, canAddHidden: Boolean)(using ctx: Context, vs: VarState = VarState.Separate): Boolean =
258+
def maxSubsumes(y: CaptureRef, canAddHidden: Boolean)(using ctx: Context)(using vs: VarState = VarState.Separate): Boolean =
259259
def yIsExistential = y.stripReadOnly match
260260
case root.Result(_) =>
261261
capt.println(i"failed existential $this >: $y")

Diff for: compiler/src/dotty/tools/dotc/cc/CaptureSet.scala

+16-18
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ sealed abstract class CaptureSet extends Showable:
181181
/** {x} <:< this where <:< is subcapturing, but treating all variables
182182
* as frozen.
183183
*/
184-
def accountsFor(x: CaptureRef)(using ctx: Context, vs: VarState = VarState.Separate): Boolean =
184+
def accountsFor(x: CaptureRef)(using ctx: Context)(using vs: VarState = VarState.Separate): Boolean =
185185

186186
def debugInfo(using Context) = i"$this accountsFor $x, which has capture set ${x.captureSetOfInfo}"
187187

@@ -210,7 +210,7 @@ sealed abstract class CaptureSet extends Showable:
210210
def mightAccountFor(x: CaptureRef)(using Context): Boolean =
211211
reporting.trace(i"$this mightAccountFor $x, ${x.captureSetOfInfo}?", show = true):
212212
CCState.withCapAsRoot: // OK here since we opportunistically choose an alternative which gets checked later
213-
elems.exists(_.subsumes(x)(using ctx, VarState.ClosedUnrecorded))
213+
elems.exists(_.subsumes(x)(using ctx)(using VarState.ClosedUnrecorded))
214214
|| !x.isRootCapability
215215
&& {
216216
val elems = x.captureSetOfInfo.elems
@@ -405,8 +405,6 @@ object CaptureSet:
405405
type Vars = SimpleIdentitySet[Var]
406406
type Deps = SimpleIdentitySet[CaptureSet]
407407

408-
@sharable private var varId = 0
409-
410408
/** If set to `true`, capture stack traces that tell us where sets are created */
411409
private final val debugSets = false
412410

@@ -485,7 +483,7 @@ object CaptureSet:
485483
object Fluid extends Const(emptyRefs):
486484
override def isAlwaysEmpty(using Context) = false
487485
override def addThisElem(elem: CaptureRef)(using Context, VarState) = CompareResult.OK
488-
override def accountsFor(x: CaptureRef)(using Context, VarState): Boolean = true
486+
override def accountsFor(x: CaptureRef)(using Context)(using VarState): Boolean = true
489487
override def mightAccountFor(x: CaptureRef)(using Context): Boolean = true
490488
override def toString = "<fluid>"
491489
end Fluid
@@ -497,8 +495,9 @@ object CaptureSet:
497495

498496
/** A unique identification number for diagnostics */
499497
val id =
500-
varId += 1
501-
varId
498+
val ccs = ccState
499+
ccs.varId += 1
500+
ccs.varId
502501

503502
//assert(id != 40)
504503

@@ -1221,37 +1220,36 @@ object CaptureSet:
12211220
* reference `r` only if `r` is already present in the hidden set of the instance.
12221221
* No new references can be added.
12231222
*/
1224-
@sharable
1225-
object Separate extends Separating
1223+
def Separate(using Context): Separating = ccState.Separate
12261224

12271225
/** Like Separate but in addition we assume that `cap` never subsumes anything else.
12281226
* Used in `++` to not lose track of dependencies between function parameters.
12291227
*/
1230-
@sharable
1231-
object HardSeparate extends Separating
1228+
def HardSeparate(using Context): Separating = ccState.HardSeparate
12321229

12331230
/** A special state that turns off recording of elements. Used only
1234-
* in `addSub` to prevent cycles in recordings.
1231+
* in `addSub` to prevent cycles in recordings. Instantiated in ccState.Unrecorded.
12351232
*/
1236-
@sharable
1237-
private[CaptureSet] object Unrecorded extends VarState:
1233+
class Unrecorded extends VarState:
12381234
override def putElems(v: Var, refs: Refs) = true
12391235
override def putDeps(v: Var, deps: Deps) = true
12401236
override def rollBack(): Unit = ()
12411237
override def addHidden(hidden: HiddenSet, elem: CaptureRef)(using Context): Boolean = true
12421238
override def toString = "unrecorded varState"
12431239

1240+
def Unrecorded(using Context): Unrecorded = ccState.Unrecorded
1241+
12441242
/** A closed state that turns off recording of hidden elements (but allows
1245-
* adding them). Used in `mightAccountFor`.
1243+
* adding them). Used in `mightAccountFor`. Instantiated in ccState.ClosedUnrecorded.
12461244
*/
1247-
@sharable
1248-
private[CaptureSet] object ClosedUnrecorded extends Closed:
1245+
class ClosedUnrecorded extends Closed:
12491246
override def addHidden(hidden: HiddenSet, elem: CaptureRef)(using Context): Boolean = true
12501247
override def toString = "closed unrecorded varState"
12511248

1249+
def ClosedUnrecorded(using Context): ClosedUnrecorded = ccState.ClosedUnrecorded
1250+
12521251
end VarState
12531252

1254-
@sharable
12551253
/** The current VarState, as passed by the implicit context */
12561254
def varState(using state: VarState): VarState = state
12571255

Diff for: compiler/src/dotty/tools/dotc/cc/root.scala

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import NameOps.isImpureFunction
1414
import reporting.Message
1515
import util.{SimpleIdentitySet, EqHashMap}
1616
import util.Spans.NoSpan
17-
import annotation.internal.sharable
17+
import annotation.constructorOnly
1818

1919
/** A module defining three kinds of root capabilities
2020
* - `cap` of kind `Global`: This is the global root capability. Among others it is
@@ -75,15 +75,14 @@ object root:
7575
case Kind.Global => false
7676
end Kind
7777

78-
@sharable private var rootId = 0
79-
8078
/** The annotation of a root instance */
81-
case class Annot(kind: Kind) extends Annotation:
79+
case class Annot(kind: Kind)(using @constructorOnly ictx: Context) extends Annotation:
8280

8381
/** id printed under -uniqid, for debugging */
8482
val id =
85-
rootId += 1
86-
rootId
83+
val ccs = ccState
84+
ccs.rootId += 1
85+
ccs.rootId
8786

8887
override def symbol(using Context) = defn.RootCapabilityAnnot
8988
override def tree(using Context) = New(symbol.typeRef, Nil)

0 commit comments

Comments
 (0)