@@ -147,7 +147,7 @@ sealed abstract class CaptureSet extends Showable:
147
147
* capture set.
148
148
*/
149
149
protected final def addNewElem (elem : CaptureRef )(using ctx : Context , vs : VarState ): CompareResult =
150
- if elem.isMaxCapability || vs.frozen != Frozen . None then
150
+ if elem.isMaxCapability || ! vs.isOpen then
151
151
addThisElem(elem)
152
152
else
153
153
addThisElem(elem).orElse:
@@ -168,7 +168,7 @@ sealed abstract class CaptureSet extends Showable:
168
168
protected def addThisElem (elem : CaptureRef )(using Context , VarState ): CompareResult
169
169
170
170
protected def addHiddenElem (elem : CaptureRef )(using ctx : Context , vs : VarState ): CompareResult =
171
- if elems.exists(_.maxSubsumes(elem, canAddHidden = vs.frozen != Frozen . All ))
171
+ if elems.exists(_.maxSubsumes(elem, canAddHidden = true ))
172
172
then CompareResult .OK
173
173
else CompareResult .Fail (this :: Nil )
174
174
@@ -177,13 +177,13 @@ sealed abstract class CaptureSet extends Showable:
177
177
178
178
/** If `cs` is a variable, add this capture set as one of its dependent sets */
179
179
protected def addAsDependentTo (cs : CaptureSet )(using Context ): this .type =
180
- cs.addDependent(this )(using ctx, UnrecordedState )
180
+ cs.addDependent(this )(using ctx, VarState . Unrecorded )
181
181
this
182
182
183
183
/** {x} <:< this where <:< is subcapturing, but treating all variables
184
184
* as frozen.
185
185
*/
186
- def accountsFor (x : CaptureRef )(using ctx : Context , vs : VarState = FrozenAllState ): Boolean =
186
+ def accountsFor (x : CaptureRef )(using ctx : Context , vs : VarState = VarState . Separate ): Boolean =
187
187
188
188
/** Like `refs.exists(p)`, but testing fresh cap instances in refs last */
189
189
def existsElem (refs : SimpleIdentitySet [CaptureRef ], p : CaptureRef => Boolean ): Boolean =
@@ -201,9 +201,9 @@ sealed abstract class CaptureSet extends Showable:
201
201
existsElem(elems, _.subsumes(x))
202
202
|| ! x.isMaxCapability
203
203
&& ! x.derivesFrom(defn.Caps_CapSet )
204
- && ! (vs.frozen == Frozen . All && x.captureSetOfInfo.containsRootCapability)
205
- // under Frozen.All , don't try to widen to cap since that might succeed with {cap} <: {cap}
206
- && x.captureSetOfInfo.subCaptures(this , Frozen . All ).isOK
204
+ && ! (vs == VarState . Separate && x.captureSetOfInfo.containsRootCapability)
205
+ // in VarState.Separate , don't try to widen to cap since that might succeed with {cap} <: {cap}
206
+ && x.captureSetOfInfo.subCaptures(this , VarState . Separate ).isOK
207
207
208
208
comparer match
209
209
case comparer : ExplainingTypeComparer => comparer.traceIndented(debugInfo)(test)
@@ -219,7 +219,7 @@ sealed abstract class CaptureSet extends Showable:
219
219
*/
220
220
def mightAccountFor (x : CaptureRef )(using Context ): Boolean =
221
221
reporting.trace(i " $this mightAccountFor $x, ${x.captureSetOfInfo}? " , show = true ):
222
- elems.exists(_.subsumes(x)(using ctx, FrozenUnrecordedHiddenState ))
222
+ elems.exists(_.subsumes(x)(using ctx, VarState . ClosedUnrecorded ))
223
223
|| ! x.isMaxCapability
224
224
&& {
225
225
val elems = x.captureSetOfInfo.elems
@@ -240,15 +240,11 @@ sealed abstract class CaptureSet extends Showable:
240
240
* be added when making this test. An attempt to add either
241
241
* will result in failure.
242
242
*/
243
- final def subCaptures (that : CaptureSet , frozen : Frozen )(using Context ): CompareResult =
244
- val state = frozen match
245
- case Frozen .None => VarState ()
246
- case Frozen .Vars => FrozenVarState ()
247
- case Frozen .All => FrozenAllState
248
- subCaptures(that)(using ctx, state)
243
+ final def subCaptures (that : CaptureSet , vs : VarState )(using Context ): CompareResult =
244
+ subCaptures(that)(using ctx, vs)
249
245
250
246
/** The subcapturing test, using a given VarState */
251
- private def subCaptures (that : CaptureSet )(using Context , VarState ): CompareResult =
247
+ def subCaptures (that : CaptureSet )(using ctx : Context , vs : VarState = VarState () ): CompareResult =
252
248
val result = that.tryInclude(elems, this )
253
249
if result.isOK then
254
250
addDependent(that)
@@ -262,16 +258,16 @@ sealed abstract class CaptureSet extends Showable:
262
258
* in a frozen state.
263
259
*/
264
260
def =:= (that : CaptureSet )(using Context ): Boolean =
265
- this .subCaptures(that, Frozen . All ).isOK
266
- && that.subCaptures(this , Frozen . All ).isOK
261
+ this .subCaptures(that, VarState . Separate ).isOK
262
+ && that.subCaptures(this , VarState . Separate ).isOK
267
263
268
264
/** The smallest capture set (via <:<) that is a superset of both
269
265
* `this` and `that`
270
266
*/
271
267
def ++ (that : CaptureSet )(using Context ): CaptureSet =
272
- if this .subCaptures(that, Frozen . All ).isOK then
268
+ if this .subCaptures(that, VarState . Separate ).isOK then
273
269
if that.isAlwaysEmpty && this .keepAlways then this else that
274
- else if that.subCaptures(this , Frozen . All ).isOK then this
270
+ else if that.subCaptures(this , VarState . Separate ).isOK then this
275
271
else if this .isConst && that.isConst then Const (this .elems ++ that.elems)
276
272
else Union (this , that)
277
273
@@ -286,8 +282,8 @@ sealed abstract class CaptureSet extends Showable:
286
282
/** The largest capture set (via <:<) that is a subset of both `this` and `that`
287
283
*/
288
284
def ** (that : CaptureSet )(using Context ): CaptureSet =
289
- if this .subCaptures(that, Frozen . Vars ).isOK then this
290
- else if that.subCaptures(this , Frozen . Vars ).isOK then that
285
+ if this .subCaptures(that, VarState . Closed () ).isOK then this
286
+ else if that.subCaptures(this , VarState . Closed () ).isOK then that
291
287
else if this .isConst && that.isConst then Const (elemIntersection(this , that))
292
288
else Intersection (this , that)
293
289
@@ -556,7 +552,7 @@ object CaptureSet:
556
552
else
557
553
// id == 108 then assert(false, i"trying to add $elem to $this")
558
554
assert(elem.isTrackableRef, elem)
559
- assert(! this .isInstanceOf [HiddenSet ] || summon[VarState ] == FrozenAllState , summon[VarState ])
555
+ assert(! this .isInstanceOf [HiddenSet ] || summon[VarState ] == VarState . Separate , summon[VarState ])
560
556
elems += elem
561
557
if elem.isRootCapability then
562
558
rootAddedHandler()
@@ -652,7 +648,8 @@ object CaptureSet:
652
648
.showing(i " solve $this = $result" , capt)
653
649
// println(i"solving var $this $approx ${approx.isConst} deps = ${deps.toList}")
654
650
val newElems = approx.elems -- elems
655
- if tryInclude(newElems, empty)(using ctx, VarState ()).isOK then
651
+ given VarState ()
652
+ if tryInclude(newElems, empty).isOK then
656
653
markSolved()
657
654
658
655
/** Mark set as solved and propagate this info to all dependent sets */
@@ -939,10 +936,6 @@ object CaptureSet:
939
936
/** A capture set variable used to record the references hidden by a Fresh.Cap instance */
940
937
class HiddenSet (initialHidden : Refs = emptySet)(using @ constructorOnly ictx : Context )
941
938
extends Var (initialElems = initialHidden):
942
- override def recordElemsState ()(using VarState ): Boolean =
943
- varState.getElems(this ) match
944
- case None => varState.putHidden(this , elems)
945
- case _ => true
946
939
947
940
/** Apply function `f` to `elems` while setting `elems` to empty for the
948
941
* duration. This is used to escape infinite recursions if two Frash.Caps
@@ -990,7 +983,7 @@ object CaptureSet:
990
983
*/
991
984
def subCapturesRange (arg1 : TypeBounds , arg2 : Type )(using Context ): Boolean = arg1 match
992
985
case TypeBounds (CapturingType (lo, loRefs), CapturingType (hi, hiRefs)) if lo =:= hi =>
993
- given VarState = VarState ()
986
+ given VarState ()
994
987
val cs2 = arg2.captureSet
995
988
hiRefs.subCaptures(cs2).isOK && cs2.subCaptures(loRefs).isOK
996
989
case _ =>
@@ -1048,19 +1041,11 @@ object CaptureSet:
1048
1041
case _ => this
1049
1042
end CompareResult
1050
1043
1051
- /** An enum indicating a Frozen degree for subCapturing tests */
1052
- enum Frozen :
1053
- case None // operations are performed in a regular VarState
1054
- case Vars // operations are performed in a FrozenVarState
1055
- case All // operations are performed in FrozenAllState
1056
-
1057
1044
/** A VarState serves as a snapshot mechanism that can undo
1058
1045
* additions of elements or super sets if an operation fails
1059
1046
*/
1060
1047
class VarState :
1061
1048
1062
- def frozen : Frozen = Frozen .None
1063
-
1064
1049
/** A map from captureset variables to their elements at the time of the snapshot. */
1065
1050
protected val elemsMap : util.EqHashMap [Var , Refs ] = new util.EqHashMap
1066
1051
@@ -1089,11 +1074,19 @@ object CaptureSet:
1089
1074
*/
1090
1075
def putDeps (v : Var , deps : Deps ): Boolean = { depsMap(v) = deps; true }
1091
1076
1092
- /** Record hidden elements in elemsMap of hidden set `v`,
1077
+ /** Does this state allow additions of elements to capture set variables? */
1078
+ def isOpen = true
1079
+
1080
+ /** Add element to hidden set, recording it in elemsMap,
1093
1081
* return whether this was allowed. By default, recording is allowed
1094
- * but the special state FrozenAllState overrides this.
1082
+ * but the special state VarState.Separate overrides this.
1095
1083
*/
1096
- def putHidden (v : HiddenSet , elems : Refs ): Boolean = { elemsMap(v) = elems; true }
1084
+ def addHidden (hidden : HiddenSet , elem : CaptureRef ): Boolean =
1085
+ elemsMap.get(hidden) match
1086
+ case None => elemsMap(hidden) = hidden.elems
1087
+ case _ =>
1088
+ hidden.elems += elem
1089
+ true
1097
1090
1098
1091
/** Roll back global state to what was recorded in this VarState */
1099
1092
def rollBack (): Unit =
@@ -1107,45 +1100,48 @@ object CaptureSet:
1107
1100
if seen.add(ref) then
1108
1101
try pred finally seen -= ref
1109
1102
else false
1110
- end VarState
1111
1103
1112
- /** A class for states that do not allow to record elements or dependent sets.
1113
- * In effect this means that no new elements or dependent sets can be added
1114
- * in these states (since the previous state cannot be recorded in a snapshot)
1115
- * On the other hand, these states do allow by default Fresh.Cap instances to
1116
- * subsume arbitary types, which are then recorded in their hidden sets.
1117
- */
1118
- class FrozenVarState extends VarState :
1119
- override def frozen = Frozen .Vars
1120
- override def putElems (v : Var , refs : Refs ) = false
1121
- override def putDeps (v : Var , deps : Deps ) = false
1122
- override def putHidden (v : HiddenSet , elems : Refs ): Boolean = { elemsMap(v) = elems; true }
1104
+ object VarState :
1123
1105
1124
- @ sharable
1125
- /** A frozen state that allows a Fresh.Cap instancce to subsume a
1126
- * reference `r` only if `r` is already present in the hidden set of the instance.
1127
- * No new references can be added.
1128
- */
1129
- object FrozenAllState extends FrozenVarState :
1130
- override def frozen = Frozen .All
1131
- override def putHidden (v : HiddenSet , elems : Refs ): Boolean = false
1106
+ /** A class for states that do not allow to record elements or dependent sets.
1107
+ * In effect this means that no new elements or dependent sets can be added
1108
+ * in these states (since the previous state cannot be recorded in a snapshot)
1109
+ * On the other hand, these states do allow by default Fresh.Cap instances to
1110
+ * subsume arbitary types, which are then recorded in their hidden sets.
1111
+ */
1112
+ class Closed extends VarState :
1113
+ override def putElems (v : Var , refs : Refs ) = false
1114
+ override def putDeps (v : Var , deps : Deps ) = false
1115
+ override def isOpen = false
1116
+
1117
+ /** A closed state that allows a Fresh.Cap instance to subsume a
1118
+ * reference `r` only if `r` is already present in the hidden set of the instance.
1119
+ * No new references can be added.
1120
+ */
1121
+ @ sharable
1122
+ object Separate extends Closed :
1123
+ override def addHidden (hidden : HiddenSet , elem : CaptureRef ): Boolean = false
1132
1124
1133
- @ sharable
1134
- /** A frozen state that allows a Fresh.Cap instancce to subsume a
1135
- * reference `r` only if `r` is already present in the hidden set of the instance.
1136
- * No new references can be added.
1137
- */
1138
- object FrozenUnrecordedHiddenState extends FrozenVarState
1125
+ /** A special state that turns off recording of elements. Used only
1126
+ * in `addSub` to prevent cycles in recordings.
1127
+ */
1128
+ @ sharable
1129
+ private [CaptureSet ] object Unrecorded extends VarState :
1130
+ override def putElems (v : Var , refs : Refs ) = true
1131
+ override def putDeps (v : Var , deps : Deps ) = true
1132
+ override def rollBack (): Unit = ()
1133
+ override def addHidden (hidden : HiddenSet , elem : CaptureRef ): Boolean = true
1134
+
1135
+ /** A closed state that turns off recording of hidden elements (but allows
1136
+ * adding them). Used in `mightAccountFor`.
1137
+ */
1138
+ @ sharable
1139
+ private [CaptureSet ] object ClosedUnrecorded extends Closed :
1140
+ override def addHidden (hidden : HiddenSet , elem : CaptureRef ): Boolean = true
1139
1141
1140
- @ sharable
1141
- /** A special state that turns off recording of elements. Used only
1142
- * in `addSub` to prevent cycles in recordings.
1143
- */
1144
- private object UnrecordedState extends VarState :
1145
- override def putElems (v : Var , refs : Refs ) = true
1146
- override def putDeps (v : Var , deps : Deps ) = true
1147
- override def rollBack (): Unit = ()
1142
+ end VarState
1148
1143
1144
+ @ sharable
1149
1145
/** The current VarState, as passed by the implicit context */
1150
1146
def varState (using state : VarState ): VarState = state
1151
1147
0 commit comments