Skip to content

Commit 66ce90f

Browse files
committed
[stdlib] add unsafe annotations
1 parent fc0da77 commit 66ce90f

File tree

2 files changed

+137
-134
lines changed

2 files changed

+137
-134
lines changed

stdlib/public/core/Span/MutableRawSpan.swift

+47-43
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
// A MutableRawSpan represents a span of memory which
1414
// contains initialized `Element` instances.
15+
@safe
1516
@frozen
1617
@available(SwiftStdlib 6.2, *)
1718
public struct MutableRawSpan: ~Copyable & ~Escapable {
@@ -23,7 +24,7 @@ public struct MutableRawSpan: ~Copyable & ~Escapable {
2324

2425
@_alwaysEmitIntoClient
2526
internal func _start() -> UnsafeMutableRawPointer {
26-
_pointer.unsafelyUnwrapped
27+
unsafe _pointer.unsafelyUnwrapped
2728
}
2829

2930
@_alwaysEmitIntoClient
@@ -32,7 +33,7 @@ public struct MutableRawSpan: ~Copyable & ~Escapable {
3233
_unchecked pointer: UnsafeMutableRawPointer?,
3334
byteCount: Int
3435
) {
35-
_pointer = pointer
36+
_pointer = unsafe pointer
3637
_count = byteCount
3738
}
3839
}
@@ -50,17 +51,17 @@ extension MutableRawSpan {
5051
) {
5152
let baseAddress = bytes.baseAddress
5253
let span = MutableRawSpan(_unchecked: baseAddress, byteCount: bytes.count)
53-
self = _overrideLifetime(span, borrowing: bytes)
54+
self = unsafe _overrideLifetime(span, borrowing: bytes)
5455
}
5556

5657
@_alwaysEmitIntoClient
5758
@lifetime(borrow bytes)
5859
public init(
5960
_unsafeBytes bytes: borrowing Slice<UnsafeMutableRawBufferPointer>
6061
) {
61-
let rebased = UnsafeMutableRawBufferPointer(rebasing: bytes)
62+
let rebased = unsafe UnsafeMutableRawBufferPointer(rebasing: bytes)
6263
let span = MutableRawSpan(_unsafeBytes: rebased)
63-
self = _overrideLifetime(span, borrowing: bytes)
64+
self = unsafe _overrideLifetime(span, borrowing: bytes)
6465
}
6566

6667
@_alwaysEmitIntoClient
@@ -80,30 +81,30 @@ extension MutableRawSpan {
8081
) {
8182
let bytes = UnsafeMutableRawBufferPointer(elements)
8283
let span = MutableRawSpan(_unsafeBytes: bytes)
83-
self = _overrideLifetime(span, borrowing: elements)
84+
self = unsafe _overrideLifetime(span, borrowing: elements)
8485
}
8586

8687
@_alwaysEmitIntoClient
8788
@lifetime(borrow elements)
8889
public init<Element: BitwiseCopyable>(
8990
_unsafeElements elements: borrowing Slice<UnsafeMutableBufferPointer<Element>>
9091
) {
91-
let rebased = UnsafeMutableBufferPointer(rebasing: elements)
92+
let rebased = unsafe UnsafeMutableBufferPointer(rebasing: elements)
9293
let span = MutableRawSpan(_unsafeElements: rebased)
93-
self = _overrideLifetime(span, borrowing: elements)
94+
self = unsafe _overrideLifetime(span, borrowing: elements)
9495
}
9596

9697
@_alwaysEmitIntoClient
9798
@lifetime(elements)
9899
public init<Element: BitwiseCopyable>(
99100
_elements elements: consuming MutableSpan<Element>
100101
) {
101-
let bytes = UnsafeMutableRawBufferPointer(
102+
let bytes = unsafe UnsafeMutableRawBufferPointer(
102103
start: elements._pointer,
103104
count: elements.count &* MemoryLayout<Element>.stride
104105
)
105106
let span = MutableRawSpan(_unsafeBytes: bytes)
106-
self = _overrideLifetime(span, copying: elements)
107+
self = unsafe _overrideLifetime(span, copying: elements)
107108
}
108109
}
109110

@@ -129,19 +130,19 @@ extension MutableRawSpan {
129130
_ body: (_ buffer: UnsafeRawBufferPointer) throws(E) -> Result
130131
) throws(E) -> Result {
131132
guard let pointer = _pointer, _count > 0 else {
132-
return try body(.init(start: nil, count: 0))
133+
return try unsafe body(.init(start: nil, count: 0))
133134
}
134-
return try body(.init(start: pointer, count: _count))
135+
return try unsafe body(.init(start: pointer, count: _count))
135136
}
136137

137138
@_alwaysEmitIntoClient
138139
public mutating func withUnsafeMutableBytes<E: Error, Result: ~Copyable>(
139140
_ body: (UnsafeMutableRawBufferPointer) throws(E) -> Result
140141
) throws(E) -> Result {
141142
guard let pointer = _pointer, _count > 0 else {
142-
return try body(.init(start: nil, count: 0))
143+
return try unsafe body(.init(start: nil, count: 0))
143144
}
144-
return try body(.init(start: pointer, count: _count))
145+
return try unsafe body(.init(start: pointer, count: _count))
145146
}
146147
}
147148

