@@ -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 , * )
461524extension 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 _override Lifetime( 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 _override Lifetime ( 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- }
0 commit comments