Skip to content

Commit fbe73da

Browse files
authored
Merge pull request #82597 from glessard/rdar145487409-inlinearray-plus-mutablespan-62
[6.2, stdlib] Remove _withUnsafeBufferPointer APIs on InlineArray
2 parents ec750ef + 3c0f754 commit fbe73da

File tree

9 files changed

+99
-67
lines changed

9 files changed

+99
-67
lines changed

stdlib/public/core/Array.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,9 +1744,9 @@ extension Array {
17441744
}
17451745

17461746
@available(SwiftStdlib 6.2, *)
1747+
@_alwaysEmitIntoClient
17471748
public var mutableSpan: MutableSpan<Element> {
17481749
@lifetime(&self)
1749-
@_alwaysEmitIntoClient
17501750
mutating get {
17511751
// _makeMutableAndUnique*() inserts begin_cow_mutation.
17521752
// LifetimeDependence analysis inserts call to end_cow_mutation_addr since we cannot schedule it in the stdlib for mutableSpan property.

stdlib/public/core/ArraySlice.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,9 +1301,9 @@ extension ArraySlice {
13011301
}
13021302

13031303
@available(SwiftStdlib 6.2, *)
1304+
@_alwaysEmitIntoClient
13041305
public var mutableSpan: MutableSpan<Element> {
1305-
@lifetime(/*inout*/&self)
1306-
@_alwaysEmitIntoClient
1306+
@lifetime(&self)
13071307
mutating get {
13081308
// _makeMutableAndUnique*() inserts begin_cow_mutation.
13091309
// LifetimeDependence analysis inserts call to end_cow_mutation_addr since we cannot schedule it in the stdlib for mutableSpan property.

stdlib/public/core/CollectionOfOne.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ extension CollectionOfOne {
173173
}
174174

175175
@available(SwiftStdlib 6.2, *)
176+
@_alwaysEmitIntoClient
176177
public var mutableSpan: MutableSpan<Element> {
177178
@lifetime(&self)
178-
@_alwaysEmitIntoClient
179179
mutating get {
180180
let pointer = unsafe UnsafeMutablePointer<Element>(
181181
Builtin.addressOfBorrow(self)

stdlib/public/core/ContiguousArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,9 +1243,9 @@ extension ContiguousArray {
12431243
}
12441244

12451245
@available(SwiftStdlib 6.2, *)
1246+
@_alwaysEmitIntoClient
12461247
public var mutableSpan: MutableSpan<Element> {
12471248
@lifetime(&self)
1248-
@_alwaysEmitIntoClient
12491249
mutating get {
12501250
// _makeMutableAndUnique*() inserts begin_cow_mutation.
12511251
// LifetimeDependence analysis inserts call to end_cow_mutation_addr since we cannot schedule it in the stdlib for mutableSpan property.

stdlib/public/core/InlineArray.swift

Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ extension InlineArray where Element: ~Copyable {
8080
unsafe UnsafeBufferPointer<Element>(start: _address, count: count)
8181
}
8282

83+
/// Returns a pointer to the first element in the array while performing stack
84+
/// checking.
85+
///
86+
/// Use this when the value of the pointer could potentially be directly used
87+
/// by users (e.g. through the use of span or the unchecked subscript).
88+
@available(SwiftStdlib 6.2, *)
89+
@_alwaysEmitIntoClient
90+
@_transparent
91+
internal var _protectedAddress: UnsafePointer<Element> {
92+
#if $AddressOfProperty
93+
unsafe UnsafePointer<Element>(Builtin.addressOfBorrow(_storage))
94+
#else
95+
unsafe UnsafePointer<Element>(Builtin.addressOfBorrow(self))
96+
#endif
97+
}
98+
99+
/// Returns a buffer pointer over the entire array while performing stack
100+
/// checking.
101+
///
102+
/// Use this when the value of the pointer could potentially be directly used
103+
/// by users (e.g. through the use of span or the unchecked subscript).
104+
@available(SwiftStdlib 6.2, *)
105+
@_alwaysEmitIntoClient
106+
@_transparent
107+
internal var _protectedBuffer: UnsafeBufferPointer<Element> {
108+
unsafe UnsafeBufferPointer<Element>(start: _protectedAddress, count: count)
109+
}
110+
83111
/// Returns a mutable pointer to the first element in the array.
84112
@available(SwiftStdlib 6.2, *)
85113
@_alwaysEmitIntoClient
@@ -103,6 +131,41 @@ extension InlineArray where Element: ~Copyable {
103131
}
104132
}
105133

134+
/// Returns a mutable pointer to the first element in the array while
135+
/// performing stack checking.
136+
///
137+
/// Use this when the value of the pointer could potentially be directly used
138+
/// by users (e.g. through the use of span or the unchecked subscript).
139+
@available(SwiftStdlib 6.2, *)
140+
@_alwaysEmitIntoClient
141+
@_transparent
142+
internal var _protectedMutableAddress: UnsafeMutablePointer<Element> {
143+
mutating get {
144+
#if $AddressOfProperty
145+
unsafe UnsafeMutablePointer<Element>(Builtin.addressof(&_storage))
146+
#else
147+
unsafe UnsafeMutablePointer<Element>(Builtin.addressof(&self))
148+
#endif
149+
}
150+
}
151+
152+
/// Returns a mutable buffer pointer over the entire array while performing
153+
/// stack checking.
154+
///
155+
/// Use this when the value of the pointer could potentially be directly used
156+
/// by users (e.g. through the use of span or the unchecked subscript).
157+
@available(SwiftStdlib 6.2, *)
158+
@_alwaysEmitIntoClient
159+
@_transparent
160+
internal var _protectedMutableBuffer: UnsafeMutableBufferPointer<Element> {
161+
mutating get {
162+
unsafe UnsafeMutableBufferPointer<Element>(
163+
start: _protectedMutableAddress,
164+
count: count
165+
)
166+
}
167+
}
168+
106169
/// Converts the given raw pointer, which points at an uninitialized array
107170
/// instance, to a mutable buffer suitable for initialization.
108171
@available(SwiftStdlib 6.2, *)
@@ -407,12 +470,12 @@ extension InlineArray where Element: ~Copyable {
407470
public subscript(unchecked i: Index) -> Element {
408471
@_transparent
409472
unsafeAddress {
410-
unsafe _address + i
473+
unsafe _protectedAddress + i
411474
}
412475

413476
@_transparent
414477
unsafeMutableAddress {
415-
unsafe _mutableAddress + i
478+
unsafe _protectedMutableAddress + i
416479
}
417480
}
418481
}
@@ -459,53 +522,28 @@ extension InlineArray where Element: ~Copyable {
459522

460523
@available(SwiftStdlib 6.2, *)
461524
extension InlineArray where Element: ~Copyable {
462-
463525
@available(SwiftStdlib 6.2, *)
526+
@_alwaysEmitIntoClient
464527
public var span: Span<Element> {
465528
@lifetime(borrow self)
466-
@_alwaysEmitIntoClient
529+
@_transparent
467530
borrowing get {
468-
let pointer = unsafe _address
469-
let span = unsafe Span(_unsafeStart: pointer, count: count)
531+
let span = unsafe Span(_unsafeStart: _protectedAddress, count: count)
470532
return unsafe _overrideLifetime(span, borrowing: self)
471533
}
472534
}
473535

474536
@available(SwiftStdlib 6.2, *)
537+
@_alwaysEmitIntoClient
475538
public var mutableSpan: MutableSpan<Element> {
476539
@lifetime(&self)
477-
@_alwaysEmitIntoClient
540+
@_transparent
478541
mutating get {
479-
let pointer = unsafe _mutableAddress
480-
let span = unsafe MutableSpan(_unsafeStart: pointer, count: count)
542+
let span = unsafe MutableSpan(
543+
_unsafeStart: _protectedMutableAddress,
544+
count: count
545+
)
481546
return unsafe _overrideLifetime(span, mutating: &self)
482547
}
483548
}
484549
}
485-
486-
//===----------------------------------------------------------------------===//
487-
// MARK: - Unsafe APIs
488-
//===----------------------------------------------------------------------===//
489-
490-
@available(SwiftStdlib 6.2, *)
491-
extension InlineArray where Element: ~Copyable {
492-
// FIXME: @available(*, deprecated, renamed: "span.withUnsafeBufferPointer(_:)")
493-
@available(SwiftStdlib 6.2, *)
494-
@_alwaysEmitIntoClient
495-
@_transparent
496-
public borrowing func _withUnsafeBufferPointer<Result: ~Copyable, E: Error>(
497-
_ body: (UnsafeBufferPointer<Element>) throws(E) -> Result
498-
) throws(E) -> Result {
499-
try unsafe body(_buffer)
500-
}
501-
502-
// FIXME: @available(*, deprecated, renamed: "mutableSpan.withUnsafeMutableBufferPointer(_:)")
503-
@available(SwiftStdlib 6.2, *)
504-
@_alwaysEmitIntoClient
505-
@_transparent
506-
public mutating func _withUnsafeMutableBufferPointer<Result: ~Copyable, E: Error>(
507-
_ body: (UnsafeMutableBufferPointer<Element>) throws(E) -> Result
508-
) throws(E) -> Result {
509-
try unsafe body(_mutableBuffer)
510-
}
511-
}

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,21 +575,22 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
575575

576576
@unsafe
577577
@available(SwiftStdlib 6.2, *)
578+
@_alwaysEmitIntoClient
578579
public var span: Span<Element> {
579580
@lifetime(borrow self)
580-
@_alwaysEmitIntoClient
581+
@_transparent
581582
get {
582-
let span = unsafe Span(_unsafeElements: self)
583-
return unsafe _overrideLifetime(span, borrowing: self)
583+
unsafe Span(_unsafeElements: self)
584584
}
585585
}
586586
%if Mutable:
587587

588588
@unsafe
589589
@available(SwiftStdlib 6.2, *)
590+
@_alwaysEmitIntoClient
590591
public var mutableSpan: MutableSpan<Element> {
591592
@lifetime(borrow self)
592-
@_alwaysEmitIntoClient
593+
@_transparent
593594
get {
594595
unsafe MutableSpan(_unsafeElements: self)
595596
}

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,9 +1164,10 @@ extension Unsafe${Mutable}RawBufferPointer {
11641164

11651165
@unsafe
11661166
@available(SwiftStdlib 6.2, *)
1167+
@_alwaysEmitIntoClient
11671168
public var mutableBytes: MutableRawSpan {
11681169
@lifetime(borrow self)
1169-
@_alwaysEmitIntoClient
1170+
@_transparent
11701171
get {
11711172
unsafe MutableRawSpan(_unsafeBytes: self)
11721173
}
@@ -1184,12 +1185,12 @@ extension Unsafe${Mutable}RawBufferPointer {
11841185

11851186
@unsafe
11861187
@available(SwiftStdlib 6.2, *)
1188+
@_alwaysEmitIntoClient
11871189
public var bytes: RawSpan {
11881190
@lifetime(borrow self)
1189-
@_alwaysEmitIntoClient
1191+
@_transparent
11901192
get {
1191-
let span = unsafe RawSpan(_unsafeBytes: self)
1192-
return unsafe _overrideLifetime(span, borrowing: self)
1193+
unsafe RawSpan(_unsafeBytes: self)
11931194
}
11941195
}
11951196
}

test/abi/macOS/arm64/stdlib.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -927,15 +927,6 @@ Added: _$ss14MutableRawSpanVN
927927
Added: _$sSS8UTF8ViewV4spans4SpanVys5UInt8VGvg
928928
Added: _$sSs8UTF8ViewV4spans4SpanVys5UInt8VGvg
929929

930-
// SE-0467 mutableSpan properties
931-
Added: _$sSa11mutableSpans07MutableB0VyxGvr
932-
Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
933-
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
934-
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
935-
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
936-
Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr
937-
Added: _$sSw12mutableBytess14MutableRawSpanVvr
938-
939930
// _SwiftifyInfo enum for _SwiftifyImports macro
940931
Added: _$ss13_SwiftifyExprO5paramyABSicABmFWC
941932
Added: _$ss13_SwiftifyExprO6returnyA2BmFWC
@@ -1085,3 +1076,8 @@ Added: _$ss8UTF8SpanV9_asciiBits6UInt64VvpZMV
10851076

10861077
// printing foreign reference types requires a new displayStyle: .foreign
10871078
Added: _$ss6MirrorV12DisplayStyleO16foreignReferenceyA2DmFWC
1079+
1080+
// var InlineArray._protectedBuffer
1081+
// var InlineArray._protectedAddress
1082+
Added: _$ss11InlineArrayVsRi__rlE16_protectedBufferSRyq_GvpMV
1083+
Added: _$ss11InlineArrayVsRi__rlE17_protectedAddressSPyq_GvpMV

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -928,15 +928,6 @@ Added: _$ss14MutableRawSpanVN
928928
Added: _$sSS8UTF8ViewV4spans4SpanVys5UInt8VGvg
929929
Added: _$sSs8UTF8ViewV4spans4SpanVys5UInt8VGvg
930930

931-
// SE-0467 mutableSpan properties
932-
Added: _$sSa11mutableSpans07MutableB0VyxGvr
933-
Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
934-
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
935-
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
936-
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
937-
Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr
938-
Added: _$sSw12mutableBytess14MutableRawSpanVvr
939-
940931
// _SwiftifyInfo enum for _SwiftifyImports macro
941932
Added: _$ss13_SwiftifyExprO5paramyABSicABmFWC
942933
Added: _$ss13_SwiftifyExprO6returnyA2BmFWC
@@ -1085,3 +1076,8 @@ Added: _$ss8UTF8SpanV9_asciiBits6UInt64VvpZMV
10851076

10861077
// printing foreign reference types requires a new displayStyle: .foreign
10871078
Added: _$ss6MirrorV12DisplayStyleO16foreignReferenceyA2DmFWC
1079+
1080+
// var InlineArray._protectedBuffer
1081+
// var InlineArray._protectedAddress
1082+
Added: _$ss11InlineArrayVsRi__rlE16_protectedBufferSRyq_GvpMV
1083+
Added: _$ss11InlineArrayVsRi__rlE17_protectedAddressSPyq_GvpMV

0 commit comments

Comments
 (0)