Skip to content

Commit 322443d

Browse files
committed
stdlib: Address StrictMemorySafety warnings in Array related code.
1 parent 3b18849 commit 322443d

File tree

7 files changed

+29
-29
lines changed

7 files changed

+29
-29
lines changed

stdlib/public/core/ArrayBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ extension _ArrayBuffer {
659659
return try unsafe body(
660660
UnsafeBufferPointer(start: firstElementAddress, count: count))
661661
}
662-
return try ContiguousArray(self).withUnsafeBufferPointer(body)
662+
return try unsafe ContiguousArray(self).withUnsafeBufferPointer(body)
663663
}
664664

665665
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the

stdlib/public/core/ArraySlice.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,12 @@ extension ArraySlice: _ArrayProtocol {
277277
@inlinable
278278
public var _baseAddressIfContiguous: UnsafeMutablePointer<Element>? {
279279
@inline(__always) // FIXME(TODO: JIRA): Hack around test failure
280-
get { return _buffer.firstElementAddressIfContiguous }
280+
get { return unsafe _buffer.firstElementAddressIfContiguous }
281281
}
282282

283283
@inlinable
284284
internal var _baseAddress: UnsafeMutablePointer<Element> {
285-
return _buffer.firstElementAddress
285+
return unsafe _buffer.firstElementAddress
286286
}
287287
}
288288

