From 76d22cc6750fc93d45e2add05b60a7273ccd86c3 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Mon, 11 Nov 2024 16:10:36 -0500 Subject: [PATCH] [span] correctly express lifetimes for initializers --- stdlib/public/core/Span/RawSpan.swift | 39 +++++++++++---------------- stdlib/public/core/Span/Span.swift | 33 ++++++++--------------- 2 files changed, 26 insertions(+), 46 deletions(-) diff --git a/stdlib/public/core/Span/RawSpan.swift b/stdlib/public/core/Span/RawSpan.swift index 7a5b3486e3986..f20638eddacb6 100644 --- a/stdlib/public/core/Span/RawSpan.swift +++ b/stdlib/public/core/Span/RawSpan.swift @@ -61,8 +61,7 @@ public struct RawSpan: ~Escapable, Copyable, BitwiseCopyable { @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient @inline(__always) - //FIXME: should be @lifetime(borrow pointer) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow pointer) internal init( _unchecked pointer: borrowing UnsafeRawPointer?, byteCount: Int @@ -90,8 +89,7 @@ extension RawSpan { /// - buffer: an `UnsafeRawBufferPointer` to initialized memory. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeBytes buffer: borrowing UnsafeRawBufferPointer ) { @@ -110,8 +108,7 @@ extension RawSpan { /// - buffer: an `UnsafeRawBufferPointer` to initialized memory. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeBytes buffer: borrowing Slice ) { @@ -128,8 +125,7 @@ extension RawSpan { /// - buffer: an `UnsafeRawBufferPointer` to initialized memory. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeBytes buffer: borrowing UnsafeMutableRawBufferPointer ) { @@ -138,8 +134,7 @@ extension RawSpan { @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeBytes buffer: borrowing Slice ) { @@ -158,8 +153,7 @@ extension RawSpan { /// - byteCount: the number of initialized bytes in the span. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow pointer) public init( _unsafeStart pointer: borrowing UnsafeRawPointer, byteCount: Int @@ -178,8 +172,7 @@ extension RawSpan { /// - buffer: an `UnsafeRawBufferPointer` to initialized memory. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeElements buffer: borrowing UnsafeBufferPointer ) { @@ -196,8 +189,7 @@ extension RawSpan { /// - buffer: an `UnsafeRawBufferPointer` to initialized memory. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeElements buffer: borrowing Slice> ) { @@ -216,8 +208,7 @@ extension RawSpan { /// - buffer: an `UnsafeRawBufferPointer` to initialized memory. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeElements buffer: borrowing UnsafeMutableBufferPointer ) { @@ -234,8 +225,7 @@ extension RawSpan { /// - buffer: an `UnsafeRawBufferPointer` to initialized memory. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeElements buffer: borrowing Slice> ) { @@ -256,8 +246,7 @@ extension RawSpan { /// - byteCount: the number of initialized bytes in the span. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow pointer) public init( _unsafeStart pointer: borrowing UnsafePointer, count: Int @@ -275,8 +264,10 @@ extension RawSpan { /// `RawSpan`'s lifetime and the memory it represents. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - @lifetime(span) - public init(_elements span: consuming Span) { + @lifetime(borrow span) + public init( + _elements span: borrowing Span + ) { self.init( _unchecked: span._pointer, byteCount: span.count &* MemoryLayout.stride diff --git a/stdlib/public/core/Span/Span.swift b/stdlib/public/core/Span/Span.swift index 2561f471c7b46..1640e3fb35a23 100644 --- a/stdlib/public/core/Span/Span.swift +++ b/stdlib/public/core/Span/Span.swift @@ -63,8 +63,7 @@ public struct Span @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient @inline(__always) - //FIXME: should be @lifetime(borrow pointer) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow pointer) internal init( _unchecked pointer: borrowing UnsafeRawPointer?, count: Int @@ -92,8 +91,7 @@ extension Span where Element: ~Copyable { /// - buffer: an `UnsafeBufferPointer` to initialized elements. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeElements buffer: borrowing UnsafeBufferPointer ) { @@ -116,8 +114,7 @@ extension Span where Element: ~Copyable { /// - buffer: an `UnsafeMutableBufferPointer` to initialized elements. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeElements buffer: borrowing UnsafeMutableBufferPointer ) { @@ -136,8 +133,7 @@ extension Span where Element: ~Copyable { /// - count: the number of initialized elements in the span. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow pointer) public init( _unsafeStart pointer: borrowing UnsafePointer, count: Int @@ -161,8 +157,7 @@ extension Span { /// - buffer: an `UnsafeBufferPointer` to initialized elements. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeElements buffer: borrowing Slice> ) { @@ -179,8 +174,7 @@ extension Span { /// - buffer: an `UnsafeMutableBufferPointer` to initialized elements. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeElements buffer: borrowing Slice> ) { @@ -206,8 +200,7 @@ extension Span where Element: BitwiseCopyable { /// - buffer: a buffer to initialized elements. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeBytes buffer: borrowing UnsafeRawBufferPointer ) { @@ -239,8 +232,7 @@ extension Span where Element: BitwiseCopyable { /// - buffer: a buffer to initialized elements. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeBytes buffer: borrowing UnsafeMutableRawBufferPointer ) { @@ -263,8 +255,7 @@ extension Span where Element: BitwiseCopyable { /// - byteCount: the number of initialized elements in the span. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow pointer) public init( _unsafeStart pointer: borrowing UnsafeRawPointer, byteCount: Int @@ -287,8 +278,7 @@ extension Span where Element: BitwiseCopyable { /// - buffer: a buffer to initialized elements. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeBytes buffer: borrowing Slice ) { @@ -309,8 +299,7 @@ extension Span where Element: BitwiseCopyable { /// - buffer: a buffer to initialized elements. @_disallowFeatureSuppression(NonescapableTypes) @_alwaysEmitIntoClient - //FIXME: should be @lifetime(borrow ) rdar://138672380 - @lifetime(immortal) + @lifetime(borrow buffer) public init( _unsafeBytes buffer: borrowing Slice ) {