Skip to content

Commit 81bc0ee

Browse files
authored
Merge pull request #3146 from apple/stdlib-dictionary-make-safer
stdlib: make some internal Dictionary APIs safer
2 parents 8cc9875 + 6f502fa commit 81bc0ee

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,7 +2502,7 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
25022502
return BufferPointer(self)
25032503
}
25042504

2505-
// All underscored functions are unsafe and need a _fixLifetime in the caller.
2505+
// This API is unsafe and needs a `_fixLifetime` in the caller.
25062506
internal var _body: _HashedContainerStorageHeader {
25072507
unsafeAddress {
25082508
return UnsafePointer(buffer._valuePointer)
@@ -2514,32 +2514,38 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
25142514

25152515
@_versioned
25162516
internal var _capacity: Int {
2517+
defer { _fixLifetime(self) }
25172518
return _body.capacity
25182519
}
25192520

25202521
@_versioned
25212522
internal var _count: Int {
25222523
set {
2524+
defer { _fixLifetime(self) }
25232525
_body.count = newValue
25242526
}
25252527
get {
2528+
defer { _fixLifetime(self) }
25262529
return _body.count
25272530
}
25282531
}
25292532

2530-
internal var _maxLoadFactorInverse : Double {
2533+
internal var _maxLoadFactorInverse: Double {
2534+
defer { _fixLifetime(self) }
25312535
return _body.maxLoadFactorInverse
25322536
}
25332537

2538+
// This API is unsafe and needs a `_fixLifetime` in the caller.
25342539
internal
25352540
var _initializedHashtableEntriesBitMapStorage: UnsafeMutablePointer<UInt> {
25362541
return _roundUp(buffer._elementPointer, toAlignmentOf: UInt.self)
25372542
}
25382543

2544+
// This API is unsafe and needs a `_fixLifetime` in the caller.
25392545
internal var _keys: UnsafeMutablePointer<Key> {
25402546
let bitMapSizeInBytes =
25412547
_unsafeMultiply(
2542-
_UnsafeBitMap.sizeInWords(forSizeInBits: _capacity),
2548+
_UnsafeBitMap.sizeInWords(forSizeInBits: _body.capacity),
25432549
strideof(UInt.self))
25442550
let start =
25452551
UnsafeMutablePointer<UInt8>(_initializedHashtableEntriesBitMapStorage)
@@ -2548,8 +2554,9 @@ final internal class _Native${Self}StorageImpl<${TypeParameters}> :
25482554
}
25492555

25502556
%if Self == 'Dictionary':
2557+
// This API is unsafe and needs a `_fixLifetime` in the caller.
25512558
internal var _values: UnsafeMutablePointer<Value> {
2552-
let keysSizeInBytes = _unsafeMultiply(_capacity, strideof(Key.self))
2559+
let keysSizeInBytes = _unsafeMultiply(_body.capacity, strideof(Key.self))
25532560
let start = UnsafeMutablePointer<UInt8>(_keys) + keysSizeInBytes
25542561
return _roundUp(start, toAlignmentOf: Value.self)
25552562
}
@@ -2660,30 +2667,23 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
26602667
@_transparent
26612668
public // @testable
26622669
var capacity: Int {
2663-
let result = buffer._capacity
2664-
_fixLifetime(buffer)
2665-
return result
2670+
return buffer._capacity
26662671
}
26672672

26682673
@_versioned
26692674
@_transparent
26702675
internal var count: Int {
26712676
get {
2672-
let result = buffer._count
2673-
_fixLifetime(buffer)
2674-
return result
2677+
return buffer._count
26752678
}
26762679
nonmutating set(newValue) {
26772680
buffer._count = newValue
2678-
_fixLifetime(buffer)
26792681
}
26802682
}
26812683

26822684
@_transparent
26832685
internal var maxLoadFactorInverse: Double {
2684-
let result = buffer._maxLoadFactorInverse
2685-
_fixLifetime(buffer)
2686-
return result
2686+
return buffer._maxLoadFactorInverse
26872687
}
26882688

26892689
@_versioned

0 commit comments

Comments
 (0)