@@ -153,7 +154,7 @@ extension RawSpan {
153154
public init(_unsafeMutableRawSpan mutableSpan: borrowing MutableRawSpan) {
154155
let start = mutableSpan._start()
155156
let span = RawSpan(_unsafeStart: start, byteCount: mutableSpan.byteCount)
156-
self = _overrideLifetime(span, borrowing: mutableSpan)
157+
self = unsafe _overrideLifetime(span, borrowing: mutableSpan)
157158
}
158159
}
159160

@@ -174,9 +175,9 @@ extension MutableRawSpan {
174175
public borrowing func _unsafeView<T: BitwiseCopyable>(
175176
as type: T.Type
176177
) -> Span<T> {
177-
let bytes = UnsafeRawBufferPointer(start: _pointer, count: _count)
178+
let bytes = unsafe UnsafeRawBufferPointer(start: _pointer, count: _count)
178179
let span = Span<T>(_unsafeBytes: bytes)
179-
return _overrideLifetime(span, borrowing: self)
180+
return unsafe _overrideLifetime(span, borrowing: self)
180181
}
181182

182183
@unsafe
@@ -185,9 +186,11 @@ extension MutableRawSpan {
185186
public mutating func _unsafeMutableView<T: BitwiseCopyable>(
186187
as type: T.Type
187188
) -> MutableSpan<T> {
188-
let bytes = UnsafeMutableRawBufferPointer(start: _pointer, count: _count)
189+
let bytes = unsafe UnsafeMutableRawBufferPointer(
190+
start: _pointer, count: _count
191+
)
189192
let span = MutableSpan<T>(_unsafeBytes: bytes)
190-
return _overrideLifetime(span, mutating: &self)
193+
return unsafe _overrideLifetime(span, mutating: &self)
191194
}
192195
}
193196

@@ -221,7 +224,7 @@ extension MutableRawSpan {
221224
MemoryLayout<T>.size <= (_count &- offset),
222225
"Byte offset range out of bounds"
223226
)
224-
return unsafeLoad(fromUncheckedByteOffset: offset, as: T.self)
227+
return unsafe unsafeLoad(fromUncheckedByteOffset: offset, as: T.self)
225228
}
226229

227230
/// Returns a new instance of the given type, constructed from the raw memory
@@ -247,7 +250,7 @@ extension MutableRawSpan {
247250
public func unsafeLoad<T>(
248251
fromUncheckedByteOffset offset: Int, as: T.Type
249252
) -> T {
250-
_start().load(fromByteOffset: offset, as: T.self)
253+
unsafe _start().load(fromByteOffset: offset, as: T.self)
251254
}
252255

253256
/// Returns a new instance of the given type, constructed from the raw memory
@@ -276,7 +279,7 @@ extension MutableRawSpan {
276279
MemoryLayout<T>.size <= (_count &- offset),
277280
"Byte offset range out of bounds"
278281
)
279-
return unsafeLoadUnaligned(fromUncheckedByteOffset: offset, as: T.self)
282+
return unsafe unsafeLoadUnaligned(fromUncheckedByteOffset: offset, as: T.self)
280283
}
281284

