@@ -4,15 +4,15 @@ package cc
44
55import core .*
66import Types .* , Symbols .* , Flags .* , Contexts .* , Decorators .*
7- import config .Printers .{ capt , ccSetup }
7+ import config .Printers .capt
88import Annotations .Annotation
99import annotation .threadUnsafe
1010import annotation .constructorOnly
1111import annotation .internal .sharable
1212import reporting .trace
1313import printing .{Showable , Printer }
1414import printing .Texts .*
15- import util .{SimpleIdentitySet , Property , optional }, optional .{ break , ? }
15+ import util .{SimpleIdentitySet , Property }
1616import typer .ErrorReporting .Addenda
1717import util .common .alwaysTrue
1818import scala .collection .mutable
@@ -84,23 +84,13 @@ sealed abstract class CaptureSet extends Showable:
8484 final def containsRoot (using Context ) =
8585 elems.exists(_.isRootCapability)
8686
87+ /** Does this capture set disallow an addiiton of `cap`, whereas it
88+ * might allow an addition of a local root?
89+ */
8790 final def disallowsUniversal (using Context ) =
8891 if isConst then ! isUniversal && elems.exists(_.isLocalRootCapability)
8992 else asVar.noUniversal
9093
91- /** Add new elements to this capture set if allowed.
92- * @pre `newElems` is not empty and does not overlap with `this.elems`.
93- * Constant capture sets never allow to add new elements.
94- * Variables allow it if and only if the new elements can be included
95- * in all their dependent sets.
96- * @param origin The set where the elements come from, or `empty` if not known.
97- * @return CompareResult.OK if elements were added, or a conflicting
98- * capture set that prevents addition otherwise.
99- */
100- protected final def tryInclude (newElems : Refs , origin : CaptureSet )(using Context , VarState ): CompareResult =
101- (CompareResult .OK /: newElems): (r, elem) =>
102- r.andAlso(tryInclude(elem, origin))
103-
10494 /** Try to include an element in this capture set.
10595 * @param elem The element to be added
10696 * @param origin The set that originated the request, or `empty` if the request came from outside.
@@ -124,6 +114,11 @@ sealed abstract class CaptureSet extends Showable:
124114 if accountsFor(elem) then CompareResult .OK
125115 else addNewElem(elem)
126116
117+ /** Try to include all element in `refs` to this capture set. */
118+ protected final def tryInclude (newElems : Refs , origin : CaptureSet )(using Context , VarState ): CompareResult =
119+ (CompareResult .OK /: newElems): (r, elem) =>
120+ r.andAlso(tryInclude(elem, origin))
121+
127122 /** Add an element to this capture set, assuming it is not already accounted for,
128123 * and omitting any mapping or filtering.
129124 *
@@ -132,12 +127,14 @@ sealed abstract class CaptureSet extends Showable:
132127 * capture set.
133128 */
134129 protected final def addNewElem (elem : CaptureRef )(using Context , VarState ): CompareResult =
135- if elem.isRootCapability || summon[VarState ] == FrozenState then addThisElem(elem)
136- else addThisElem(elem).orElse:
137- val underlying = elem.captureSetOfInfo
138- tryInclude(underlying.elems, this ).andAlso:
139- underlying.addDependent(this )
140- CompareResult .OK
130+ if elem.isRootCapability || summon[VarState ] == FrozenState then
131+ addThisElem(elem)
132+ else
133+ addThisElem(elem).orElse:
134+ val underlying = elem.captureSetOfInfo
135+ tryInclude(underlying.elems, this ).andAlso:
136+ underlying.addDependent(this )
137+ CompareResult .OK
141138
142139 /** Add new elements one by one using `addNewElem`, abort on first failure */
143140 protected final def addNewElems (newElems : Refs )(using Context , VarState ): CompareResult =
@@ -146,7 +143,7 @@ sealed abstract class CaptureSet extends Showable:
146143
147144 /** Add a specific element, assuming it is not already accounted for,
148145 * and omitting any mapping or filtering, without possibility to backtrack
149- * to underlying capture set
146+ * to the underlying capture set.
150147 */
151148 protected def addThisElem (elem : CaptureRef )(using Context , VarState ): CompareResult
152149
@@ -157,29 +154,14 @@ sealed abstract class CaptureSet extends Showable:
157154 protected def addAsDependentTo (cs : CaptureSet )(using Context ): this .type =
158155 cs.addDependent(this )(using ctx, UnrecordedState )
159156 this
160- /*
161- /** Try to include all references of `elems` that are not yet accounted for by this
162- * capture set. Inclusion is via `addNewElems`.
163- * @param origin The set where the elements come from, or `empty` if not known.
164- * @return CompareResult.OK if all unaccounted elements could be added,
165- * capture set that prevents addition otherwise.
166- */
167- protected final def tryInclude(elems: Refs, origin: CaptureSet)(using Context, VarState): CompareResult =
168- val unaccounted = elems.filter(!accountsFor(_))
169- if unaccounted.isEmpty then CompareResult.OK
170- else tryInclude(unaccounted, origin)
171157
172- /** Equivalent to `tryInclude({elem}, origin)`, but more efficient */
173- protected final def tryInclude(elem: CaptureRef, origin: CaptureSet)(using Context, VarState): CompareResult =
174- if accountsFor(elem) then CompareResult.OK
175- else tryInclude(elem, origin)
176- */
177- /* x subsumes y if one of the following is true:
178- * - x is the same as y,
179- * - x is a this reference and y refers to a field of x
180- * - x and y are local roots and y is an enclosing root of x
181- */
182158 extension (x : CaptureRef )(using Context )
159+
160+ /* x subsumes y if one of the following is true:
161+ * - x is the same as y,
162+ * - x is a this reference and y refers to a field of x
163+ * - x is a super root of y
164+ */
183165 private def subsumes (y : CaptureRef ) =
184166 (x eq y)
185167 || x.isSuperRootOf(y)
@@ -193,8 +175,8 @@ sealed abstract class CaptureSet extends Showable:
193175 */
194176 private def isSuperRootOf (y : CaptureRef ): Boolean = x match
195177 case x : TermRef =>
196- if x.isUniversalRootCapability then true
197- else if x.isLocalRootCapability && ! y.isUniversalRootCapability then
178+ x.isUniversalRootCapability
179+ || x.isLocalRootCapability && ! y.isUniversalRootCapability && {
198180 val xowner = x.localRootOwner
199181 y match
200182 case y : TermRef =>
@@ -203,7 +185,7 @@ sealed abstract class CaptureSet extends Showable:
203185 xowner.isContainedIn(y.cls)
204186 case _ =>
205187 false
206- else false
188+ }
207189 case _ => false
208190 end extension
209191
@@ -548,14 +530,14 @@ object CaptureSet:
548530 private def levelOK (elem : CaptureRef )(using Context ): Boolean =
549531 if elem.isUniversalRootCapability then ! noUniversal
550532 else ! levelLimit.exists
551- || elem.match
552- case elem : TermRef =>
553- var sym = elem.symbol
554- if sym.isLevelOwner then sym = sym.owner
555- levelLimit.isContainedIn(sym.levelOwner)
556- case elem : ThisType =>
557- levelLimit.isContainedIn(elem.cls.levelOwner)
558- case _ => true
533+ || elem.match
534+ case elem : TermRef =>
535+ var sym = elem.symbol
536+ if sym.isLevelOwner then sym = sym.owner
537+ levelLimit.isContainedIn(sym.levelOwner)
538+ case elem : ThisType =>
539+ levelLimit.isContainedIn(elem.cls.levelOwner)
540+ case _ => true
559541
560542 def addDependent (cs : CaptureSet )(using Context , VarState ): CompareResult =
561543 if (cs eq this ) || cs.isUniversal || isConst then
@@ -1015,10 +997,8 @@ object CaptureSet:
1015997
1016998 /** The capture set of the type underlying CaptureRef */
1017999 def ofInfo (ref : CaptureRef )(using Context ): CaptureSet = ref match
1018- case ref : TermRef if ref.isRootCapability =>
1019- ref.singletonCaptureSet
1020- case _ =>
1021- ofType(ref.underlying, followResult = true )
1000+ case ref : TermRef if ref.isRootCapability => ref.singletonCaptureSet
1001+ case _ => ofType(ref.underlying, followResult = true )
10221002
10231003 /** Capture set of a type */
10241004 def ofType (tp : Type , followResult : Boolean )(using Context ): CaptureSet =
0 commit comments