Skip to content

Commit 40a2ecb

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

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ extension OutputSpan where Element: ~Copyable {
8282
extension OutputSpan where Element: ~Copyable {
8383
/// The number of initialized elements in this span.
8484
@_alwaysEmitIntoClient
85+
@_semantics("fixed_storage.get_count")
8586
public var count: Int { _count }
8687

8788
/// The number of additional elements that can be added to this span.
@@ -193,6 +194,14 @@ extension OutputSpan where Element: ~Copyable {
193194
unsafe Range(_uncheckedBounds: (0, _count))
194195
}
195196

197+
// SILOptimizer looks for fixed_storage.check_index semantics for bounds check optimizations.
198+
@_semantics("fixed_storage.check_index")
199+
@inline(__always)
200+
@_alwaysEmitIntoClient
201+
internal func _checkIndex(_ index: Index) {
202+
_precondition(indices.contains(index), "index out of bounds")
203+
}
204+
196205
/// Accesses the element at the specified position.
197206
///
198207
/// - Parameter index: A valid index into this span.
@@ -201,12 +210,12 @@ extension OutputSpan where Element: ~Copyable {
201210
@_alwaysEmitIntoClient
202211
public subscript(_ index: Index) -> Element {
203212
unsafeAddress {
204-
_precondition(indices.contains(index), "index out of bounds")
213+
_checkIndex(index)
205214
return unsafe UnsafePointer(_unsafeAddressOfElement(unchecked: index))
206215
}
207216
@lifetime(self: copy self)
208217
unsafeMutableAddress {
209-
_precondition(indices.contains(index), "index out of bounds")
218+
_checkIndex(index)
210219
return unsafe _unsafeAddressOfElement(unchecked: index)
211220
}
212221
}

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)