282285
/// Returns a new instance of the given type, constructed from the raw memory
@@ -301,7 +304,7 @@ extension MutableRawSpan {
301304
public func unsafeLoadUnaligned<T: BitwiseCopyable>(
302305
fromUncheckedByteOffset offset: Int, as: T.Type
303306
) -> T {
304-
_start().loadUnaligned(fromByteOffset: offset, as: T.self)
307+
unsafe _start().loadUnaligned(fromByteOffset: offset, as: T.self)
305308
}
306309

307310
@_alwaysEmitIntoClient
@@ -313,15 +316,15 @@ extension MutableRawSpan {
313316
MemoryLayout<T>.size <= (_count &- offset),
314317
"Byte offset range out of bounds"
315318
)
316-
storeBytes(of: value, toUncheckedByteOffset: offset, as: type)
319+
unsafe storeBytes(of: value, toUncheckedByteOffset: offset, as: type)
317320
}
318321

319322
@unsafe
320323
@_alwaysEmitIntoClient
321324
public func storeBytes<T: BitwiseCopyable>(
322325
of value: T, toUncheckedByteOffset offset: Int, as type: T.Type
323326
) {
324-
_start().storeBytes(of: value, toByteOffset: offset, as: type)
327+
unsafe _start().storeBytes(of: value, toByteOffset: offset, as: type)
325328
}
326329
}
327330

@@ -347,7 +350,9 @@ extension MutableRawSpan {
347350
var offset = byteOffset
348351
while offset + MemoryLayout<Element>.stride <= _count {
349352
guard let element = elements.next() else { break }
350-
storeBytes(of: element, toUncheckedByteOffset: offset, as: Element.self)
353+
unsafe storeBytes(
354+
of: element, toUncheckedByteOffset: offset, as: Element.self
355+
)
351356
offset &+= MemoryLayout<Element>.stride
352357
}
353358
return offset
@@ -403,8 +408,8 @@ extension MutableRawSpan {
403408
) -> Int {
404409
if source.byteCount == 0 { return byteOffset }
405410
source.withUnsafeBytes {
406-
_start().advanced(by: byteOffset)
407-
.copyMemory(from: $0.baseAddress!, byteCount: $0.count)
411+
unsafe _start().advanced(by: byteOffset)
412+
.copyMemory(from: $0.baseAddress!, byteCount: $0.count)
408413
}
409414
return byteOffset &+ source.byteCount
410415
}
@@ -443,7 +448,7 @@ extension MutableRawSpan {
443448
UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count),
444449
"Index range out of bounds"
445450
)
446-
return _extracting(unchecked: bounds)
451+
return unsafe _extracting(unchecked: bounds)
447452
}
448453

