Skip to content

Commit 5d10022

Browse files
committed
Update subscript and count for MutableSpan and OutputSpan with @_semantics
1 parent 2365d12 commit 5d10022

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

stdlib/public/core/Span/MutableSpan.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ extension MutableSpan where Element: ~Copyable {
236236
extension MutableSpan where Element: ~Copyable {
237237

238238
@_alwaysEmitIntoClient
239+
@_semantics("fixed_storage.get_count")
239240
public var count: Int { _assumeNonNegative(_count) }
240241

241242
@_alwaysEmitIntoClient
@@ -279,7 +280,13 @@ extension MutableSpan where Element: BitwiseCopyable {
279280
@available(SwiftCompatibilitySpan 5.0, *)
280281
@_originallyDefinedIn(module: "Swift;CompatibilitySpan", SwiftCompatibilitySpan 6.2)
281282
extension MutableSpan where Element: ~Copyable {
282-
283+
// SILOptimizer looks for fixed_storage.check_index semantics for bounds check optimizations.
284+
@_semantics("fixed_storage.check_index")
285+
@inline(__always)
286+
@_alwaysEmitIntoClient
287+
internal func _checkIndex(_ position: Index) {
288+
_precondition(indices.contains(position), "index out of bounds")
289+
}
283290
/// Accesses the element at the specified position in the `MutableSpan`.
284291
///
285292
/// - Parameter position: The offset of the element to access. `position`
@@ -289,12 +296,12 @@ extension MutableSpan where Element: ~Copyable {
289296
@_alwaysEmitIntoClient
290297
public subscript(_ position: Index) -> Element {
291298
unsafeAddress {
292-
_precondition(indices.contains(position), "index out of bounds")
299+
_checkIndex(position)
293300
return unsafe UnsafePointer(_unsafeAddressOfElement(unchecked: position))
294301
}
295302
@lifetime(self: copy self)
296303
unsafeMutableAddress {
297-
_precondition(indices.contains(position), "index out of bounds")
304+
_checkIndex(position)
298305
return unsafe _unsafeAddressOfElement(unchecked: position)
299306
}
300307
}

stdlib/public/core/Span/OutputSpan.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ extension OutputSpan where Element: ~Copyable {
193193
unsafe Range(_uncheckedBounds: (0, _count))
194194
}
195195

196+
// SILOptimizer looks for fixed_storage.check_index semantics for bounds check optimizations.
197+
@_semantics("fixed_storage.check_index")
198+
@inline(__always)
199+
@_alwaysEmitIntoClient
200+
internal func _checkIndex(_ index: Index) {
201+
_precondition(indices.contains(index), "Index out of bounds")
202+
}
203+
196204
/// Accesses the element at the specified position.
197205
///
198206
/// - Parameter index: A valid index into this span.
@@ -201,12 +209,12 @@ extension OutputSpan where Element: ~Copyable {
201209
@_alwaysEmitIntoClient
202210
public subscript(_ index: Index) -> Element {
203211
unsafeAddress {
204-
_precondition(indices.contains(index), "index out of bounds")
212+
_checkIndex(index)
205213
return unsafe UnsafePointer(_unsafeAddressOfElement(unchecked: index))
206214
}
207215
@lifetime(self: copy self)
208216
unsafeMutableAddress {
209-
_precondition(indices.contains(index), "index out of bounds")
217+
_checkIndex(index)
210218
return unsafe _unsafeAddressOfElement(unchecked: index)
211219
}
212220
}

stdlib/public/core/Span/Span.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ extension Span where Element: ~Copyable {
414414
@available(SwiftCompatibilitySpan 5.0, *)
415415
@_originallyDefinedIn(module: "Swift;CompatibilitySpan", SwiftCompatibilitySpan 6.2)
416416
extension Span where Element: ~Copyable {
417+
// SILOptimizer looks for fixed_storage.check_index semantics for bounds check optimizations.
417418
@_semantics("fixed_storage.check_index")
418419
@inline(__always)
419420
@_alwaysEmitIntoClient

0 commit comments

Comments
 (0)