Skip to content

Commit fe68567

Browse files
committed
[Strict memory safety] Remove now-extraneous "unsafe" from the standard libraries
Now that we aren't propagating "unsafe" to nested types, remove unnecessary "unsafe" keywords from the standard library.
1 parent 0405f61 commit fe68567

File tree

8 files changed

+49
-49
lines changed

8 files changed

+49
-49
lines changed

stdlib/public/Concurrency/Deque/Deque+UnsafeHandle.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extension _Deque._UnsafeHandle {
6363

6464
var startSlot: Slot {
6565
get { unsafe _header.pointee.startSlot }
66-
nonmutating set { unsafe _header.pointee.startSlot = unsafe newValue }
66+
nonmutating set { unsafe _header.pointee.startSlot = newValue }
6767
}
6868

6969
func ptr(at slot: Slot) -> UnsafeMutablePointer<Element> {
@@ -99,28 +99,28 @@ extension _Deque._UnsafeHandle {
9999

100100
internal func slot(after slot: Slot) -> Slot {
101101
unsafe assert(slot.position < capacity)
102-
let position = unsafe slot.position + 1
102+
let position = slot.position + 1
103103
if unsafe position >= capacity {
104-
return unsafe Slot(at: 0)
104+
return Slot(at: 0)
105105
}
106-
return unsafe Slot(at: position)
106+
return Slot(at: position)
107107
}
108108

109109
internal func slot(before slot: Slot) -> Slot {
110110
unsafe assert(slot.position < capacity)
111-
if unsafe slot.position == 0 { return unsafe Slot(at: capacity - 1) }
112-
return unsafe Slot(at: slot.position - 1)
111+
if slot.position == 0 { return unsafe Slot(at: capacity - 1) }
112+
return Slot(at: slot.position - 1)
113113
}
114114

115115
internal func slot(_ slot: Slot, offsetBy delta: Int) -> Slot {
116116
unsafe assert(slot.position <= capacity)
117-
let position = unsafe slot.position + delta
117+
let position = slot.position + delta
118118
if delta >= 0 {
119119
if unsafe position >= capacity { return unsafe Slot(at: position - capacity) }
120120
} else {
121121
if position < 0 { return unsafe Slot(at: position + capacity) }
122122
}
123-
return unsafe Slot(at: position)
123+
return Slot(at: position)
124124
}
125125

126126
internal var endSlot: Slot {
@@ -139,7 +139,7 @@ extension _Deque._UnsafeHandle {
139139
// random-access subscript operations. (Up to 2x on some microbenchmarks.)
140140
let position = unsafe startSlot.position &+ offset
141141
guard unsafe position < capacity else { return unsafe Slot(at: position &- capacity) }
142-
return unsafe Slot(at: position)
142+
return Slot(at: position)
143143
}
144144
}
145145

@@ -199,9 +199,9 @@ extension _Deque._UnsafeHandle {
199199
from source: UnsafeBufferPointer<Element>
200200
) -> Slot {
201201
unsafe assert(start.position + source.count <= capacity)
202-
guard source.count > 0 else { return unsafe start }
202+
guard source.count > 0 else { return start }
203203
unsafe ptr(at: start).initialize(from: source.baseAddress!, count: source.count)
204-
return unsafe Slot(at: start.position + source.count)
204+
return Slot(at: start.position + source.count)
205205
}
206206

207207
@discardableResult
@@ -210,9 +210,9 @@ extension _Deque._UnsafeHandle {
210210
from source: UnsafeMutableBufferPointer<Element>
211211
) -> Slot {
212212
unsafe assert(start.position + source.count <= capacity)
213-
guard source.count > 0 else { return unsafe start }
213+
guard source.count > 0 else { return start }
214214
unsafe ptr(at: start).moveInitialize(from: source.baseAddress!, count: source.count)
215-
return unsafe Slot(at: start.position + source.count)
215+
return Slot(at: start.position + source.count)
216216
}
217217

218218
@discardableResult
@@ -224,7 +224,7 @@ extension _Deque._UnsafeHandle {
224224
assert(count >= 0)
225225
unsafe assert(source.position + count <= self.capacity)
226226
unsafe assert(target.position + count <= self.capacity)
227-
guard count > 0 else { return unsafe (source, target) }
227+
guard count > 0 else { return (source, target) }
228228
unsafe ptr(at: target).moveInitialize(from: ptr(at: source), count: count)
229229
return unsafe (slot(source, offsetBy: count), slot(target, offsetBy: count))
230230
}
@@ -451,7 +451,7 @@ extension _Deque._UnsafeHandle {
451451
) -> _UnsafeMutableWrappedBuffer<Element> {
452452
unsafe assert(start.position <= capacity)
453453
unsafe assert(end.position <= capacity)
454-
if unsafe start < end {
454+
if start < end {
455455
return unsafe .init(start: ptr(at: start), count: end.position - start.position)
456456
}
457457
return unsafe .init(

stdlib/public/Synchronization/Atomics/AtomicPointers.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ extension UnsafeBufferPointer: @unsafe AtomicRepresentable where Element: ~Copya
822822
public static func decodeAtomicRepresentation(
823823
_ representation: consuming AtomicRepresentation
824824
) -> UnsafeBufferPointer<Element> {
825-
let wp = unsafe WordPair.decodeAtomicRepresentation(representation)
825+
let wp = WordPair.decodeAtomicRepresentation(representation)
826826

827827
return unsafe UnsafeBufferPointer<Element>(
828828
start: UnsafePointer<Element>(bitPattern: wp.first),
@@ -890,7 +890,7 @@ where Element: ~Copyable
890890
public static func decodeAtomicRepresentation(
891891
_ representation: consuming AtomicRepresentation
892892
) -> UnsafeMutableBufferPointer<Element> {
893-
let wp = unsafe WordPair.decodeAtomicRepresentation(representation)
893+
let wp = WordPair.decodeAtomicRepresentation(representation)
894894

895895
return unsafe UnsafeMutableBufferPointer<Element>(
896896
start: UnsafeMutablePointer<Element>(bitPattern: wp.first),
@@ -956,7 +956,7 @@ extension UnsafeRawBufferPointer: @unsafe AtomicRepresentable {
956956
public static func decodeAtomicRepresentation(
957957
_ representation: consuming AtomicRepresentation
958958
) -> UnsafeRawBufferPointer {
959-
let wp = unsafe WordPair.decodeAtomicRepresentation(representation)
959+
let wp = WordPair.decodeAtomicRepresentation(representation)
960960

961961
return unsafe UnsafeRawBufferPointer(
962962
start: UnsafeRawPointer(bitPattern: wp.first),
@@ -1022,7 +1022,7 @@ extension UnsafeMutableRawBufferPointer: @unsafe AtomicRepresentable {
10221022
public static func decodeAtomicRepresentation(
10231023
_ representation: consuming AtomicRepresentation
10241024
) -> UnsafeMutableRawBufferPointer {
1025-
let wp = unsafe WordPair.decodeAtomicRepresentation(representation)
1025+
let wp = WordPair.decodeAtomicRepresentation(representation)
10261026

10271027
return unsafe UnsafeMutableRawBufferPointer(
10281028
start: UnsafeMutableRawPointer(bitPattern: wp.first),

stdlib/public/core/ArrayBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ extension _ArrayBuffer {
330330
.assumingMemoryBound(to: AnyObject.self)
331331
let (_, c) = unsafe _nonNative._copyContents(
332332
initializing: UnsafeMutableBufferPointer(start: ptr, count: buffer.count))
333-
return unsafe (IndexingIterator(_elements: self, _position: c), c)
333+
return (IndexingIterator(_elements: self, _position: c), c)
334334
}
335335

336336
/// Returns a `_SliceBuffer` containing the given sub-range of elements in

stdlib/public/core/CollectionAlgorithms.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ extension MutableCollection where Self: BidirectionalCollection {
402402
(bufferPointer) -> Int in
403403
let unsafeBufferPivot = try unsafe bufferPointer._partitionImpl(
404404
by: belongsInSecondPartition)
405-
return unsafe unsafeBufferPivot - bufferPointer.startIndex
405+
return unsafeBufferPivot - bufferPointer.startIndex
406406
}
407407
if let offset = maybeOffset {
408408
return index(startIndex, offsetBy: offset)

stdlib/public/core/SmallString.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ extension _SmallString {
147147
var copy = self
148148
unsafe withUnsafeBytes(of: &copy._storage) {
149149
unsafe _internalInvariant(
150-
$0[count..<_SmallString.capacity].allSatisfy { unsafe $0 == 0 })
150+
$0[count..<_SmallString.capacity].allSatisfy { $0 == 0 })
151151
}
152152
}
153153
#endif // INTERNAL_CHECKS_ENABLED

stdlib/public/core/Sort.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,10 @@ extension UnsafeMutableBufferPointer {
530530
buffer: UnsafeMutablePointer<Element>,
531531
by areInIncreasingOrder: (Element, Element) throws -> Bool
532532
) rethrows -> Bool {
533-
unsafe _internalInvariant(runs[i - 1].upperBound == runs[i].lowerBound)
534-
let low = unsafe runs[i - 1].lowerBound
535-
let middle = unsafe runs[i].lowerBound
536-
let high = unsafe runs[i].upperBound
533+
_internalInvariant(runs[i - 1].upperBound == runs[i].lowerBound)
534+
let low = runs[i - 1].lowerBound
535+
let middle = runs[i].lowerBound
536+
let high = runs[i].upperBound
537537

538538
try unsafe _merge(
539539
low: baseAddress! + low,
@@ -542,8 +542,8 @@ extension UnsafeMutableBufferPointer {
542542
buffer: buffer,
543543
by: areInIncreasingOrder)
544544

545-
unsafe runs[i - 1] = unsafe low..<high
546-
unsafe runs.remove(at: i)
545+
runs[i - 1] = low..<high
546+
runs.remove(at: i)
547547

548548
return true
549549
}
@@ -588,30 +588,30 @@ extension UnsafeMutableBufferPointer {
588588
// for the entirety of `runs`.
589589

590590
// The invariant is always in place for a single element.
591-
while unsafe runs.count > 1 {
592-
var lastIndex = unsafe runs.count - 1
591+
while runs.count > 1 {
592+
var lastIndex = runs.count - 1
593593

594594
// Check for the three invariant-breaking conditions, and break out of
595595
// the while loop if none are met.
596-
if unsafe lastIndex >= 3 &&
596+
if lastIndex >= 3 &&
597597
(runs[lastIndex - 3].count <=
598598
runs[lastIndex - 2].count + runs[lastIndex - 1].count)
599599
{
600600
// Second-to-last three runs do not follow W > X + Y.
601601
// Always merge Y with the smaller of X or Z.
602-
if unsafe runs[lastIndex - 2].count < runs[lastIndex].count {
602+
if runs[lastIndex - 2].count < runs[lastIndex].count {
603603
lastIndex -= 1
604604
}
605-
} else if unsafe lastIndex >= 2 &&
605+
} else if lastIndex >= 2 &&
606606
(runs[lastIndex - 2].count <=
607607
runs[lastIndex - 1].count + runs[lastIndex].count)
608608
{
609609
// Last three runs do not follow X > Y + Z.
610610
// Always merge Y with the smaller of X or Z.
611-
if unsafe runs[lastIndex - 2].count < runs[lastIndex].count {
611+
if runs[lastIndex - 2].count < runs[lastIndex].count {
612612
lastIndex -= 1
613613
}
614-
} else if unsafe runs[lastIndex - 1].count <= runs[lastIndex].count {
614+
} else if runs[lastIndex - 1].count <= runs[lastIndex].count {
615615
// Last two runs do not follow Y > Z, so merge Y and Z.
616616
// This block is intentionally blank--the merge happens below.
617617
} else {
@@ -645,7 +645,7 @@ extension UnsafeMutableBufferPointer {
645645
buffer: UnsafeMutablePointer<Element>,
646646
by areInIncreasingOrder: (Element, Element) throws -> Bool
647647
) rethrows -> Bool {
648-
while unsafe runs.count > 1 {
648+
while runs.count > 1 {
649649
try unsafe _mergeRuns(
650650
&runs, at: runs.count - 1, buffer: buffer, by: areInIncreasingOrder)
651651
}
@@ -677,7 +677,7 @@ extension UnsafeMutableBufferPointer {
677677
// closure, since the buffer is guaranteed to be uninitialized at exit.
678678
_ = try unsafe Array<Element>(_unsafeUninitializedCapacity: count / 2) {
679679
buffer, _ in
680-
var runs: [Range<Index>] = unsafe []
680+
var runs: [Range<Index>] = []
681681

682682
var start = startIndex
683683
while start < endIndex {
@@ -690,24 +690,24 @@ extension UnsafeMutableBufferPointer {
690690

691691
// If the current run is shorter than the minimum length, use the
692692
// insertion sort to extend it.
693-
if unsafe end < endIndex && end - start < minimumRunLength {
693+
if end < endIndex && end - start < minimumRunLength {
694694
let newEnd = Swift.min(endIndex, start + minimumRunLength)
695695
try unsafe _insertionSort(
696696
within: start..<newEnd, sortedEnd: end, by: areInIncreasingOrder)
697-
unsafe end = newEnd
697+
end = newEnd
698698
}
699699

700700
// Append this run and merge down as needed to maintain the `runs`
701701
// invariants.
702-
unsafe runs.append(start..<end)
702+
runs.append(start..<end)
703703
try unsafe _mergeTopRuns(
704704
&runs, buffer: buffer.baseAddress!, by: areInIncreasingOrder)
705-
start = unsafe end
705+
start = end
706706
}
707707

708708
try unsafe _finalizeRuns(
709709
&runs, buffer: buffer.baseAddress!, by: areInIncreasingOrder)
710-
unsafe _internalInvariant(runs.count == 1, "Didn't complete final merge")
710+
_internalInvariant(runs.count == 1, "Didn't complete final merge")
711711
}
712712
}
713713
}

stdlib/public/core/StringUTF8Validation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ internal func validateUTF8(_ buf: UnsafeBufferPointer<UInt8>) -> UTF8ValidationR
160160
func _legacyNarrowIllegalRange(buf: Slice<UnsafeBufferPointer<UInt8>>) -> Range<Int> {
161161
var reversePacked: UInt32 = 0
162162
if let third = unsafe buf.dropFirst(2).first {
163-
unsafe reversePacked |= UInt32(third)
163+
reversePacked |= UInt32(third)
164164
reversePacked <<= 8
165165
}
166166
if let second = unsafe buf.dropFirst().first {
167-
unsafe reversePacked |= UInt32(second)
167+
reversePacked |= UInt32(second)
168168
reversePacked <<= 8
169169
}
170170
unsafe reversePacked |= UInt32(buf.first!)
@@ -177,8 +177,8 @@ internal func validateUTF8(_ buf: UnsafeBufferPointer<UInt8>) -> UTF8ValidationR
177177
var endIndex = unsafe buf.startIndex
178178
var iter = unsafe buf.makeIterator()
179179
_ = unsafe iter.next()
180-
while let cu = unsafe iter.next(), unsafe UTF8.isContinuation(cu) {
181-
unsafe endIndex += 1
180+
while let cu = unsafe iter.next(), UTF8.isContinuation(cu) {
181+
endIndex += 1
182182
// Unicode's Maximal subpart of an ill-formed subsequence will yield
183183
// at most 3 bytes of error.
184184
if unsafe buf.distance(from: buf.startIndex, to: endIndex) >= 3 {

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ extension UnsafeMutableBufferPointer where Element: ~Copyable {
13001300
/// - index: The index of the element to initialize
13011301
@_alwaysEmitIntoClient
13021302
public func initializeElement(at index: Index, to value: consuming Element) {
1303-
_debugPrecondition(unsafe startIndex <= index && index < endIndex)
1303+
_debugPrecondition(startIndex <= index && index < endIndex)
13041304
let p = unsafe baseAddress._unsafelyUnwrappedUnchecked.advanced(by: index)
13051305
unsafe p.initialize(to: value)
13061306
}
@@ -1317,7 +1317,7 @@ extension UnsafeMutableBufferPointer where Element: ~Copyable {
13171317
/// - Returns: The instance referenced by this index in this buffer.
13181318
@_alwaysEmitIntoClient
13191319
public func moveElement(from index: Index) -> Element {
1320-
_debugPrecondition(unsafe startIndex <= index && index < endIndex)
1320+
_debugPrecondition(startIndex <= index && index < endIndex)
13211321
return unsafe baseAddress._unsafelyUnwrappedUnchecked.advanced(by: index).move()
13221322
}
13231323

@@ -1331,7 +1331,7 @@ extension UnsafeMutableBufferPointer where Element: ~Copyable {
13311331
/// - index: The index of the buffer element to deinitialize.
13321332
@_alwaysEmitIntoClient
13331333
public func deinitializeElement(at index: Index) {
1334-
_debugPrecondition(unsafe startIndex <= index && index < endIndex)
1334+
_debugPrecondition(startIndex <= index && index < endIndex)
13351335
let p = unsafe baseAddress._unsafelyUnwrappedUnchecked.advanced(by: index)
13361336
unsafe p.deinitialize(count: 1)
13371337
}

0 commit comments

Comments
 (0)