@@ -7,6 +7,7 @@ import Types.*, Symbols.*, Flags.*, Contexts.*, Decorators.*
7
7
import config .Printers .capt
8
8
import Annotations .Annotation
9
9
import annotation .threadUnsafe
10
+ import annotation .constructorOnly
10
11
import annotation .internal .sharable
11
12
import reporting .trace
12
13
import printing .{Showable , Printer }
@@ -58,11 +59,11 @@ sealed abstract class CaptureSet extends Showable:
58
59
protected def addNewElems (newElems : Refs , origin : CaptureSet )(using Context , VarState ): CompareResult
59
60
60
61
/** If this is a variable, add `cs` as a super set */
61
- protected def addSuper (cs : CaptureSet ): this . type
62
+ protected def addSuper (cs : CaptureSet )( using Context , VarState ) : CompareResult
62
63
63
64
/** If `cs` is a variable, add this capture set as one of its super sets */
64
- protected def addSub (cs : CaptureSet ): this .type =
65
- cs.addSuper(this )
65
+ protected def addSub (cs : CaptureSet )( using Context ) : this .type =
66
+ cs.addSuper(this )( using ctx, UnrecordedState )
66
67
this
67
68
68
69
/** Try to include all references of `elems` that are not yet accounted by this
@@ -81,18 +82,20 @@ sealed abstract class CaptureSet extends Showable:
81
82
def accountsFor (x : CaptureRef )(using Context ): Boolean =
82
83
reporting.trace(i " $this accountsFor $x, ${x.captureSetOfInfo}? " , show = true ) {
83
84
elems.contains(x)
84
- || ! x.isRootCapability
85
- && x.captureSetOfInfo.subCaptures(this , frozen = true ) == CompareResult .OK
85
+ || ! x.isRootCapability && x.captureSetOfInfo.elems.forall(accountsFor)
86
86
}
87
87
88
88
/** The subcapturing test */
89
- def subCaptures (that : CaptureSet , frozen : Boolean )(using Context ): CompareResult =
89
+ final def subCaptures (that : CaptureSet , frozen : Boolean )(using Context ): CompareResult =
90
90
subCaptures(that)(using ctx, if frozen then FrozenState else VarState ())
91
91
92
92
private def subCaptures (that : CaptureSet )(using Context , VarState ): CompareResult =
93
93
val result = that.tryInclude(elems, this )
94
- if result == CompareResult .OK then addSuper(that) else varState.abort()
95
- result
94
+ if result == CompareResult .OK then
95
+ addSuper(that)
96
+ else
97
+ varState.abort()
98
+ result
96
99
97
100
def =:= (that : CaptureSet )(using Context ): Boolean =
98
101
this .subCaptures(that, frozen = true ) == CompareResult .OK
@@ -201,7 +204,7 @@ object CaptureSet:
201
204
def addNewElems (elems : Refs , origin : CaptureSet )(using Context , VarState ): CompareResult =
202
205
CompareResult .fail(this )
203
206
204
- def addSuper (cs : CaptureSet ) = this
207
+ def addSuper (cs : CaptureSet )( using Context , VarState ) = CompareResult . OK
205
208
206
209
override def toString = elems.toString
207
210
end Const
@@ -244,7 +247,14 @@ object CaptureSet:
244
247
else
245
248
CompareResult .fail(this )
246
249
247
- def addSuper (cs : CaptureSet ) = { deps += cs; this }
250
+ def addSuper (cs : CaptureSet )(using Context , VarState ): CompareResult =
251
+ if (cs eq this ) || cs.elems.contains(defn.captureRoot.termRef) then
252
+ CompareResult .OK
253
+ else if recordDepsState() then
254
+ deps += cs
255
+ CompareResult .OK
256
+ else
257
+ CompareResult .fail(this )
248
258
249
259
override def toString = s " Var $id$elems"
250
260
end Var
@@ -254,7 +264,7 @@ object CaptureSet:
254
264
*/
255
265
class Mapped private [CaptureSet ] (
256
266
cv : Var , tm : TypeMap , variance : Int , initial : CaptureSet
257
- ) extends Var (initial.elems):
267
+ )( using @ constructorOnly ctx : Context ) extends Var (initial.elems):
258
268
addSub(cv)
259
269
addSub(initial)
260
270
val stack = if debugSets then (new Throwable ).getStackTrace().take(20 ) else null
@@ -271,21 +281,21 @@ object CaptureSet:
271
281
mapRefs(newElems, tm, variance)
272
282
else
273
283
if variance <= 0 && ! origin.isConst && (origin ne initial) then
274
- report.error (i " trying to add elems $newElems to $this from unrecognized source of mapped set $this$whereCreated" )
284
+ report.warning (i " trying to add elems $newElems to $this from unrecognized source of mapped set $this$whereCreated" )
275
285
Const (newElems)
276
286
val result = super .addNewElems(added.elems, origin)
277
287
if result == CompareResult .OK then
278
288
added match
279
289
case added : Var =>
280
- added.recordDepsState()
281
- addSub(added )
290
+ if added.recordDepsState() then addSub(added )
291
+ else CompareResult .fail( this )
282
292
case _ =>
283
293
result
284
294
285
295
override def toString = s " Mapped $id( $cv, elems = $elems) "
286
296
end Mapped
287
297
288
- class BiMapped private [CaptureSet ] (cv : Var , bimap : BiTypeMap , initialElems : Refs ) extends Var (initialElems):
298
+ class BiMapped private [CaptureSet ] (cv : Var , bimap : BiTypeMap , initialElems : Refs )( using @ constructorOnly ctx : Context ) extends Var (initialElems):
289
299
addSub(cv)
290
300
291
301
override def addNewElems (newElems : Refs , origin : CaptureSet )(using Context , VarState ): CompareResult =
@@ -302,7 +312,7 @@ object CaptureSet:
302
312
end BiMapped
303
313
304
314
/** A variable with elements given at any time as { x <- cv.elems | p(x) } */
305
- class Filtered private [CaptureSet ] (cv : Var , p : CaptureRef => Boolean )
315
+ class Filtered private [CaptureSet ] (cv : Var , p : CaptureRef => Boolean )( using @ constructorOnly ctx : Context )
306
316
extends Var (cv.elems.filter(p)):
307
317
addSub(cv)
308
318
@@ -370,6 +380,12 @@ object CaptureSet:
370
380
override def putDeps (v : Var , deps : Deps ) = false
371
381
override def abort (): Unit = ()
372
382
383
+ @ sharable
384
+ object UnrecordedState extends VarState :
385
+ override def putElems (v : Var , refs : Refs ) = true
386
+ override def putDeps (v : Var , deps : Deps ) = true
387
+ override def abort (): Unit = ()
388
+
373
389
def varState (using state : VarState ): VarState = state
374
390
375
391
def ofClass (cinfo : ClassInfo , argTypes : List [Type ])(using Context ): CaptureSet =
0 commit comments