@@ -703,7 +703,7 @@ extension ArraySlice: RangeReplaceableCollection {
703703
if count > 0 {
704704
_buffer = ArraySlice._allocateBufferUninitialized(minimumCapacity: count)
705705
_buffer.count = count
706-
var p = _buffer.firstElementAddress
706+
var p = unsafe _buffer.firstElementAddress
707707
for _ in 0..<count {
708708
unsafe p.initialize(to: repeatedValue)
709709
unsafe p += 1
@@ -752,7 +752,7 @@ extension ArraySlice: RangeReplaceableCollection {
752752
_ count: Int
753753
) -> (ArraySlice, UnsafeMutablePointer<Element>) {
754754
let result = ArraySlice(_uninitializedCount: count)
755-
return (result, result._buffer.firstElementAddress)
755+
return (result, unsafe result._buffer.firstElementAddress)
756756
}
757757

758758
//===--- basic mutations ------------------------------------------------===//
@@ -1171,7 +1171,7 @@ extension ArraySlice {
11711171
func withUnsafeBufferPointer<R>(
11721172
_ body: (UnsafeBufferPointer<Element>) throws -> R
11731173
) rethrows -> R {
1174-
return try _buffer.withUnsafeBufferPointer(body)
1174+
return try unsafe _buffer.withUnsafeBufferPointer(body)
11751175
}
11761176

11771177
/// Calls a closure with a pointer to the array's contiguous storage.
@@ -1207,7 +1207,7 @@ extension ArraySlice {
12071207
public func withUnsafeBufferPointer<R, E>(
12081208
_ body: (UnsafeBufferPointer<Element>) throws(E) -> R
12091209
) throws(E) -> R {
1210-
return try _buffer.withUnsafeBufferPointer(body)
1210+
return try unsafe _buffer.withUnsafeBufferPointer(body)
12111211
}
12121212

12131213
@available(SwiftStdlib 6.2, *)
@@ -1283,7 +1283,7 @@ extension ArraySlice {
12831283
_makeMutableAndUnique()
12841284

12851285
// Create an UnsafeBufferPointer that we can pass to body
1286-
let pointer = _buffer.firstElementAddress
1286+
let pointer = unsafe _buffer.firstElementAddress
12871287
var inoutBufferPointer = unsafe UnsafeMutableBufferPointer(
12881288
start: pointer, count: count)
12891289

@@ -1573,7 +1573,7 @@ extension ArraySlice {
15731573
@_alwaysEmitIntoClient
15741574
public func _copyToNewArray() -> [Element] {
15751575
unsafe Array(unsafeUninitializedCapacity: self.count) { buffer, count in
1576-
var (it, c) = self._buffer._copyContents(initializing: buffer)
1576+
var (it, c) = unsafe self._buffer._copyContents(initializing: buffer)
15771577
_precondition(it.next() == nil)
15781578
count = c
15791579
}

stdlib/public/core/CocoaArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ internal struct _CocoaArrayWrapper: RandomAccessCollection {
7676
let cocoaStorageBaseAddress = unsafe self.contiguousStorage(self.indices)
7777

7878
if let cocoaStorageBaseAddress = unsafe cocoaStorageBaseAddress {
79-
return _SliceBuffer(
79+
return unsafe _SliceBuffer(
8080
owner: self.buffer,
8181
subscriptBaseAddress: cocoaStorageBaseAddress,
8282
indices: bounds,

stdlib/public/core/ContiguousArray.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ extension ContiguousArray: RangeReplaceableCollection {
995995
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
996996
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
997997
) rethrows -> R? {
998-
return try withUnsafeMutableBufferPointer {
998+
return try unsafe withUnsafeMutableBufferPointer {
999999
(bufferPointer) -> R in
10001000
return try unsafe body(&bufferPointer)
10011001
}
@@ -1005,7 +1005,7 @@ extension ContiguousArray: RangeReplaceableCollection {
10051005
public mutating func withContiguousMutableStorageIfAvailable<R>(
10061006
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
10071007
) rethrows -> R? {
1008-
return try withUnsafeMutableBufferPointer {
1008+
return try unsafe withUnsafeMutableBufferPointer {
10091009
(bufferPointer) -> R in
10101010
return try unsafe body(&bufferPointer)
10111011
}
@@ -1015,7 +1015,7 @@ extension ContiguousArray: RangeReplaceableCollection {
10151015
public func withContiguousStorageIfAvailable<R>(
10161016
_ body: (UnsafeBufferPointer<Element>) throws -> R
10171017
) rethrows -> R? {
1018-
return try withUnsafeBufferPointer {
1018+
return try unsafe withUnsafeBufferPointer {
10191019
(bufferPointer) -> R in
10201020
return try unsafe body(bufferPointer)
10211021
}
@@ -1059,7 +1059,7 @@ extension ContiguousArray: CustomStringConvertible, CustomDebugStringConvertible
10591059
extension ContiguousArray {
10601060
@usableFromInline @_transparent
10611061
internal func _cPointerArgs() -> (AnyObject?, UnsafeRawPointer?) {
1062-
let p = _baseAddressIfContiguous
1062+
let p = unsafe _baseAddressIfContiguous
10631063
if unsafe _fastPath(p != nil || isEmpty) {
10641064
return (_owner, UnsafeRawPointer(p))
10651065
}
@@ -1173,7 +1173,7 @@ extension ContiguousArray {
11731173
mutating func __abi_withUnsafeMutableBufferPointer<R>(
11741174
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
11751175
) rethrows -> R {
1176-
return try withUnsafeMutableBufferPointer(body)
1176+
return try unsafe withUnsafeMutableBufferPointer(body)
11771177
}
11781178

11791179
/// Calls the given closure with a pointer to the array's mutable contiguous
@@ -1272,7 +1272,7 @@ extension ContiguousArray {
12721272
_precondition(self.count <= buffer.count,
12731273
"Insufficient space allocated to copy array contents")
12741274

1275-
if let s = _baseAddressIfContiguous {
1275+
if let s = unsafe _baseAddressIfContiguous {
12761276
unsafe p.initialize(from: s, count: self.count)
12771277
// Need a _fixLifetime bracketing the _baseAddressIfContiguous getter
12781278
// and all uses of the pointer it returns:
@@ -1446,7 +1446,7 @@ extension ContiguousArray {
14461446
public mutating func withUnsafeMutableBytes<R>(
14471447
_ body: (UnsafeMutableRawBufferPointer) throws -> R
14481448
) rethrows -> R {
1449-
return try self.withUnsafeMutableBufferPointer {
1449+
return try unsafe self.withUnsafeMutableBufferPointer {
14501450
return try unsafe body(UnsafeMutableRawBufferPointer($0))
14511451
}
14521452
}
@@ -1482,7 +1482,7 @@ extension ContiguousArray {
14821482
public func withUnsafeBytes<R>(
14831483
_ body: (UnsafeRawBufferPointer) throws -> R
14841484
) rethrows -> R {
1485-
return try self.withUnsafeBufferPointer {
1485+
return try unsafe self.withUnsafeBufferPointer {
14861486
try unsafe body(UnsafeRawBufferPointer($0))
14871487
}
14881488
}

stdlib/public/core/Diffing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ private func _myers<C,D>(
355355
) rethrows -> R {
356356
if let result = try values.withContiguousStorageIfAvailable(body) { return result }
357357
let array = ContiguousArray(values)
358-
return try array.withUnsafeBufferPointer(body)
358+
return try unsafe array.withUnsafeBufferPointer(body)
359359
}
360360

361361
return unsafe _withContiguousStorage(for: old) { a in

stdlib/public/core/InlineArray.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ extension InlineArray where Element: ~Copyable {
147147
public init<E: Error>(_ body: (Index) throws(E) -> Element) throws(E) {
148148
#if $BuiltinEmplaceTypedThrows
149149
self = try Builtin.emplace { (rawPtr) throws(E) -> () in
150-
let buffer = Self._initializationBuffer(start: rawPtr)
150+
let buffer = unsafe Self._initializationBuffer(start: rawPtr)
151151

152152
for i in 0 ..< count {
153153
do throws(E) {
@@ -204,7 +204,7 @@ extension InlineArray where Element: ~Copyable {
204204
var o: Element? = first
205205

206206
self = try Builtin.emplace { (rawPtr) throws(E) -> () in
207-
let buffer = Self._initializationBuffer(start: rawPtr)
207+
let buffer = unsafe Self._initializationBuffer(start: rawPtr)
208208

209209
guard Self.count > 0 else {
210210
return
@@ -247,7 +247,7 @@ extension InlineArray where Element: Copyable {
247247
@_alwaysEmitIntoClient
248248
public init(repeating value: Element) {
249249
self = Builtin.emplace {
250-
let buffer = Self._initializationBuffer(start: $0)
250+
let buffer = unsafe Self._initializationBuffer(start: $0)
251251

252252
unsafe buffer.initialize(repeating: value)
253253
}

stdlib/public/core/SliceBuffer.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ internal struct _SliceBuffer<Element>
5252
endIndexAndFlags: UInt
5353
) {
5454
self.owner = owner
55-
self.subscriptBaseAddress = unsafe subscriptBaseAddress
55+
unsafe self.subscriptBaseAddress = subscriptBaseAddress
5656
self.startIndex = startIndex
5757
self.endIndexAndFlags = endIndexAndFlags
5858
}
@@ -63,7 +63,7 @@ internal struct _SliceBuffer<Element>
6363
indices: Range<Int>, hasNativeBuffer: Bool
6464
) {
6565
self.owner = owner
66-
self.subscriptBaseAddress = unsafe subscriptBaseAddress
66+
unsafe self.subscriptBaseAddress = subscriptBaseAddress
6767
self.startIndex = indices.lowerBound
6868
let bufferFlag = UInt(hasNativeBuffer ? 1 : 0)
6969
self.endIndexAndFlags = (UInt(indices.upperBound) << 1) | bufferFlag
@@ -78,7 +78,7 @@ internal struct _SliceBuffer<Element>
7878
#else
7979
self.owner = _emptyArrayStorage
8080
#endif
81-
self.subscriptBaseAddress = unsafe empty.firstElementAddress
81+
unsafe self.subscriptBaseAddress = empty.firstElementAddress
8282
self.startIndex = empty.startIndex
8383
self.endIndexAndFlags = 1
8484
_invariantCheck()
@@ -176,7 +176,7 @@ internal struct _SliceBuffer<Element>
176176
/// identity and count.
177177
@inlinable
178178
internal var identity: UnsafeRawPointer {
179-
return UnsafeRawPointer(firstElementAddress)
179+
return unsafe UnsafeRawPointer(firstElementAddress)
180180
}
181181

182182
@inlinable
@@ -186,7 +186,7 @@ internal struct _SliceBuffer<Element>
186186

187187
@inlinable
188188
internal var firstElementAddressIfContiguous: UnsafeMutablePointer<Element>? {
189-
return firstElementAddress
189+
return unsafe firstElementAddress
190190
}
191191

192192
//===--- Non-essential bits ---------------------------------------------===//
@@ -399,7 +399,7 @@ internal struct _SliceBuffer<Element>
399399
_internalInvariant(bounds.lowerBound >= startIndex)
400400
_internalInvariant(bounds.upperBound >= bounds.lowerBound)
401401
_internalInvariant(bounds.upperBound <= endIndex)
402-
return _SliceBuffer(
402+
return unsafe _SliceBuffer(
403403
owner: owner,
404404
subscriptBaseAddress: subscriptBaseAddress,
405405
indices: bounds,
@@ -482,7 +482,7 @@ internal struct _SliceBuffer<Element>
482482
_internalInvariant(_isClassOrObjCExistential(T.self))
483483
let baseAddress = unsafe UnsafeMutableRawPointer(self.subscriptBaseAddress)
484484
.assumingMemoryBound(to: T.self)
485-
return _SliceBuffer<T>(
485+
return unsafe _SliceBuffer<T>(
486486
owner: self.owner,
487487
subscriptBaseAddress: baseAddress,
488488
startIndex: self.startIndex,

0 commit comments

Comments
 (0)