Skip to content

Commit 88a242f

Browse files
committed
Update SpanExtras.swift for strict @Lifetime checking.
These tests can't be fully enabled until this is fixed: rdar://147194789 ([nonescapable] 'mutating get' causes a type checking error for non-existent _read accessor)
1 parent e919812 commit 88a242f

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

test/SILOptimizer/Inputs/SpanExtras.swift

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ extension MutableSpan where Element: BitwiseCopyable {
184184
extension Span where Element: ~Copyable {
185185

186186
@_alwaysEmitIntoClient
187+
@lifetime(borrow mutableSpan)
187188
public init(_unsafeMutableSpan mutableSpan: borrowing MutableSpan<Element>) {
188189
let pointer = mutableSpan._pointer?.assumingMemoryBound(to: Element.self)
189190
let buffer = UnsafeBufferPointer(start: pointer, count: mutableSpan.count)
@@ -215,6 +216,7 @@ extension MutableSpan where Element: ~Copyable {
215216
extension RawSpan {
216217

217218
@_alwaysEmitIntoClient
219+
@lifetime(borrow mutableSpan)
218220
public init<Element: BitwiseCopyable>(
219221
_unsafeMutableSpan mutableSpan: borrowing MutableSpan<Element>
220222
) {
@@ -286,7 +288,10 @@ extension MutableSpan where Element: BitwiseCopyable {
286288
/// - Returns: a RawSpan over the memory represented by this span
287289
@unsafe //FIXME: remove when the lifetime inference is fixed
288290
@_alwaysEmitIntoClient
289-
public var _unsafeRawSpan: RawSpan { RawSpan(_unsafeMutableSpan: self) }
291+
public var _unsafeRawSpan: RawSpan {
292+
@lifetime(borrow self)
293+
get { RawSpan(_unsafeMutableSpan: self) }
294+
}
290295
}
291296

292297
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *)
@@ -300,10 +305,12 @@ extension MutableSpan where Element: ~Copyable {
300305
/// - Complexity: O(1)
301306
@_alwaysEmitIntoClient
302307
public subscript(_ position: Index) -> Element {
308+
@lifetime(borrow self)
303309
_read {
304310
precondition(indices.contains(position), "index out of bounds")
305311
yield self[unchecked: position]
306312
}
313+
@lifetime(borrow self)
307314
_modify {
308315
precondition(indices.contains(position), "index out of bounds")
309316
yield &self[unchecked: position]
@@ -323,6 +330,7 @@ extension MutableSpan where Element: ~Copyable {
323330
unsafeAddress {
324331
UnsafePointer(_unsafeAddressOfElement(unchecked: position))
325332
}
333+
@lifetime(self: copy self)
326334
unsafeMutableAddress {
327335
_unsafeAddressOfElement(unchecked: position)
328336
}
@@ -342,12 +350,14 @@ extension MutableSpan where Element: ~Copyable {
342350
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *)
343351
extension MutableSpan where Element: ~Copyable {
344352

353+
@lifetime(self: copy self)
345354
public mutating func swapAt(_ i: Index, _ j: Index) {
346355
precondition(indices.contains(Index(i)))
347356
precondition(indices.contains(Index(j)))
348357
swapAt(unchecked: i, unchecked: j)
349358
}
350359

360+
@lifetime(self: copy self)
351361
public mutating func swapAt(unchecked i: Index, unchecked j: Index) {
352362
let pi = _unsafeAddressOfElement(unchecked: i)
353363
let pj = _unsafeAddressOfElement(unchecked: j)
@@ -372,6 +382,7 @@ extension MutableSpan where Element: BitwiseCopyable {
372382
precondition(indices.contains(position))
373383
return self[unchecked: position]
374384
}
385+
@lifetime(self: copy self)
375386
set {
376387
precondition(indices.contains(position))
377388
self[unchecked: position] = newValue
@@ -392,6 +403,7 @@ extension MutableSpan where Element: BitwiseCopyable {
392403
let offset = position&*MemoryLayout<Element>.stride
393404
return _start().loadUnaligned(fromByteOffset: offset, as: Element.self)
394405
}
406+
@lifetime(self: copy self)
395407
set {
396408
let offset = position&*MemoryLayout<Element>.stride
397409
_start().storeBytes(of: newValue, toByteOffset: offset, as: Element.self)
@@ -412,6 +424,7 @@ extension MutableSpan where Element: ~Copyable {
412424

413425
//FIXME: mark closure parameter as non-escaping
414426
@_alwaysEmitIntoClient
427+
@lifetime(self: copy self)
415428
public mutating func withUnsafeMutableBufferPointer<E: Error, Result: ~Copyable>(
416429
_ body: (UnsafeMutableBufferPointer<Element>) throws(E) -> Result
417430
) throws(E) -> Result {
@@ -440,6 +453,7 @@ extension MutableSpan where Element: BitwiseCopyable {
440453

441454
//FIXME: mark closure parameter as non-escaping
442455
@_alwaysEmitIntoClient
456+
@lifetime(self: copy self)
443457
public mutating func withUnsafeMutableBytes<E: Error, Result: ~Copyable>(
444458
_ body: (_ buffer: UnsafeMutableRawBufferPointer) throws(E) -> Result
445459
) throws(E) -> Result {
@@ -456,13 +470,15 @@ extension MutableSpan where Element: BitwiseCopyable {
456470
extension MutableSpan {
457471

458472
@_alwaysEmitIntoClient
473+
@lifetime(self: copy self)
459474
public mutating func update(repeating repeatedValue: Element) {
460475
_start().withMemoryRebound(to: Element.self, capacity: count) {
461476
$0.update(repeating: repeatedValue, count: count)
462477
}
463478
}
464479

465480
@_alwaysEmitIntoClient
481+
@lifetime(self: copy self)
466482
public mutating func update<S: Sequence>(
467483
from source: S
468484
) -> (unwritten: S.Iterator, index: Index) where S.Element == Element {
@@ -472,6 +488,7 @@ extension MutableSpan {
472488
}
473489

474490
@_alwaysEmitIntoClient
491+
@lifetime(self: copy self)
475492
public mutating func update(
476493
from elements: inout some IteratorProtocol<Element>
477494
) -> Index {
@@ -485,6 +502,7 @@ extension MutableSpan {
485502
}
486503

487504
@_alwaysEmitIntoClient
505+
@lifetime(self: copy self)
488506
public mutating func update(
489507
fromContentsOf source: some Collection<Element>
490508
) -> Index {
@@ -505,6 +523,7 @@ extension MutableSpan {
505523
}
506524

507525
@_alwaysEmitIntoClient
526+
@lifetime(self: copy self)
508527
public mutating func update(fromContentsOf source: Span<Element>) -> Index {
509528
guard !source.isEmpty else { return 0 }
510529
precondition(
@@ -520,6 +539,7 @@ extension MutableSpan {
520539
}
521540

522541
@_alwaysEmitIntoClient
542+
@lifetime(self: copy self)
523543
public mutating func update(
524544
fromContentsOf source: borrowing MutableSpan<Element>
525545
) -> Index {
@@ -532,6 +552,7 @@ extension MutableSpan {
532552
extension MutableSpan where Element: BitwiseCopyable {
533553

534554
@_alwaysEmitIntoClient
555+
@lifetime(self: copy self)
535556
public mutating func update(
536557
repeating repeatedValue: Element
537558
) where Element: BitwiseCopyable {
@@ -544,6 +565,7 @@ extension MutableSpan where Element: BitwiseCopyable {
544565
}
545566

546567
@_alwaysEmitIntoClient
568+
@lifetime(self: copy self)
547569
public mutating func update<S: Sequence>(
548570
from source: S
549571
) -> (unwritten: S.Iterator, index: Index)
@@ -554,6 +576,7 @@ extension MutableSpan where Element: BitwiseCopyable {
554576
}
555577

556578
@_alwaysEmitIntoClient
579+
@lifetime(self: copy self)
557580
public mutating func update(
558581
from elements: inout some IteratorProtocol<Element>
559582
) -> Index {
@@ -567,6 +590,7 @@ extension MutableSpan where Element: BitwiseCopyable {
567590
}
568591

569592
@_alwaysEmitIntoClient
593+
@lifetime(self: copy self)
570594
public mutating func update(
571595
fromContentsOf source: some Collection<Element>
572596
) -> Index where Element: BitwiseCopyable {
@@ -587,6 +611,7 @@ extension MutableSpan where Element: BitwiseCopyable {
587611
}
588612

589613
@_alwaysEmitIntoClient
614+
@lifetime(self: copy self)
590615
public mutating func update(
591616
fromContentsOf source: Span<Element>
592617
) -> Index where Element: BitwiseCopyable {
@@ -604,6 +629,7 @@ extension MutableSpan where Element: BitwiseCopyable {
604629
}
605630

606631
@_alwaysEmitIntoClient
632+
@lifetime(self: copy self)
607633
public mutating func update(
608634
fromContentsOf source: borrowing MutableSpan<Element>
609635
) -> Index where Element: BitwiseCopyable {
@@ -667,6 +693,7 @@ extension OutputSpan where Element: ~Copyable {
667693
@available(macOS 9999, *)
668694
@available(macOS 9999, *)
669695
@_alwaysEmitIntoClient
696+
@lifetime(self: copy self)
670697
public mutating func append(_ value: consuming Element) {
671698
precondition(_initialized < capacity, "Output buffer overflow")
672699
let p = _start.advanced(by: _initialized&*MemoryLayout<Element>.stride)
@@ -696,6 +723,7 @@ extension OutputSpan where Element: ~Copyable {
696723
extension OutputSpan {
697724

698725
@_alwaysEmitIntoClient
726+
@lifetime(self: copy self)
699727
public mutating func append(repeating repeatedValue: Element, count: Int) {
700728
let available = capacity &- _initialized
701729
precondition(
@@ -711,6 +739,7 @@ extension OutputSpan {
711739
}
712740

713741
@_alwaysEmitIntoClient
742+
@lifetime(self: copy self)
714743
public mutating func append<S>(
715744
from elements: S
716745
) -> S.Iterator where S: Sequence, S.Element == Element {
@@ -720,6 +749,7 @@ extension OutputSpan {
720749
}
721750

722751
@_alwaysEmitIntoClient
752+
@lifetime(self: copy self)
723753
public mutating func append(
724754
from elements: inout some IteratorProtocol<Element>
725755
) {
@@ -732,6 +762,7 @@ extension OutputSpan {
732762
}
733763

734764
@_alwaysEmitIntoClient
765+
@lifetime(self: copy self)
735766
public mutating func append(
736767
fromContentsOf source: some Collection<Element>
737768
) {
@@ -762,6 +793,7 @@ extension OutputSpan {
762793
}
763794

764795
//FIXME: remove once rdar://136838539 & rdar://136849171 are fixed
796+
@lifetime(self: copy self)
765797
public mutating func append(
766798
fromContentsOf source: UnsafeBufferPointer<Element>
767799
) {
@@ -779,6 +811,7 @@ extension OutputSpan {
779811

780812
//FIXME: rdar://136838539 & rdar://136849171
781813
@_alwaysEmitIntoClient
814+
@lifetime(self: copy self)
782815
public mutating func append(
783816
fromContentsOf source: Span<Element>
784817
) {
@@ -797,6 +830,7 @@ extension OutputSpan {
797830
}
798831

799832
@_alwaysEmitIntoClient
833+
@lifetime(self: copy self)
800834
public mutating func append(fromContentsOf source: borrowing MutableSpan<Element>) {
801835
source.withUnsafeBufferPointer { append(fromContentsOf: $0) }
802836
}
@@ -806,6 +840,7 @@ extension OutputSpan {
806840
extension OutputSpan where Element: ~Copyable {
807841

808842
@_alwaysEmitIntoClient
843+
@lifetime(self: copy self)
809844
public mutating func moveAppend(
810845
fromContentsOf source: consuming Self
811846
) {
@@ -824,6 +859,7 @@ extension OutputSpan where Element: ~Copyable {
824859
}
825860

826861
@_alwaysEmitIntoClient
862+
@lifetime(self: copy self)
827863
public mutating func moveAppend(
828864
fromContentsOf source: UnsafeMutableBufferPointer<Element>
829865
) {
@@ -849,6 +885,7 @@ extension OutputSpan where Element: ~Copyable {
849885
extension OutputSpan {
850886

851887
@_alwaysEmitIntoClient
888+
@lifetime(self: copy self)
852889
public mutating func moveAppend(
853890
fromContentsOf source: Slice<UnsafeMutableBufferPointer<Element>>
854891
) {
@@ -875,16 +912,19 @@ extension OutputSpan where Element: ~Copyable {
875912
}
876913
}
877914

915+
/* FIXME: rdar://147194789 ([nonescapable] 'mutating get' causes a
916+
type checking error for non-existent _read accessor)
878917
@_alwaysEmitIntoClient
879918
public var mutableSpan: MutableSpan<Element> {
880-
// @lifetime(inout self) // this just crashes the compiler
919+
@lifetime(borrow self)
881920
mutating get { // the accessor must provide a mutable projection
882921
let pointer = _pointer?.assumingMemoryBound(to: Element.self)
883922
let buffer = UnsafeMutableBufferPointer(start: pointer, count: _initialized)
884923
let span = MutableSpan(_unsafeElements: buffer)
885924
return _overrideLifetime(span, mutating: &self)
886925
}
887926
}
927+
*/
888928
}
889929

890930
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *)

0 commit comments

Comments
 (0)