From 9e2fc09c3e97dec71fdcb98eb679b09df0c54817 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 8 Apr 2025 14:42:17 -0700 Subject: [PATCH] Update Span/* to build with current snapshots --- Sources/Future/Span/LifetimeOverride.swift | 2 +- Sources/Future/Span/MutableRawSpan.swift | 2 + Sources/Future/Span/MutableSpan.swift | 21 +++++++++ Sources/Future/Span/MutableSpanSlicing.swift | 18 +++++--- Sources/Future/Span/OutputSpan.swift | 46 +++++++++++-------- Sources/Future/Span/SpanExtensions.swift | 6 +-- .../Span/StdlibOutputSpanExtensions.swift | 6 +-- .../Span/UnsafeBufferPointer+Additions.swift | 10 ++-- 8 files changed, 75 insertions(+), 36 deletions(-) diff --git a/Sources/Future/Span/LifetimeOverride.swift b/Sources/Future/Span/LifetimeOverride.swift index ed416cfa..53940989 100644 --- a/Sources/Future/Span/LifetimeOverride.swift +++ b/Sources/Future/Span/LifetimeOverride.swift @@ -35,7 +35,7 @@ internal func _overrideLifetime< @_unsafeNonescapableResult @_alwaysEmitIntoClient @_transparent -@lifetime(source) +@lifetime(copy source) internal func _overrideLifetime< T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable >( diff --git a/Sources/Future/Span/MutableRawSpan.swift b/Sources/Future/Span/MutableRawSpan.swift index 3a2770a1..8e5cd9f8 100644 --- a/Sources/Future/Span/MutableRawSpan.swift +++ b/Sources/Future/Span/MutableRawSpan.swift @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +#if false // FIXME: Revive // A MutableRawSpan represents a span of memory which // contains initialized `Element` instances. @safe @@ -628,3 +629,4 @@ extension MutableRawSpan { return unsafe _overrideLifetime(newSpan, mutating: &self) } } +#endif diff --git a/Sources/Future/Span/MutableSpan.swift b/Sources/Future/Span/MutableSpan.swift index d552a93f..1b6aefaf 100644 --- a/Sources/Future/Span/MutableSpan.swift +++ b/Sources/Future/Span/MutableSpan.swift @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +#if false // FIXME: Revive import Builtin // A MutableSpan represents a span of memory which @@ -382,6 +383,7 @@ extension MutableSpan where Element: ~Copyable { //FIXME: mark closure parameter as non-escaping @_alwaysEmitIntoClient + @lifetime(self: copy self) public func withUnsafeBufferPointer( _ body: (_ buffer: UnsafeBufferPointer) throws(E) -> Result ) throws(E) -> Result { @@ -390,6 +392,7 @@ extension MutableSpan where Element: ~Copyable { //FIXME: mark closure parameter as non-escaping @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func withUnsafeMutableBufferPointer< E: Error, Result: ~Copyable >( @@ -412,6 +415,7 @@ extension MutableSpan where Element: BitwiseCopyable { //FIXME: mark closure parameter as non-escaping @_alwaysEmitIntoClient + @lifetime(self: copy self) public func withUnsafeBytes( _ body: (_ buffer: UnsafeRawBufferPointer) throws(E) -> Result ) throws(E) -> Result { @@ -420,6 +424,7 @@ extension MutableSpan where Element: BitwiseCopyable { //FIXME: mark closure parameter as non-escaping @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func withUnsafeMutableBytes( _ body: (_ buffer: UnsafeMutableRawBufferPointer) throws(E) -> Result ) throws(E) -> Result { @@ -436,6 +441,7 @@ extension MutableSpan where Element: BitwiseCopyable { extension MutableSpan { @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update(repeating repeatedValue: consuming Element) { unsafe _start().withMemoryRebound(to: Element.self, capacity: count) { unsafe $0.update(repeating: repeatedValue, count: count) @@ -443,6 +449,7 @@ extension MutableSpan { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update( from source: S ) -> (unwritten: S.Iterator, index: Index) where S.Element == Element { @@ -452,6 +459,7 @@ extension MutableSpan { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update( from elements: inout some IteratorProtocol ) -> Index { @@ -465,6 +473,7 @@ extension MutableSpan { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update( fromContentsOf source: some Collection ) -> Index { @@ -487,6 +496,7 @@ extension MutableSpan { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update(fromContentsOf source: Span) -> Index { guard !source.isEmpty else { return 0 } precondition( @@ -504,6 +514,7 @@ extension MutableSpan { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update( fromContentsOf source: borrowing MutableSpan ) -> Index { @@ -515,6 +526,7 @@ extension MutableSpan { extension MutableSpan where Element: ~Copyable { @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func moveUpdate( fromContentsOf source: consuming OutputSpan ) -> Index { @@ -532,6 +544,7 @@ extension MutableSpan where Element: ~Copyable { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func moveUpdate( fromContentsOf source: UnsafeMutableBufferPointer ) -> Index { @@ -544,6 +557,7 @@ extension MutableSpan where Element: ~Copyable { extension MutableSpan { @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func moveUpdate( fromContentsOf source: Slice> ) -> Index { @@ -555,6 +569,7 @@ extension MutableSpan { extension MutableSpan where Element: BitwiseCopyable { @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update( repeating repeatedValue: Element ) where Element: BitwiseCopyable { @@ -568,6 +583,7 @@ extension MutableSpan where Element: BitwiseCopyable { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update( from source: S ) -> (unwritten: S.Iterator, index: Index) @@ -578,6 +594,7 @@ extension MutableSpan where Element: BitwiseCopyable { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update( from elements: inout some IteratorProtocol ) -> Index { @@ -591,6 +608,7 @@ extension MutableSpan where Element: BitwiseCopyable { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update( fromContentsOf source: some Collection ) -> Index where Element: BitwiseCopyable { @@ -613,6 +631,7 @@ extension MutableSpan where Element: BitwiseCopyable { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update( fromContentsOf source: Span ) -> Index where Element: BitwiseCopyable { @@ -631,6 +650,7 @@ extension MutableSpan where Element: BitwiseCopyable { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func update( fromContentsOf source: borrowing MutableSpan ) -> Index where Element: BitwiseCopyable { @@ -858,3 +878,4 @@ extension MutableSpan where Element: ~Copyable { return unsafe _overrideLifetime(newSpan, mutating: &self) } } +#endif diff --git a/Sources/Future/Span/MutableSpanSlicing.swift b/Sources/Future/Span/MutableSpanSlicing.swift index 7db6229b..28feaa34 100644 --- a/Sources/Future/Span/MutableSpanSlicing.swift +++ b/Sources/Future/Span/MutableSpanSlicing.swift @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +#if false // FIXME: Revive //MARK: Option 1 extracting() function @available(macOS 9999, *) extension MutableSpan where Element: ~Copyable { @@ -21,7 +22,7 @@ extension MutableSpan where Element: ~Copyable { UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count), "Index range out of bounds" ) - let newSpan = MutableSpan(_unchecked: _start(), uncheckedBounds: bounds) + let newSpan = unsafe MutableSpan(_unchecked: _start(), uncheckedBounds: bounds) return unsafe _overrideLifetime(newSpan, mutating: &self) } } @@ -36,7 +37,7 @@ extension MutableSpan where Element: ~Copyable { ) { let delta = bounds.lowerBound &* MemoryLayout.stride let newStart = unsafe pointer.advanced(by: delta) - let newSpan = Self( + let newSpan = unsafe Self( _unchecked: newStart, count: bounds.upperBound &- bounds.lowerBound ) self = unsafe _overrideLifetime(newSpan, borrowing: pointer) @@ -51,7 +52,7 @@ extension Span where Element: ~Copyable { _unchecked pointer: UnsafeMutableRawPointer, uncheckedBounds bounds: Range ) { - let mut = MutableSpan(_unchecked: pointer, uncheckedBounds: bounds) + let mut = unsafe MutableSpan(_unchecked: pointer, uncheckedBounds: bounds) let newSpan = mut.span self = unsafe _overrideLifetime(newSpan, borrowing: pointer) } @@ -69,7 +70,7 @@ extension MutableSpan where Element: ~Copyable { UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count), "Index range out of bounds" ) - let newSpan = MutableSpan(_unchecked: _start(), uncheckedBounds: bounds) + let newSpan = unsafe MutableSpan(_unchecked: _start(), uncheckedBounds: bounds) return unsafe _overrideLifetime(newSpan, mutating: &self) } } @@ -84,6 +85,7 @@ public struct SubMutableSpan public fileprivate(set) var offset: MutableSpan.Index public /*exclusive*/ var base: MutableSpan + @lifetime(copy _base) init(_ _offset: Index, _ _base: consuming MutableSpan) { offset = _offset base = _base @@ -100,6 +102,7 @@ extension SubMutableSpan where Element: ~Copyable { precondition(offset <= index && index < base.count) return unsafe UnsafePointer(base._unsafeAddressOfElement(unchecked: index)) } + @lifetime(self: copy self) unsafeMutableAddress { precondition(offset <= index && index < base.count) return unsafe base._unsafeAddressOfElement(unchecked: index) @@ -109,7 +112,7 @@ extension SubMutableSpan where Element: ~Copyable { public var span: Span { @lifetime(borrow self) borrowing get { - let newSpan = Span( + let newSpan = unsafe Span( _unchecked: base._start(), uncheckedBounds: offset..(bounds.lowerBound, prefix) @@ -148,6 +152,7 @@ extension MutableSpan where Element: ~Copyable { @available(macOS 9999, *) extension MutableSpan where Element: Copyable { + @lifetime(self: copy self) public mutating func update( in bounds: Range, repeating repeatedValue: consuming Element @@ -163,3 +168,4 @@ extension MutableSpan where Element: Copyable { } } } +#endif diff --git a/Sources/Future/Span/OutputSpan.swift b/Sources/Future/Span/OutputSpan.swift index 4df8661b..d1e1cb4b 100644 --- a/Sources/Future/Span/OutputSpan.swift +++ b/Sources/Future/Span/OutputSpan.swift @@ -56,7 +56,7 @@ public struct OutputSpan: ~Copyable, ~Escapable { capacity: Int, initialized: Int ) { - _pointer = unsafe start + unsafe _pointer = start self.capacity = capacity _initialized = initialized } @@ -75,7 +75,7 @@ extension OutputSpan where Element: ~Copyable { _unchecked buffer: UnsafeMutableBufferPointer, initialized: Int ) { - _pointer = .init(buffer.baseAddress) + unsafe _pointer = .init(buffer.baseAddress) capacity = buffer.count _initialized = initialized } @@ -91,7 +91,7 @@ extension OutputSpan where Element: ~Copyable { (MemoryLayout.alignment&-1)) == 0), "baseAddress must be properly aligned to access Element" ) - self.init(_unchecked: buffer, initialized: initialized) + unsafe self.init(_unchecked: buffer, initialized: initialized) } @_alwaysEmitIntoClient @@ -103,7 +103,7 @@ extension OutputSpan where Element: ~Copyable { ) { precondition(capacity >= 0, "Capacity must be 0 or greater") let buf = unsafe UnsafeMutableBufferPointer(start: pointer, count: capacity) - let os = OutputSpan(_initializing: buf, initialized: initialized) + let os = unsafe OutputSpan(_initializing: buf, initialized: initialized) self = unsafe _overrideLifetime(os, borrowing: pointer) } } @@ -118,7 +118,7 @@ extension OutputSpan { initialized: Int = 0 ) { let rebased = unsafe UnsafeMutableBufferPointer(rebasing: buffer) - let os = OutputSpan(_initializing: rebased, initialized: 0) + let os = unsafe OutputSpan(_initializing: rebased, initialized: 0) self = unsafe unsafe _overrideLifetime(os, borrowing: buffer) } } @@ -141,7 +141,7 @@ extension OutputSpan where Element: BitwiseCopyable { let (count, remainder) = byteCount.quotientAndRemainder(dividingBy: stride) precondition(remainder == 0, "Span must contain a whole number of elements") let pointer = bytes.baseAddress - let os = OutputSpan( + let os = unsafe OutputSpan( _unchecked: pointer, capacity: count, initialized: initialized ) self = unsafe _overrideLifetime(os, borrowing: bytes) @@ -156,7 +156,7 @@ extension OutputSpan where Element: BitwiseCopyable { ) { precondition(capacity >= 0, "Capacity must be 0 or greater") let buf = unsafe UnsafeMutableRawBufferPointer(start: pointer, count: capacity) - let os = OutputSpan(_initializing: buf, initialized: initialized) + let os = unsafe OutputSpan(_initializing: buf, initialized: initialized) self = unsafe _overrideLifetime(os, borrowing: pointer) } @@ -167,7 +167,7 @@ extension OutputSpan where Element: BitwiseCopyable { initialized: Int = 0 ) { let rebased = unsafe UnsafeMutableRawBufferPointer(rebasing: buffer) - let os = OutputSpan(_initializing: rebased, initialized: initialized) + let os = unsafe OutputSpan(_initializing: rebased, initialized: initialized) self = unsafe _overrideLifetime(os, borrowing: buffer) } } @@ -176,6 +176,7 @@ extension OutputSpan where Element: BitwiseCopyable { extension OutputSpan where Element: ~Copyable { @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func append(_ value: consuming Element) { precondition(_initialized < capacity, "Output buffer overflow") let p = unsafe _start().advanced(by: _initialized&*MemoryLayout.stride) @@ -205,6 +206,7 @@ extension OutputSpan where Element: ~Copyable { extension OutputSpan { @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func append(repeating repeatedValue: Element, count: Int) { let available = capacity &- _initialized precondition( @@ -220,6 +222,7 @@ extension OutputSpan { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func append( from elements: S ) -> S.Iterator where S: Sequence, S.Element == Element { @@ -229,6 +232,7 @@ extension OutputSpan { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func append( from elements: inout some IteratorProtocol ) { @@ -241,11 +245,12 @@ extension OutputSpan { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func append( fromContentsOf source: some Collection ) { let void: Void? = source.withContiguousStorageIfAvailable { - append(fromContentsOf: Span(_unsafeElements: $0)) + append(fromContentsOf: unsafe Span(_unsafeElements: $0)) } if void != nil { return @@ -267,6 +272,7 @@ extension OutputSpan { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func append( fromContentsOf source: Span ) { @@ -276,7 +282,7 @@ extension OutputSpan { "destination span cannot contain every element from source." ) let tail = unsafe _start().advanced(by: _initialized&*MemoryLayout.stride) - _ = source.withUnsafeBufferPointer { + _ = unsafe source.withUnsafeBufferPointer { unsafe tail.initializeMemory( as: Element.self, from: $0.baseAddress!, count: $0.count ) @@ -285,8 +291,9 @@ extension OutputSpan { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func append(fromContentsOf source: borrowing MutableSpan) { - source.withUnsafeBufferPointer { append(fromContentsOf: $0) } + unsafe source.withUnsafeBufferPointer { unsafe append(fromContentsOf: $0) } } } @@ -294,6 +301,7 @@ extension OutputSpan { extension OutputSpan where Element: ~Copyable { @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func moveAppend( fromContentsOf source: consuming Self ) { @@ -312,10 +320,11 @@ extension OutputSpan where Element: ~Copyable { } @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func moveAppend( fromContentsOf source: UnsafeMutableBufferPointer ) { - let source = OutputSpan(_initializing: source, initialized: source.count) + let source = unsafe OutputSpan(_initializing: source, initialized: source.count) moveAppend(fromContentsOf: source) } } @@ -324,11 +333,12 @@ extension OutputSpan where Element: ~Copyable { extension OutputSpan { @_alwaysEmitIntoClient + @lifetime(self: copy self) public mutating func moveAppend( fromContentsOf source: Slice> ) { - moveAppend( - fromContentsOf: unsafe UnsafeMutableBufferPointer(rebasing: source) + unsafe moveAppend( + fromContentsOf: UnsafeMutableBufferPointer(rebasing: source) ) } } @@ -347,7 +357,7 @@ extension OutputSpan where Element: ~Copyable { borrowing get { let pointer = unsafe _pointer?.assumingMemoryBound(to: Element.self) let buffer = unsafe UnsafeBufferPointer(start: pointer, count: _initialized) - let span = Span(_unsafeElements: buffer) + let span = unsafe Span(_unsafeElements: buffer) return unsafe _overrideLifetime(span, borrowing: self) } } @@ -360,7 +370,7 @@ extension OutputSpan where Element: ~Copyable { let buffer = unsafe UnsafeMutableBufferPointer( start: pointer, count: _initialized ) - let span = MutableSpan(_unsafeElements: buffer) + let span = unsafe MutableSpan(_unsafeElements: buffer) return unsafe _overrideLifetime(span, mutating: &self) } } @@ -373,7 +383,7 @@ extension OutputSpan where Element: ~Copyable { @_alwaysEmitIntoClient public consuming func relinquishBorrowedMemory( ) -> UnsafeMutableBufferPointer { - let (start, count) = (self._pointer, self._initialized) + let (start, count) = unsafe (self._pointer, self._initialized) discard self let typed = unsafe start?.bindMemory(to: Element.self, capacity: count) return unsafe UnsafeMutableBufferPointer(start: typed, count: count) @@ -387,7 +397,7 @@ extension OutputSpan where Element: BitwiseCopyable { @_alwaysEmitIntoClient public consuming func relinquishBorrowedBytes( ) -> UnsafeMutableRawBufferPointer { - let (start, count) = (self._pointer, self._initialized) + let (start, count) = unsafe (self._pointer, self._initialized) discard self let byteCount = count&*MemoryLayout.stride return unsafe UnsafeMutableRawBufferPointer(start: start, count: byteCount) diff --git a/Sources/Future/Span/SpanExtensions.swift b/Sources/Future/Span/SpanExtensions.swift index c0806547..9cd081b1 100644 --- a/Sources/Future/Span/SpanExtensions.swift +++ b/Sources/Future/Span/SpanExtensions.swift @@ -20,7 +20,7 @@ extension Span { @lifetime(immortal) get { let empty = unsafe UnsafeBufferPointer(start: nil, count: 0) - let span = Span(_unsafeElements: empty) + let span = unsafe Span(_unsafeElements: empty) return unsafe _overrideLifetime(span, borrowing: immortalThing) } } @@ -29,7 +29,7 @@ extension Span { @lifetime(immortal) public init() { let empty = unsafe UnsafeBufferPointer(start: nil, count: 0) - let span = Span(_unsafeElements: empty) + let span = unsafe Span(_unsafeElements: empty) self = unsafe _overrideLifetime(span, borrowing: immortalThing) } } @@ -77,7 +77,7 @@ extension Span where Element: Equatable { @_alwaysEmitIntoClient public func _elementsEqual(_ other: some Collection) -> Bool { let equal = other.withContiguousStorageIfAvailable { - _elementsEqual(Span(_unsafeElements: $0)) + _elementsEqual(unsafe Span(_unsafeElements: $0)) } if let equal { return equal } diff --git a/Sources/Future/Span/StdlibOutputSpanExtensions.swift b/Sources/Future/Span/StdlibOutputSpanExtensions.swift index fce41a04..5a81b5bc 100644 --- a/Sources/Future/Span/StdlibOutputSpanExtensions.swift +++ b/Sources/Future/Span/StdlibOutputSpanExtensions.swift @@ -21,7 +21,7 @@ extension Array { unsafeUninitializedCapacity: capacity, initializingWith: { (buffer, count) in let pointer = unsafe buffer.baseAddress.unsafelyUnwrapped - var output = OutputSpan( + var output = unsafe OutputSpan( _initializing: pointer, capacity: buffer.count ) try initializer(&output) @@ -47,7 +47,7 @@ extension String { unsafeUninitializedCapacity: capacity, initializingUTF8With: { buffer in let pointer = unsafe buffer.baseAddress.unsafelyUnwrapped - var output = OutputSpan( + var output = unsafe OutputSpan( _initializing: pointer, capacity: buffer.count ) try initializer(&output) @@ -73,7 +73,7 @@ extension Data { unsafe try rawBuffer.withMemoryRebound(to: UInt8.self) { buffer in unsafe buffer.deinitialize() let pointer = unsafe buffer.baseAddress.unsafelyUnwrapped - var output = OutputSpan( + var output = unsafe OutputSpan( _initializing: pointer, capacity: capacity ) try initializer(&output) diff --git a/Sources/Future/Span/UnsafeBufferPointer+Additions.swift b/Sources/Future/Span/UnsafeBufferPointer+Additions.swift index 7b7a5e96..60b683a9 100644 --- a/Sources/Future/Span/UnsafeBufferPointer+Additions.swift +++ b/Sources/Future/Span/UnsafeBufferPointer+Additions.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Collections open source project // -// Copyright (c) 2024 Apple Inc. and the Swift project authors +// Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -14,7 +14,7 @@ extension UnsafeBufferPointer where Element: ~Copyable { /// instances refer to the same region in memory. @inlinable @inline(__always) public static func ===(_ a: Self, _ b: Self) -> Bool { - (a.baseAddress == b.baseAddress) && (a.count == b.count) + unsafe (a.baseAddress == b.baseAddress) && (a.count == b.count) } } @@ -24,7 +24,7 @@ extension UnsafeMutableBufferPointer where Element: ~Copyable { /// memory. @inlinable @inline(__always) public static func ===(_ a: Self, _ b: Self) -> Bool { - (a.baseAddress == b.baseAddress) && (a.count == b.count) + unsafe (a.baseAddress == b.baseAddress) && (a.count == b.count) } } @@ -33,7 +33,7 @@ extension UnsafeRawBufferPointer { /// instances refer to the same region in memory. @inlinable @inline(__always) public static func ===(_ a: Self, _ b: Self) -> Bool { - (a.baseAddress == b.baseAddress) && (a.count == b.count) + unsafe (a.baseAddress == b.baseAddress) && (a.count == b.count) } } @@ -43,6 +43,6 @@ extension UnsafeMutableRawBufferPointer { /// memory. @inlinable @inline(__always) public static func ===(_ a: Self, _ b: Self) -> Bool { - (a.baseAddress == b.baseAddress) && (a.count == b.count) + unsafe (a.baseAddress == b.baseAddress) && (a.count == b.count) } }