@@ -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
@@ -86,13 +87,16 @@ sealed abstract class CaptureSet extends Showable:
86
87
}
87
88
88
89
/** The subcapturing test */
89
- def subCaptures (that : CaptureSet , frozen : Boolean )(using Context ): CompareResult =
90
+ final def subCaptures (that : CaptureSet , frozen : Boolean )(using Context ): CompareResult =
90
91
subCaptures(that)(using ctx, if frozen then FrozenState else VarState ())
91
92
92
93
private def subCaptures (that : CaptureSet )(using Context , VarState ): CompareResult =
93
94
val result = that.tryInclude(elems, this )
94
- if result == CompareResult .OK then addSuper(that) else varState.abort()
95
- result
95
+ if result == CompareResult .OK then
96
+ addSuper(that)
97
+ else
98
+ varState.abort()
99
+ result
96
100
97
101
def =:= (that : CaptureSet )(using Context ): Boolean =
98
102
this .subCaptures(that, frozen = true ) == CompareResult .OK
@@ -201,7 +205,7 @@ object CaptureSet:
201
205
def addNewElems (elems : Refs , origin : CaptureSet )(using Context , VarState ): CompareResult =
202
206
CompareResult .fail(this )
203
207
204
- def addSuper (cs : CaptureSet ) = this
208
+ def addSuper (cs : CaptureSet )( using Context , VarState ) = CompareResult . OK
205
209
206
210
override def toString = elems.toString
207
211
end Const
@@ -244,7 +248,14 @@ object CaptureSet:
244
248
else
245
249
CompareResult .fail(this )
246
250
247
- def addSuper (cs : CaptureSet ) = { deps += cs; this }
251
+ def addSuper (cs : CaptureSet )(using Context , VarState ): CompareResult =
252
+ if (cs eq this ) || cs.elems.contains(defn.captureRoot.termRef) then
253
+ CompareResult .OK
254
+ else if recordDepsState() then
255
+ deps += cs
256
+ CompareResult .OK
257
+ else
258
+ CompareResult .fail(this )
248
259
249
260
override def toString = s " Var $id$elems"
250
261
end Var
@@ -254,7 +265,7 @@ object CaptureSet:
254
265
*/
255
266
class Mapped private [CaptureSet ] (
256
267
cv : Var , tm : TypeMap , variance : Int , initial : CaptureSet
257
- ) extends Var (initial.elems):
268
+ )( using @ constructorOnly ctx : Context ) extends Var (initial.elems):
258
269
addSub(cv)
259
270
addSub(initial)
260
271
val stack = if debugSets then (new Throwable ).getStackTrace().take(20 ) else null
@@ -271,21 +282,21 @@ object CaptureSet:
271
282
mapRefs(newElems, tm, variance)
272
283
else
273
284
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" )
285
+ report.warning (i " trying to add elems $newElems to $this from unrecognized source of mapped set $this$whereCreated" )
275
286
Const (newElems)
276
287
val result = super .addNewElems(added.elems, origin)
277
288
if result == CompareResult .OK then
278
289
added match
279
290
case added : Var =>
280
- added.recordDepsState()
281
- addSub(added )
291
+ if added.recordDepsState() then addSub(added )
292
+ else CompareResult .fail( this )
282
293
case _ =>
283
294
result
284
295
285
296
override def toString = s " Mapped $id( $cv, elems = $elems) "
286
297
end Mapped
287
298
288
- class BiMapped private [CaptureSet ] (cv : Var , bimap : BiTypeMap , initialElems : Refs ) extends Var (initialElems):
299
+ class BiMapped private [CaptureSet ] (cv : Var , bimap : BiTypeMap , initialElems : Refs )( using @ constructorOnly ctx : Context ) extends Var (initialElems):
289
300
addSub(cv)
290
301
291
302
override def addNewElems (newElems : Refs , origin : CaptureSet )(using Context , VarState ): CompareResult =
@@ -302,7 +313,7 @@ object CaptureSet:
302
313
end BiMapped
303
314
304
315
/** A variable with elements given at any time as { x <- cv.elems | p(x) } */
305
- class Filtered private [CaptureSet ] (cv : Var , p : CaptureRef => Boolean )
316
+ class Filtered private [CaptureSet ] (cv : Var , p : CaptureRef => Boolean )( using @ constructorOnly ctx : Context )
306
317
extends Var (cv.elems.filter(p)):
307
318
addSub(cv)
308
319
@@ -370,6 +381,12 @@ object CaptureSet:
370
381
override def putDeps (v : Var , deps : Deps ) = false
371
382
override def abort (): Unit = ()
372
383
384
+ @ sharable
385
+ object UnrecordedState extends VarState :
386
+ override def putElems (v : Var , refs : Refs ) = true
387
+ override def putDeps (v : Var , deps : Deps ) = true
388
+ override def abort (): Unit = ()
389
+
373
390
def varState (using state : VarState ): VarState = state
374
391
375
392
def ofClass (cinfo : ClassInfo , argTypes : List [Type ])(using Context ): CaptureSet =
0 commit comments