@@ -84,6 +84,34 @@ extension InlineArray where Element: ~Copyable {
8484 unsafe UnsafeBufferPointer< Element > ( start: _address, count: count)
8585 }
8686
87+ /// Returns a pointer to the first element in the array while performing stack
88+ /// checking.
89+ ///
90+ /// Use this when the value of the pointer could potentially be directly used
91+ /// by users (e.g. through the use of span or the unchecked subscript).
92+ @available ( SwiftStdlib 6 . 2 , * )
93+ @_alwaysEmitIntoClient
94+ @_transparent
95+ internal var _protectedAddress : UnsafePointer < Element > {
96+ #if $AddressOfProperty
97+ unsafe UnsafePointer< Element > ( Builtin . addressOfBorrow ( _storage) )
98+ #else
99+ unsafe UnsafePointer< Element > ( Builtin . addressOfBorrow ( self ) )
100+ #endif
101+ }
102+
103+ /// Returns a buffer pointer over the entire array while performing stack
104+ /// checking.
105+ ///
106+ /// Use this when the value of the pointer could potentially be directly used
107+ /// by users (e.g. through the use of span or the unchecked subscript).
108+ @available ( SwiftStdlib 6 . 2 , * )
109+ @_alwaysEmitIntoClient
110+ @_transparent
111+ internal var _protectedBuffer : UnsafeBufferPointer < Element > {
112+ unsafe UnsafeBufferPointer< Element > ( start: _protectedAddress, count: count)
113+ }
114+
87115 /// Returns a mutable pointer to the first element in the array.
88116 @available ( SwiftStdlib 6 . 2 , * )
89117 @_alwaysEmitIntoClient
@@ -111,6 +139,41 @@ extension InlineArray where Element: ~Copyable {
111139 }
112140 }
113141
142+ /// Returns a mutable pointer to the first element in the array while
143+ /// performing stack checking.
144+ ///
145+ /// Use this when the value of the pointer could potentially be directly used
146+ /// by users (e.g. through the use of span or the unchecked subscript).
147+ @available ( SwiftStdlib 6 . 2 , * )
148+ @_alwaysEmitIntoClient
149+ @_transparent
150+ internal var _protectedMutableAddress : UnsafeMutablePointer < Element > {
151+ mutating get {
152+ #if $AddressOfProperty
153+ unsafe UnsafeMutablePointer< Element > ( Builtin . addressof ( & _storage) )
154+ #else
155+ unsafe UnsafeMutablePointer< Element > ( Builtin . addressof ( & self ) )
156+ #endif
157+ }
158+ }
159+
160+ /// Returns a mutable buffer pointer over the entire array while performing
161+ /// stack checking.
162+ ///
163+ /// Use this when the value of the pointer could potentially be directly used
164+ /// by users (e.g. through the use of span or the unchecked subscript).
165+ @available ( SwiftStdlib 6 . 2 , * )
166+ @_alwaysEmitIntoClient
167+ @_transparent
168+ internal var _protectedMutableBuffer : UnsafeMutableBufferPointer < Element > {
169+ mutating get {
170+ unsafe UnsafeMutableBufferPointer< Element > (
171+ start: _protectedMutableAddress,
172+ count: count
173+ )
174+ }
175+ }
176+
114177 /// Converts the given raw pointer, which points at an uninitialized array
115178 /// instance, to a mutable buffer suitable for initialization.
116179 @available ( SwiftStdlib 6 . 2 , * )
@@ -415,12 +478,12 @@ extension InlineArray where Element: ~Copyable {
415478 public subscript( unchecked i: Index ) -> Element {
416479 @_transparent
417480 unsafeAddress {
418- unsafe _address + i
481+ unsafe _protectedAddress + i
419482 }
420483
421484 @_transparent
422485 unsafeMutableAddress {
423- unsafe _mutableAddress + i
486+ unsafe _protectedMutableAddress + i
424487 }
425488 }
426489}
@@ -467,53 +530,28 @@ extension InlineArray where Element: ~Copyable {
467530
468531@available ( SwiftStdlib 6 . 2 , * )
469532extension InlineArray where Element: ~ Copyable {
470-
471533 @available ( SwiftStdlib 6 . 2 , * )
534+ @_alwaysEmitIntoClient
472535 public var span: Span< Element > {
473536 @lifetime ( borrow self)
474- @_alwaysEmitIntoClient
537+ @_transparent
475538 borrowing get {
476- let pointer = unsafe _address
477- let span = unsafe Span( _unsafeStart: pointer, count: count)
539+ let span = unsafe Span( _unsafeStart: _protectedAddress, count: count)
478540 return unsafe _override Lifetime( span, borrowing : self)
479541 }
480542 }
481543
482544 @available ( SwiftStdlib 6 . 2 , * )
545+ @_alwaysEmitIntoClient
483546 public var mutableSpan : MutableSpan < Element > {
484547 @lifetime ( & self )
485- @_alwaysEmitIntoClient
548+ @_transparent
486549 mutating get {
487- let pointer = unsafe _mutableAddress
488- let span = unsafe MutableSpan( _unsafeStart: pointer, count: count)
550+ let span = unsafe MutableSpan(
551+ _unsafeStart: _protectedMutableAddress,
552+ count: count
553+ )
489554 return unsafe _override Lifetime ( span, mutating: & self )
490555 }
491556 }
492557}
493-
494- //===----------------------------------------------------------------------===//
495- // MARK: - Unsafe APIs
496- //===----------------------------------------------------------------------===//
497-
498- @available ( SwiftStdlib 6 . 2 , * )
499- extension InlineArray where Element: ~ Copyable {
500- // FIXME: @available(*, deprecated, renamed: "span.withUnsafeBufferPointer(_:)")
501- @available ( SwiftStdlib 6 . 2 , * )
502- @_alwaysEmitIntoClient
503- @_transparent
504- public borrowing func _withUnsafeBufferPointer< Result: ~ Copyable, E: Error > (
505- _ body: ( UnsafeBufferPointer < Element > ) throws ( E ) -> Result
506- ) throws ( E) -> Result {
507- try unsafe body( _buffer)
508- }
509-
510- // FIXME: @available(*, deprecated, renamed: "mutableSpan.withUnsafeMutableBufferPointer(_:)")
511- @available ( SwiftStdlib 6 . 2 , * )
512- @_alwaysEmitIntoClient
513- @_transparent
514- public mutating func _withUnsafeMutableBufferPointer< Result: ~ Copyable, E: Error > (
515- _ body: ( UnsafeMutableBufferPointer < Element > ) throws ( E ) -> Result
516- ) throws ( E) -> Result {
517- try unsafe body( _mutableBuffer)
518- }
519- }
0 commit comments