449454
/// Constructs a new span over the items within the supplied range of
@@ -465,9 +470,9 @@ extension MutableRawSpan {
465470
@_alwaysEmitIntoClient
466471
@lifetime(borrow self)
467472
mutating public func _extracting(unchecked bounds: Range<Int>) -> Self {
468-
let newStart = _pointer?.advanced(by: bounds.lowerBound)
473+
let newStart = unsafe _pointer?.advanced(by: bounds.lowerBound)
469474
let newSpan = Self(_unchecked: newStart, byteCount: bounds.count)
470-
return _overrideLifetime(newSpan, mutating: &self)
475+
return unsafe _overrideLifetime(newSpan, mutating: &self)
471476
}
472477

473478
/// Constructs a new span over the items within the supplied range of
@@ -488,7 +493,7 @@ extension MutableRawSpan {
488493
mutating public func _extracting(
489494
_ bounds: some RangeExpression<Int>
490495
) -> Self {
491-
_extracting(bounds.relative(to: byteOffsets).clamped(to: byteOffsets))
496+
_extracting(bounds.relative(to: byteOffsets))
492497
}
493498

494499
/// Constructs a new span over the items within the supplied range of
@@ -512,11 +517,10 @@ extension MutableRawSpan {
512517
mutating public func _extracting(
513518
unchecked bounds: some RangeExpression<Int>
514519
) -> Self {
515-
_extracting(
516-
unchecked: bounds.relative(to: byteOffsets).clamped(to: byteOffsets)
517-
)
520+
unsafe _extracting(unchecked: bounds.relative(to: byteOffsets))
518521
}
519522

523+
@unsafe
520524
@_alwaysEmitIntoClient
521525
@lifetime(borrow self)
522526
mutating public func _extracting(
@@ -525,7 +529,7 @@ extension MutableRawSpan {
525529
let range = Range(
526530
_uncheckedBounds: (bounds.lowerBound, bounds.upperBound&+1)
527531
)
528-
return _extracting(unchecked: range)
532+
return unsafe _extracting(unchecked: range)
529533
}
530534

531535
/// Constructs a new span over all the items of this span.
@@ -541,7 +545,7 @@ extension MutableRawSpan {
541545
@lifetime(borrow self)
542546
mutating public func _extracting(_: UnboundedRange) -> Self {
543547
let newSpan = Self(_unchecked: _start(), byteCount: _count)
544-
return _overrideLifetime(newSpan, mutating: &self)
548+
return unsafe _overrideLifetime(newSpan, mutating: &self)
545549
}
546550
}
547551

@@ -570,7 +574,7 @@ extension MutableRawSpan {
570574
_precondition(maxLength >= 0, "Can't have a prefix of negative length")
571575
let newCount = min(maxLength, byteCount)
572576
let newSpan = Self(_unchecked: _pointer, byteCount: newCount)
573-
return _overrideLifetime(newSpan, mutating: &self)
577+
return unsafe _overrideLifetime(newSpan, mutating: &self)
574578
}
575579

576580
/// Returns a span over all but the given number of trailing elements.
@@ -593,7 +597,7 @@ extension MutableRawSpan {
593597
_precondition(k >= 0, "Can't drop a negative number of elements")
594598
let dropped = min(k, byteCount)
595599
let newSpan = Self(_unchecked: _pointer, byteCount: byteCount &- dropped)
596-
return _overrideLifetime(newSpan, mutating: &self)
600+
return unsafe _overrideLifetime(newSpan, mutating: &self)
597601
}
598602

599603
/// Returns a span containing the final elements of the span,
@@ -616,9 +620,9 @@ extension MutableRawSpan {
616620
mutating public func _extracting(last maxLength: Int) -> Self {
617621
_precondition(maxLength >= 0, "Can't have a suffix of negative length")
618622
let newCount = min(maxLength, byteCount)
619-
let newStart = _pointer?.advanced(by: byteCount &- newCount)
623+
let newStart = unsafe _pointer?.advanced(by: byteCount &- newCount)
620624
let newSpan = Self(_unchecked: newStart, byteCount: newCount)
621-
return _overrideLifetime(newSpan, copying: self)
625+
return unsafe _overrideLifetime(newSpan, copying: self)
622626
}
623627

624628
/// Returns a span over all but the given number of initial elements.
@@ -640,8 +644,8 @@ extension MutableRawSpan {
640644
mutating public func _extracting(droppingFirst k: Int) -> Self {
641645
_precondition(k >= 0, "Can't drop a negative number of bytes")
642646
let dropped = min(k, byteCount)
643-
let newStart = _pointer?.advanced(by: dropped)
647+
let newStart = unsafe _pointer?.advanced(by: dropped)
644648
let newSpan = Self(_unchecked: newStart, byteCount: byteCount &- dropped)
645-
return _overrideLifetime(newSpan, mutating: &self)
649+
return unsafe _overrideLifetime(newSpan, mutating: &self)
646650
}
647651
}

0 commit comments

Comments
 (0)