@@ -18,6 +18,24 @@ internal func unsafeBitCast<T: ~Escapable, U>(
18
18
Builtin . reinterpretCast ( x)
19
19
}
20
20
21
+ /// Unsafely discard any lifetime dependency on the `dependent` argument. Return
22
+ /// a value identical to `dependent` with a lifetime dependency on the caller's
23
+ /// borrow scope of the `source` argument.
24
+ @unsafe
25
+ @_unsafeNonescapableResult
26
+ @_alwaysEmitIntoClient
27
+ @_transparent
28
+ @lifetime ( borrow source)
29
+ internal func _overrideLifetime<
30
+ T: ~ Copyable & ~ Escapable, U: ~ Copyable & ~ Escapable
31
+ > (
32
+ _ dependent: consuming T , borrowing source: borrowing U
33
+ ) -> T {
34
+ // TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
35
+ // should be expressed by a builtin that is hidden within the function body.
36
+ dependent
37
+ }
38
+
21
39
/// A C++ type that is an object that can refer to a contiguous sequence of objects.
22
40
///
23
41
/// C++ standard library type `std::span` conforms to this protocol.
@@ -62,12 +80,15 @@ extension CxxSpan {
62
80
@available ( SwiftStdlib 6 . 1 , * )
63
81
extension Span {
64
82
@_alwaysEmitIntoClient
65
- @lifetime ( immortal)
66
83
@unsafe
84
+ @lifetime ( borrow span)
67
85
public init < T: CxxSpan < Element > > (
68
- _unsafeCxxSpan span: T ,
86
+ _unsafeCxxSpan span: borrowing T ,
69
87
) {
70
- self . init ( _unsafeElements: . init( start: span. __dataUnsafe ( ) , count: Int ( span. size ( ) ) ) )
88
+ let buffer = UnsafeBufferPointer ( start: span. __dataUnsafe ( ) , count: Int ( span. size ( ) ) )
89
+ let newSpan = Span ( _unsafeElements: buffer)
90
+ // 'self' is limited to the caller's scope of the variable passed to the 'span' argument.
91
+ self = _overrideLifetime ( newSpan, borrowing: span)
71
92
}
72
93
}
73
94
0 commit comments