@@ -4,15 +4,15 @@ package cc
4
4
5
5
import core .*
6
6
import Types .* , Symbols .* , Flags .* , Contexts .* , Decorators .*
7
- import config .Printers .{ capt , ccSetup }
7
+ import config .Printers .capt
8
8
import Annotations .Annotation
9
9
import annotation .threadUnsafe
10
10
import annotation .constructorOnly
11
11
import annotation .internal .sharable
12
12
import reporting .trace
13
13
import printing .{Showable , Printer }
14
14
import printing .Texts .*
15
- import util .{SimpleIdentitySet , Property , optional }, optional .{ break , ? }
15
+ import util .{SimpleIdentitySet , Property }
16
16
import typer .ErrorReporting .Addenda
17
17
import util .common .alwaysTrue
18
18
import scala .collection .mutable
@@ -84,23 +84,13 @@ sealed abstract class CaptureSet extends Showable:
84
84
final def containsRoot (using Context ) =
85
85
elems.exists(_.isRootCapability)
86
86
87
+ /** Does this capture set disallow an addiiton of `cap`, whereas it
88
+ * might allow an addition of a local root?
89
+ */
87
90
final def disallowsUniversal (using Context ) =
88
91
if isConst then ! isUniversal && elems.exists(_.isLocalRootCapability)
89
92
else asVar.noUniversal
90
93
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
-
104
94
/** Try to include an element in this capture set.
105
95
* @param elem The element to be added
106
96
* @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:
124
114
if accountsFor(elem) then CompareResult .OK
125
115
else addNewElem(elem)
126
116
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
+
127
122
/** Add an element to this capture set, assuming it is not already accounted for,
128
123
* and omitting any mapping or filtering.
129
124
*
@@ -132,12 +127,14 @@ sealed abstract class CaptureSet extends Showable:
132
127
* capture set.
133
128
*/
134
129
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
141
138
142
139
/** Add new elements one by one using `addNewElem`, abort on first failure */
143
140
protected final def addNewElems (newElems : Refs )(using Context , VarState ): CompareResult =
@@ -146,7 +143,7 @@ sealed abstract class CaptureSet extends Showable:
146
143
147
144
/** Add a specific element, assuming it is not already accounted for,
148
145
* and omitting any mapping or filtering, without possibility to backtrack
149
- * to underlying capture set
146
+ * to the underlying capture set.
150
147
*/
151
148
protected def addThisElem (elem : CaptureRef )(using Context , VarState ): CompareResult
152
149
@@ -157,29 +154,14 @@ sealed abstract class CaptureSet extends Showable:
157
154
protected def addAsDependentTo (cs : CaptureSet )(using Context ): this .type =
158
155
cs.addDependent(this )(using ctx, UnrecordedState )
159
156
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)
171
157
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
- */
182
158
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
+ */
183
165
private def subsumes (y : CaptureRef ) =
184
166
(x eq y)
185
167
|| x.isSuperRootOf(y)
@@ -193,8 +175,8 @@ sealed abstract class CaptureSet extends Showable:
193
175
*/
194
176
private def isSuperRootOf (y : CaptureRef ): Boolean = x match
195
177
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 && {
198
180
val xowner = x.localRootOwner
199
181
y match
200
182
case y : TermRef =>
@@ -203,7 +185,7 @@ sealed abstract class CaptureSet extends Showable:
203
185
xowner.isContainedIn(y.cls)
204
186
case _ =>
205
187
false
206
- else false
188
+ }
207
189
case _ => false
208
190
end extension
209
191
@@ -548,14 +530,14 @@ object CaptureSet:
548
530
private def levelOK (elem : CaptureRef )(using Context ): Boolean =
549
531
if elem.isUniversalRootCapability then ! noUniversal
550
532
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
559
541
560
542
def addDependent (cs : CaptureSet )(using Context , VarState ): CompareResult =
561
543
if (cs eq this ) || cs.isUniversal || isConst then
@@ -1015,10 +997,8 @@ object CaptureSet:
1015
997
1016
998
/** The capture set of the type underlying CaptureRef */
1017
999
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 )
1022
1002
1023
1003
/** Capture set of a type */
1024
1004
def ofType (tp : Type , followResult : Boolean )(using Context ): CaptureSet =
0 commit comments