Skip to content

Commit b3038e6

Browse files
committed
stdlib: Address StrictMemorySafety warnings in Set related code.
1 parent 87db90c commit b3038e6

File tree

5 files changed

+71
-70
lines changed

5 files changed

+71
-70
lines changed

stdlib/public/core/NativeSet.swift

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ internal struct _NativeSet<Element: Hashable> {
2525
@inlinable
2626
@inline(__always)
2727
internal init() {
28-
self._storage = unsafe __RawSetStorage.empty
28+
unsafe self._storage = __RawSetStorage.empty
2929
}
3030

3131
/// Constructs a native set adopting the given storage.
3232
@inlinable
3333
@inline(__always)
3434
internal init(_ storage: __owned __RawSetStorage) {
35-
self._storage = unsafe storage
35+
unsafe self._storage = storage
3636
}
3737

3838
@inlinable
3939
internal init(capacity: Int) {
4040
if capacity == 0 {
41-
self._storage = unsafe __RawSetStorage.empty
41+
unsafe self._storage = __RawSetStorage.empty
4242
} else {
43-
self._storage = unsafe _SetStorage<Element>.allocate(capacity: capacity)
43+
unsafe self._storage = _SetStorage<Element>.allocate(capacity: capacity)
4444
}
4545
}
4646

@@ -53,10 +53,10 @@ internal struct _NativeSet<Element: Hashable> {
5353
@inlinable
5454
internal init(_ cocoa: __owned __CocoaSet, capacity: Int) {
5555
if capacity == 0 {
56-
self._storage = unsafe __RawSetStorage.empty
56+
unsafe self._storage = __RawSetStorage.empty
5757
} else {
5858
_internalInvariant(cocoa.count <= capacity)
59-
self._storage = unsafe _SetStorage<Element>.convert(cocoa, capacity: capacity)
59+
unsafe self._storage = _SetStorage<Element>.convert(cocoa, capacity: capacity)
6060
for element in cocoa {
6161
let nativeElement = _forceBridgeFromObjectiveC(element, Element.self)
6262
insertNew(nativeElement, isUnique: true)
@@ -151,10 +151,11 @@ extension _NativeSet { // Low-level lookup operations
151151
return unsafe element._rawHashValue(seed: _storage._seed)
152152
}
153153

154+
@safe
154155
@inlinable
155156
@inline(__always)
156157
internal func find(_ element: Element) -> (bucket: Bucket, found: Bool) {
157-
return find(element, hashValue: self.hashValue(for: element))
158+
return unsafe find(element, hashValue: self.hashValue(for: element))
158159
}
159160

160161
/// Search for a given element, assuming it has the specified hash value.
@@ -167,10 +168,10 @@ extension _NativeSet { // Low-level lookup operations
167168
_ element: Element,
168169
hashValue: Int
169170
) -> (bucket: Bucket, found: Bool) {
170-
let hashTable = self.hashTable
171+
let hashTable = unsafe self.hashTable
171172
var bucket = unsafe hashTable.idealBucket(forHashValue: hashValue)
172173
while unsafe hashTable._isOccupied(bucket) {
173-
if uncheckedElement(at: bucket) == element {
174+
if unsafe uncheckedElement(at: bucket) == element {
174175
return unsafe (bucket, true)
175176
}
176177
unsafe bucket = unsafe hashTable.bucket(wrappedAfter: bucket)
@@ -197,7 +198,7 @@ extension _NativeSet { // ensureUnique
197198
unsafe _storage._hashTable.clear()
198199
unsafe _storage._count = 0
199200
}
200-
_storage = result._storage
201+
unsafe _storage = result._storage
201202
}
202203

203204
@inlinable
@@ -209,10 +210,10 @@ extension _NativeSet { // ensureUnique
209210
move: false))
210211
if count > 0 {
211212
for unsafe bucket in unsafe hashTable {
212-
result._unsafeInsertNew(self.uncheckedElement(at: bucket))
213+
unsafe result._unsafeInsertNew(self.uncheckedElement(at: bucket))
213214
}
214215
}
215-
_storage = result._storage
216+
unsafe _storage = result._storage
216217
}
217218

218219
@inlinable
@@ -221,16 +222,16 @@ extension _NativeSet { // ensureUnique
221222
unsafe _internalInvariant(newStorage._scale == _storage._scale)
222223
unsafe _internalInvariant(newStorage._age == _storage._age)
223224
unsafe _internalInvariant(newStorage._seed == _storage._seed)
224-
let result = _NativeSet(newStorage)
225+
let result = unsafe _NativeSet(newStorage)
225226
if count > 0 {
226227
unsafe result.hashTable.copyContents(of: hashTable)
227228
unsafe result._storage._count = self.count
228229
for unsafe bucket in unsafe hashTable {
229-
let element = uncheckedElement(at: bucket)
230-
result.uncheckedInitialize(at: bucket, to: element)
230+
let element = unsafe uncheckedElement(at: bucket)
231+
unsafe result.uncheckedInitialize(at: bucket, to: element)
231232
}
232233
}
233-
_storage = result._storage
234+
unsafe _storage = result._storage
234235
}
235236

236237
/// Ensure storage of self is uniquely held and can hold at least `capacity`
@@ -347,8 +348,8 @@ extension _NativeSet: _SetBuffer {
347348
@inlinable
348349
@inline(__always)
349350
internal func element(at index: Index) -> Element {
350-
let bucket = validatedBucket(for: index)
351-
return uncheckedElement(at: bucket)
351+
let bucket = unsafe validatedBucket(for: index)
352+
return unsafe uncheckedElement(at: bucket)
352353
}
353354
}
354355

@@ -382,7 +383,7 @@ extension _NativeSet { // Insertions
382383
// elements -- these imply that the Element type violates Hashable
383384
// requirements. This is generally more costly than a direct insertion,
384385
// because we'll need to compare elements in case of hash collisions.
385-
let (bucket, found) = find(element, hashValue: hashValue)
386+
let (bucket, found) = unsafe find(element, hashValue: hashValue)
386387
guard !found else {
387388
#if !$Embedded
388389
ELEMENT_TYPE_OF_SET_VIOLATES_HASHABLE_REQUIREMENTS(Element.self)
@@ -391,10 +392,10 @@ extension _NativeSet { // Insertions
391392
#endif
392393
}
393394
unsafe hashTable.insert(bucket)
394-
uncheckedInitialize(at: bucket, to: element)
395+
unsafe uncheckedInitialize(at: bucket, to: element)
395396
} else {
396397
let bucket = unsafe hashTable.insertNew(hashValue: hashValue)
397-
uncheckedInitialize(at: bucket, to: element)
398+
unsafe uncheckedInitialize(at: bucket, to: element)
398399
}
399400
unsafe _storage._count &+= 1
400401
}
@@ -411,7 +412,7 @@ extension _NativeSet { // Insertions
411412
@inlinable
412413
internal func _unsafeInsertNew(_ element: __owned Element, at bucket: Bucket) {
413414
unsafe hashTable.insert(bucket)
414-
uncheckedInitialize(at: bucket, to: element)
415+
unsafe uncheckedInitialize(at: bucket, to: element)
415416
unsafe _storage._count += 1
416417
}
417418

@@ -435,7 +436,7 @@ extension _NativeSet { // Insertions
435436
}
436437
unsafe bucket = unsafe b
437438
}
438-
_unsafeInsertNew(element, at: bucket)
439+
unsafe _unsafeInsertNew(element, at: bucket)
439440
}
440441

441442
@inlinable
@@ -460,10 +461,10 @@ extension _NativeSet { // Insertions
460461
}
461462
if found {
462463
let old = unsafe (_elements + bucket.offset).move()
463-
uncheckedInitialize(at: bucket, to: element)
464+
unsafe uncheckedInitialize(at: bucket, to: element)
464465
return old
465466
}
466-
_unsafeInsertNew(element, at: bucket)
467+
unsafe _unsafeInsertNew(element, at: bucket)
467468
return nil
468469
}
469470

@@ -475,10 +476,10 @@ extension _NativeSet { // Insertions
475476
) {
476477
let (bucket, found) = find(element)
477478
if found {
478-
uncheckedAssign(at: bucket, to: element)
479+
unsafe uncheckedAssign(at: bucket, to: element)
479480
} else {
480481
_precondition(count < capacity)
481-
_unsafeInsertNew(element, at: bucket)
482+
unsafe _unsafeInsertNew(element, at: bucket)
482483
}
483484
}
484485
}
@@ -487,7 +488,7 @@ extension _NativeSet {
487488
@inlinable
488489
@inline(__always)
489490
func isEqual(to other: _NativeSet) -> Bool {
490-
if self._storage === other._storage { return true }
491+
if unsafe (self._storage === other._storage) { return true }
491492
if self.count != other.count { return false }
492493

493494
for member in self {
@@ -503,7 +504,7 @@ extension _NativeSet {
503504

504505
defer { _fixLifetime(self) }
505506
for unsafe bucket in unsafe self.hashTable {
506-
let key = self.uncheckedElement(at: bucket)
507+
let key = unsafe self.uncheckedElement(at: bucket)
507508
let bridgedKey = _bridgeAnythingToObjectiveC(key)
508509
guard other.contains(bridgedKey) else { return false }
509510
}
@@ -516,7 +517,7 @@ extension _NativeSet: _HashTableDelegate {
516517
@inlinable
517518
@inline(__always)
518519
internal func hashValue(at bucket: Bucket) -> Int {
519-
return hashValue(for: uncheckedElement(at: bucket))
520+
return hashValue(for: unsafe uncheckedElement(at: bucket))
520521
}
521522

522523
@inlinable
@@ -546,15 +547,15 @@ extension _NativeSet { // Deletion
546547
let rehashed = ensureUnique(isUnique: isUnique, capacity: capacity)
547548
_internalInvariant(!rehashed)
548549
let old = unsafe (_elements + bucket.offset).move()
549-
_delete(at: bucket)
550+
unsafe _delete(at: bucket)
550551
return old
551552
}
552553

553554
@usableFromInline
554555
internal mutating func removeAll(isUnique: Bool) {
555556
guard isUnique else {
556557
let scale = unsafe self._storage._scale
557-
_storage = unsafe _SetStorage<Element>.allocate(
558+
unsafe _storage = _SetStorage<Element>.allocate(
558559
scale: scale,
559560
age: nil,
560561
seed: nil)
@@ -585,7 +586,7 @@ extension _NativeSet: Sequence {
585586
@inline(__always)
586587
init(_ base: __owned _NativeSet) {
587588
self.base = base
588-
self.iterator = unsafe base.hashTable.makeIterator()
589+
unsafe self.iterator = base.hashTable.makeIterator()
589590
}
590591
}
591592

@@ -604,7 +605,7 @@ extension _NativeSet.Iterator: IteratorProtocol {
604605
@inline(__always)
605606
internal mutating func next() -> Element? {
606607
guard let index = unsafe iterator.next() else { return nil }
607-
return base.uncheckedElement(at: index)
608+
return unsafe base.uncheckedElement(at: index)
608609
}
609610
}
610611

@@ -736,7 +737,7 @@ extension _NativeSet {
736737
}
737738
}
738739
unsafe _internalInvariant(difference.count > 0)
739-
return extractSubset(using: difference, count: remainingCount)
740+
return unsafe extractSubset(using: difference, count: remainingCount)
740741
}
741742
}
742743

@@ -747,12 +748,12 @@ extension _NativeSet {
747748
try unsafe _UnsafeBitset.withTemporaryBitset(capacity: bucketCount) { bitset in
748749
var count = 0
749750
for unsafe bucket in unsafe hashTable {
750-
if try isIncluded(uncheckedElement(at: bucket)) {
751+
if try isIncluded(unsafe uncheckedElement(at: bucket)) {
751752
unsafe bitset.uncheckedInsert(bucket.offset)
752753
count += 1
753754
}
754755
}
755-
return extractSubset(using: bitset, count: count)
756+
return unsafe extractSubset(using: bitset, count: count)
756757
}
757758
}
758759

@@ -779,13 +780,13 @@ extension _NativeSet {
779780
}
780781
} else {
781782
for unsafe bucket in unsafe hashTable {
782-
if other.find(uncheckedElement(at: bucket)).found {
783+
if other.find(unsafe uncheckedElement(at: bucket)).found {
783784
unsafe bitset.uncheckedInsert(bucket.offset)
784785
count += 1
785786
}
786787
}
787788
}
788-
return extractSubset(using: bitset, count: count)
789+
return unsafe extractSubset(using: bitset, count: count)
789790
}
790791
}
791792

@@ -807,7 +808,7 @@ extension _NativeSet {
807808
count += 1
808809
}
809810
}
810-
return extractSubset(using: bitset, count: count)
811+
return unsafe extractSubset(using: bitset, count: count)
811812
}
812813
}
813814
}

stdlib/public/core/Set.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ extension Set: ExpressibleByArrayLiteral {
231231
// FIXME: Shouldn't this trap?
232232
continue
233233
}
234-
native._unsafeInsertNew(element, at: bucket)
234+
unsafe native._unsafeInsertNew(element, at: bucket)
235235
}
236236
self.init(_native: native)
237237
}
@@ -1309,7 +1309,7 @@ extension Set {
13091309
@inlinable
13101310
@inline(__always)
13111311
internal init(_native index: _HashTable.Index) {
1312-
self.init(_variant: .native(index))
1312+
self.init(_variant: unsafe .native(index))
13131313
}
13141314

13151315
#if _runtime(_ObjC)
@@ -1393,7 +1393,7 @@ extension Set.Index {
13931393
"Attempting to access Set elements using an invalid index")
13941394
}
13951395
let dummy = unsafe _HashTable.Index(bucket: _HashTable.Bucket(offset: 0), age: 0)
1396-
_variant = .native(dummy)
1396+
_variant = unsafe .native(dummy)
13971397
defer { _variant = .cocoa(cocoa) }
13981398
yield &cocoa
13991399
}

0 commit comments

Comments
 (0)