diff --git a/Package.swift b/Package.swift index cb425802..2a719c76 100644 --- a/Package.swift +++ b/Package.swift @@ -87,9 +87,6 @@ var defines: [SwiftSetting] = [ // Enables longer, more exhaustive tests. // .define("COLLECTIONS_LONG_TESTS"), - - // Enable the use of `Builtin.Borrow` in `struct Ref`. -// .define("COLLECTIONS_BORROW_BUILTIN") ] let availabilityMacros: KeyValuePairs = [ diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift b/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift index 6b7f1b7c..d1a607c6 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Consumption.swift @@ -137,8 +137,8 @@ extension RigidArray where Element: ~Copyable { } #endif -#if compiler(>=6.3) && UnstableContainersPreview -@available(SwiftStdlib 5.0, *) +#if compiler(>=6.4) && UnstableContainersPreview +@available(SwiftStdlib 6.4, *) extension RigidArray where Element: ~Copyable { @_alwaysEmitIntoClient @inline(__always) @@ -148,13 +148,16 @@ extension RigidArray where Element: ~Copyable { } } -@available(SwiftStdlib 5.0, *) +@available(SwiftStdlib 6.4, *) extension RigidArray where Element: ~Copyable { + @available(SwiftStdlib 6.4, *) @frozen public struct SubrangeConsumer: ~Copyable, ~Escapable { + // FIXME: We have to use our own MutableRef because the standard one + // provides no access to the underlying pointer. See deinit why we need it. @usableFromInline - internal var _base: MutableRef - + internal var _base: _MutableRef + @usableFromInline internal var _offsetRange: Range @@ -167,7 +170,7 @@ extension RigidArray where Element: ~Copyable { internal init(_base: inout RigidArray, offsetRange: Range) { self._remainder = _base._storage._extracting(unchecked: offsetRange) - self._base = MutableRef(&_base) + self._base = _MutableRef(&_base) self._offsetRange = offsetRange } @@ -178,7 +181,7 @@ extension RigidArray where Element: ~Copyable { // FIXME: This needs to be written as // self._base.value.closeGap(offsets: self._offsetRange) // but unfortunately we cannot mutate self in deinit yet. - // Inout's dereferencing operation is necessarily declared mutating + // MutableRef's dereferencing operation is necessarily declared mutating // to avoid exclusivity violations. self._base._pointer.pointee ._closeGap(at: _offsetRange.lowerBound, count: _offsetRange.count) @@ -188,14 +191,11 @@ extension RigidArray where Element: ~Copyable { } -#if compiler(>=6.4) && UnstableContainersPreview -@available(SwiftStdlib 5.0, *) +@available(SwiftStdlib 6.4, *) extension RigidArray.SubrangeConsumer: Drain where Element: ~Copyable { } -#endif - -@available(SwiftStdlib 5.0, *) +@available(SwiftStdlib 6.4, *) extension RigidArray.SubrangeConsumer where Element: ~Copyable { @inlinable @_lifetime(&self) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index c883ce72..3ae8cc59 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -40,11 +40,11 @@ extension RigidArray: BidirectionalContainer where Element: ~Copyable {} @available(SwiftStdlib 5.0, *) extension RigidArray: RandomAccessContainer where Element: ~Copyable {} -#if compiler(>=6.3) -@available(SwiftStdlib 5.0, *) +#if compiler(>=6.4) +@available(SwiftStdlib 6.4, *) extension RigidArray: MutableContainer where Element: ~Copyable {} -@available(SwiftStdlib 5.0, *) +@available(SwiftStdlib 6.4, *) extension RigidArray: RangeReplaceableContainer where Element: ~Copyable {} #endif #endif diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Consumption.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Consumption.swift index 5c1dbf6a..01364e04 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Consumption.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Consumption.swift @@ -116,8 +116,8 @@ extension UniqueArray where Element: ~Copyable { } #endif -#if compiler(>=6.3) && UnstableContainersPreview -@available(SwiftStdlib 5.0, *) +#if compiler(>=6.4) && UnstableContainersPreview +@available(SwiftStdlib 6.4, *) extension UniqueArray where Element: ~Copyable { public typealias SubrangeConsumer = RigidArray.SubrangeConsumer diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift index 8b8fd3e9..c314324e 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift @@ -47,11 +47,11 @@ extension UniqueArray: RandomAccessContainer where Element: ~Copyable {} @available(SwiftStdlib 5.0, *) extension UniqueArray: MutableContainer where Element: ~Copyable {} -#if compiler(>=6.3) -@available(SwiftStdlib 5.0, *) +#if compiler(>=6.4) +@available(SwiftStdlib 6.4, *) extension UniqueArray: RangeReplaceableContainer where Element: ~Copyable {} -@available(SwiftStdlib 5.0, *) +@available(SwiftStdlib 6.4, *) extension UniqueArray: DynamicContainer where Element: ~Copyable {} #endif #endif diff --git a/Sources/ContainersPreview/CMakeLists.txt b/Sources/ContainersPreview/CMakeLists.txt index 84941192..f703ddb1 100644 --- a/Sources/ContainersPreview/CMakeLists.txt +++ b/Sources/ContainersPreview/CMakeLists.txt @@ -25,6 +25,7 @@ else() endif() target_sources(${module_name} PRIVATE + "Extensions/Optional+Extras.swift" "Extensions/OutputSpan+Extras.swift" "Extensions/TemporaryAllocation.swift" "Protocols/Container/BidirectionalContainer.swift" @@ -61,7 +62,6 @@ target_sources(${module_name} PRIVATE "Protocols/Producer+Reduce.swift" "Types/InputSpan.swift" "Types/MutableRef.swift" - "Types/Ref.swift" "Types/Shared.swift" "Types/SpanIterator.swift" "Types/UniqueBox.swift" diff --git a/Sources/ContainersPreview/Extensions/Optional+Extras.swift b/Sources/ContainersPreview/Extensions/Optional+Extras.swift new file mode 100644 index 00000000..df9ca615 --- /dev/null +++ b/Sources/ContainersPreview/Extensions/Optional+Extras.swift @@ -0,0 +1,59 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2025 - 2026 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 +// +// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception +// +//===----------------------------------------------------------------------===// + +#if compiler(>=6.4) && UnstableContainersPreview +import Builtin + +extension Optional where Wrapped: ~Copyable /* FIXME: ~Escapable */ { + @available(SwiftStdlib 6.4, *) + @_lifetime(borrow self) + @_addressableSelf + public func borrow() -> Ref? { + if self == nil { + return nil + } + + // FIXME: This assumes that `Optional.some` is guaranteed to have + // `T` at the beginning of its layout. Is that true? + let pointer = unsafe UnsafePointer( + Builtin.unprotectedAddressOfBorrow(self) + ) + + return unsafe Ref(unsafeAddress: pointer, borrowing: self) + } +} + +extension Optional where Wrapped: ~Copyable /* FIXME: ~Escapable */ { + @available(SwiftStdlib 6.4, *) + @_lifetime(&self) + @_alwaysEmitIntoClient + package mutating func mutate() -> MutableRef? { + if self == nil { + return nil + } + let pointer = unsafe UnsafeMutablePointer( + Builtin.unprotectedAddressOf(&self)) + return unsafe MutableRef(unsafeAddress: pointer, mutating: &self) + } + + @available(SwiftStdlib 6.4, *) + @_lifetime(&self) + @_alwaysEmitIntoClient + @_transparent + package mutating func insert(_ value: consuming Wrapped) -> MutableRef { + self = .some(value) + return mutate()._consumingUnsafelyUnwrap() + } +} + +#endif diff --git a/Sources/ContainersPreview/Types/MutableRef.swift b/Sources/ContainersPreview/Types/MutableRef.swift index 348b66a1..0aeb9aac 100644 --- a/Sources/ContainersPreview/Types/MutableRef.swift +++ b/Sources/ContainersPreview/Types/MutableRef.swift @@ -14,18 +14,13 @@ #if compiler(>=6.3) && UnstableContainersPreview import Builtin -@available(*, deprecated, renamed: "MutableRef") -public typealias Inout = MutableRef - -/// A safe mutable reference allowing in-place mutation to an exclusive value. -/// -/// In order to get an instance of a `MutableRef`, one must have exclusive access -/// to the instance of `Target`. This is achieved through the 'inout' operator, '&'. +// FIXME: Remove when MutableRef becomes usable @frozen @safe -public struct MutableRef: ~Copyable, ~Escapable { +@usableFromInline +package struct _MutableRef: ~Copyable, ~Escapable { @available(*, deprecated, renamed: "Value") - public typealias Target = Value + package typealias Target = Value @usableFromInline package let _pointer: UnsafeMutablePointer @@ -36,7 +31,7 @@ public struct MutableRef: ~Copyable, ~ /// - Parameter value: The desired instance to get a mutable reference to. @_alwaysEmitIntoClient @_transparent - public init(_ value: inout Value) { + package init(_ value: inout Value) { unsafe _pointer = UnsafeMutablePointer(Builtin.unprotectedAddressOf(&value)) } @@ -52,7 +47,7 @@ public struct MutableRef: ~Copyable, ~ @_alwaysEmitIntoClient @_transparent @_lifetime(&owner) - public init( + package init( unsafeAddress: UnsafeMutablePointer, mutating owner: inout Owner ) { @@ -69,17 +64,17 @@ public struct MutableRef: ~Copyable, ~ @unsafe @_alwaysEmitIntoClient @_transparent - public init( + package init( unsafeImmortalAddress: UnsafeMutablePointer ) { unsafe _pointer = unsafeImmortalAddress } } -extension MutableRef where Value: ~Copyable { +extension _MutableRef where Value: ~Copyable { @_alwaysEmitIntoClient @available(*, deprecated, renamed: "value") - public subscript() -> Value { + package subscript() -> Value { @_transparent unsafeAddress { unsafe UnsafePointer(_pointer) @@ -94,7 +89,7 @@ extension MutableRef where Value: ~Copyable { /// Dereferences the mutable reference allowing for in-place reads and writes /// to the underlying instance. @_alwaysEmitIntoClient - public var value: Value { + package var value: Value { @_transparent unsafeAddress { unsafe .init(_pointer) @@ -104,28 +99,5 @@ extension MutableRef where Value: ~Copyable { unsafe _pointer } } - -} - -extension Optional where Wrapped: ~Copyable /* FIXME: ~Escapable */ { - @_lifetime(&self) - @_alwaysEmitIntoClient - public mutating func mutate() -> MutableRef? { - if self == nil { - return nil - } - let pointer = unsafe UnsafeMutablePointer( - Builtin.unprotectedAddressOf(&self)) - return unsafe MutableRef(unsafeAddress: pointer, mutating: &self) - } - - @_lifetime(&self) - @_alwaysEmitIntoClient - @_transparent - public mutating func insert(_ value: consuming Wrapped) -> MutableRef { - self = .some(value) - return mutate()._consumingUnsafelyUnwrap() - } - } #endif diff --git a/Sources/ContainersPreview/Types/Ref.swift b/Sources/ContainersPreview/Types/Ref.swift deleted file mode 100644 index c7068b1b..00000000 --- a/Sources/ContainersPreview/Types/Ref.swift +++ /dev/null @@ -1,202 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift Collections open source project -// -// Copyright (c) 2025 - 2026 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 -// -// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception -// -//===----------------------------------------------------------------------===// - -#if false - -#if compiler(>=6.3) && UnstableContainersPreview -import Builtin - -#if compiler(>=6.4) && COLLECTIONS_BORROW_BUILTIN -/// A safe reference allowing in-place reads to a shared value. -@available(SwiftStdlib 5.0, *) -@frozen -public struct Ref: Copyable, ~Escapable { - @usableFromInline - let _builtin: Builtin.Borrow - - /// Initializes an instance of `Ref` with the given borrowed value. This - /// creates a constant reference to that value preventing writes on the - /// original value while this reference is still active. - @_alwaysEmitIntoClient - @_lifetime(borrow value) - @_transparent - public init(_ value: borrowing Value) { - _builtin = Builtin.makeBorrow(value) - } - - /// Unsafely initializes an instance of `Ref` using the given - /// 'unsafeAddress' as the reference based on the borrowed lifetime of the - /// given 'owner' argument. - /// - /// - Parameter unsafeAddress: The address to use to reference an instance of - /// type `Value`. - /// - Parameter owner: The owning instance that this `Ref` instance's - /// lifetime is based on. - @unsafe - @_alwaysEmitIntoClient - @_lifetime(borrow owner) - @_transparent - public init( - unsafeAddress pointer: UnsafePointer, - borrowing owner: borrowing Owner - ) { - _builtin = unsafe Builtin.makeBorrow(pointer.pointee) - } -} - -@available(SwiftStdlib 5.0, *) -extension Ref where Value: ~Copyable { - /// Dereferences the constant reference allowing for in-place reads to the - /// underlying value. - @_alwaysEmitIntoClient - @_transparent - public var value: Value { - borrow { - Builtin.dereferenceBorrow(_builtin) - } - } -} - -#else - -/// A safe reference allowing in-place reads to a shared value. -@available(SwiftStdlib 5.0, *) -@frozen -public struct Ref: Copyable, ~Escapable { - @usableFromInline - package let _pointer: UnsafePointer - - @_lifetime(borrow value) - @_alwaysEmitIntoClient - @_transparent - internal init(_borrowing value: borrowing @_addressable Value) { - unsafe _pointer = UnsafePointer(Builtin.unprotectedAddressOfBorrow(value)) - } - - /// Initializes an instance of `Ref` with the given borrowed value. This - /// creates a constant reference to that value preventing writes on the - /// original value while this reference is still active. - @_lifetime(borrow value) - @_alwaysEmitIntoClient - @_transparent - public init(_ value: borrowing @_addressable Value) { - unsafe _pointer = UnsafePointer(Builtin.unprotectedAddressOfBorrow(value)) - } - - /// Unsafely initializes an instance of `Ref` using the given - /// 'unsafeAddress' as the reference based on the borrowed lifetime of the - /// given 'owner' argument. - /// - /// - Parameter unsafeAddress: The address to use to reference an instance of - /// type `Value`. - /// - Parameter owner: The owning instance that this `Ref` instance's - /// lifetime is based on. - @unsafe - @_alwaysEmitIntoClient - @_lifetime(borrow owner) - @_transparent - public init( - unsafeAddress pointer: UnsafePointer, - borrowing owner: borrowing Owner - ) { - _pointer = pointer - } -} - -@available(SwiftStdlib 5.0, *) -extension Ref where Value: ~Copyable { - /// Dereferences the constant reference allowing for in-place reads to the - /// underlying value. - @_alwaysEmitIntoClient - public var value: Value { - @_transparent - unsafeAddress { - _pointer - } - } -} - -#endif - -@available(SwiftStdlib 5.0, *) -extension Ref where Value: ~Copyable { - @_lifetime(copy owner) - @_alwaysEmitIntoClient - @_transparent - public init( - unsafeAddress pointer: UnsafePointer, - copying owner: borrowing Owner - ) { -#if compiler(>=6.4) && COLLECTIONS_BORROW_BUILTIN - _builtin = unsafe Builtin.makeBorrow(pointer.pointee) -#else - _pointer = pointer -#endif - } -} - -@available(SwiftStdlib 5.0, *) -@available(*, deprecated, renamed: "Ref") -public typealias Borrow = Ref - -@available(SwiftStdlib 5.0, *) -extension Ref where Value: ~Copyable { - @available(*, deprecated, renamed: "Value") - public typealias Target = Value -} - -@available(SwiftStdlib 5.0, *) -extension Ref where Value: ~Copyable { -#if compiler(>=6.4) && COLLECTIONS_BORROW_BUILTIN - @_alwaysEmitIntoClient - @available(*, deprecated, renamed: "value") - public subscript() -> Value { - @_transparent - borrow { - value - } - } -#else - @_alwaysEmitIntoClient - @available(*, deprecated, renamed: "value") - public subscript() -> Value { - @_transparent - unsafeAddress { - _pointer - } - } -#endif -} - -extension Optional where Wrapped: ~Copyable /* FIXME: ~Escapable */ { - @available(SwiftStdlib 5.0, *) - @_lifetime(borrow self) - @_addressableSelf - public func borrow() -> Ref? { - if self == nil { - return nil - } - - // FIXME: This assumes that `Optional.some` is guaranteed to have - // `T` at the beginning of its layout. Is that true? - let pointer = unsafe UnsafePointer( - Builtin.unprotectedAddressOfBorrow(self) - ) - - return unsafe Ref(unsafeAddress: pointer, borrowing: self) - } -} - -#endif - -#endif diff --git a/Sources/ContainersPreview/Types/UniqueBox.swift b/Sources/ContainersPreview/Types/UniqueBox.swift index f7a3aac0..2e646b83 100644 --- a/Sources/ContainersPreview/Types/UniqueBox.swift +++ b/Sources/ContainersPreview/Types/UniqueBox.swift @@ -11,8 +11,6 @@ // //===----------------------------------------------------------------------===// -#if false - #if compiler(<6.2) @available(*, unavailable, renamed: "UniqueBox", message: "struct UniqueBox requires a Swift 6.2 toolchain") @@ -186,22 +184,25 @@ extension UniqueBox where Value: Copyable { } extension UniqueBox where Value: ~Copyable { -#if compiler(>=6.3) && UnstableContainersPreview +#if compiler(>=6.4) && UnstableContainersPreview /// Leak the heap allocation behind this box, converting it into an /// immortal mutating reference. + @available(SwiftStdlib 6.4, *) @_alwaysEmitIntoClient @_transparent @_lifetime(immortal) + @_unsafeNonescapableResult // FIXME: This should not be necessary public consuming func leak() -> MutableRef { - let result = unsafe MutableRef(unsafeImmortalAddress: _pointer) + let p = _pointer discard self - return result + // FIXME: _unsafeImmortalize does not work here, and MutableRef has no immortal initializer + return unsafe MutableRef(&p.pointee) } #endif -#if compiler(>=6.3) && UnstableContainersPreview +#if compiler(>=6.4) && UnstableContainersPreview /// Return a borrowing reference to the contents of this box. - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) @_alwaysEmitIntoClient @_transparent @_lifetime(borrow self) @@ -210,8 +211,9 @@ extension UniqueBox where Value: ~Copyable { } #endif -#if compiler(>=6.3) && UnstableContainersPreview +#if compiler(>=6.4) && UnstableContainersPreview /// Return a mutating reference to the contents of this box. + @available(SwiftStdlib 6.4, *) @_alwaysEmitIntoClient @_transparent @_lifetime(&self) @@ -222,5 +224,3 @@ extension UniqueBox where Value: ~Copyable { } #endif - -#endif diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Consumption.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Consumption.swift index 79fb457c..d7185855 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Consumption.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Consumption.swift @@ -164,9 +164,10 @@ extension RigidDeque where Element: ~Copyable { #endif } -#if compiler(>=6.3) && UnstableContainersPreview +#if compiler(>=6.4) && UnstableContainersPreview @available(SwiftStdlib 5.0, *) extension RigidDeque where Element: ~Copyable { + @available(SwiftStdlib 6.4, *) @_alwaysEmitIntoClient @inline(__always) @_lifetime(&self) @@ -177,11 +178,14 @@ extension RigidDeque where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidDeque where Element: ~Copyable { + @available(SwiftStdlib 6.4, *) @frozen public struct SubrangeConsumer: ~Copyable, ~Escapable { + // FIXME: We have to use our own MutableRef because the standard one + // provides no access to the underlying pointer. See deinit why we need it. @usableFromInline - internal var _base: MutableRef - + internal var _base: _MutableRef + @usableFromInline internal var _offsetRange: Range @@ -199,7 +203,7 @@ extension RigidDeque where Element: ~Copyable { let segments = _base._handle.mutableSegments(forOffsets: offsetRange) self._buffer1 = segments.first self._buffer2 = segments.second ?? .init(start: nil, count: 0) - self._base = MutableRef(&_base) + self._base = _MutableRef(&_base) self._offsetRange = offsetRange } @@ -217,13 +221,11 @@ extension RigidDeque where Element: ~Copyable { } } -#if compiler(>=6.4) -@available(SwiftStdlib 5.0, *) +@available(SwiftStdlib 6.4, *) extension RigidDeque.SubrangeConsumer: Drain where Element: ~Copyable { } -#endif -@available(SwiftStdlib 5.0, *) +@available(SwiftStdlib 6.4, *) extension RigidDeque.SubrangeConsumer where Element: ~Copyable { @inlinable @_lifetime(&self) diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift index f6222044..2e7aa125 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift @@ -76,11 +76,11 @@ extension RigidDeque: BidirectionalContainer where Element: ~Copyable {} @available(SwiftStdlib 5.0, *) extension RigidDeque: RandomAccessContainer where Element: ~Copyable {} -#if compiler(>=6.3) -@available(SwiftStdlib 5.0, *) +#if compiler(>=6.4) +@available(SwiftStdlib 6.4, *) extension RigidDeque: MutableContainer where Element: ~Copyable {} -@available(SwiftStdlib 5.0, *) +@available(SwiftStdlib 6.4, *) extension RigidDeque: RangeReplaceableContainer where Element: ~Copyable {} #endif #endif diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Consumption.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Consumption.swift index 5e487c71..cc3f0d39 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Consumption.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Consumption.swift @@ -147,8 +147,8 @@ extension UniqueDeque where Element: ~Copyable { #endif } -#if compiler(>=6.3) && UnstableContainersPreview -@available(SwiftStdlib 5.0, *) +#if compiler(>=6.4) && UnstableContainersPreview +@available(SwiftStdlib 6.4, *) extension UniqueDeque where Element: ~Copyable { public typealias SubrangeConsumer = RigidDeque.SubrangeConsumer diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift index 4367227f..3124ebef 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift @@ -45,11 +45,11 @@ extension UniqueDeque: RandomAccessContainer where Element: ~Copyable {} @available(SwiftStdlib 5.0, *) extension UniqueDeque: MutableContainer where Element: ~Copyable {} -#if compiler(>=6.3) -@available(SwiftStdlib 5.0, *) +#if compiler(>=6.4) +@available(SwiftStdlib 6.4, *) extension UniqueDeque: RangeReplaceableContainer where Element: ~Copyable {} -@available(SwiftStdlib 5.0, *) +@available(SwiftStdlib 6.4, *) extension UniqueDeque: DynamicContainer where Element: ~Copyable {} #endif #endif diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index 1fb89155..dbfe64a9 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -1469,6 +1469,7 @@ class RigidArrayTests: CollectionTestCase { #endif #if compiler(>=6.4) && UnstableContainersPreview + @available(SwiftStdlib 6.4, *) func test_borrowing_map() { let c = 100 let items = RigidArray(capacity: c, copying: 0 ..< c) @@ -1482,6 +1483,7 @@ class RigidArrayTests: CollectionTestCase { expectUniqueArrayContents(transformed, equalTo: expected) } + @available(SwiftStdlib 6.4, *) func test_borrowing_filter() { let c = 100 let items = RigidArray(capacity: c, copying: 0 ..< c) @@ -1497,6 +1499,7 @@ class RigidArrayTests: CollectionTestCase { #endif #if compiler(>=6.4) && UnstableContainersPreview + @available(SwiftStdlib 6.4, *) func test_consume_subrange_drainNext() { withLifetimeTracking { tracker in var a = RigidArray>(capacity: 4) diff --git a/Tests/ContainersTests/MutableRefTests.swift b/Tests/ContainersTests/MutableRefTests.swift index f7d94404..435c23c1 100644 --- a/Tests/ContainersTests/MutableRefTests.swift +++ b/Tests/ContainersTests/MutableRefTests.swift @@ -28,7 +28,7 @@ final class MutableRefTests: CollectionTestCase { func test_basic() { var x = 0 - var y = MutableRef(&x) + var y = _MutableRef(&x) var v = y.value XCTAssertEqual(v, 0) @@ -44,19 +44,19 @@ final class MutableRefTests: CollectionTestCase { func test_init_and_value_read_int() { var x = 42 - let ref = MutableRef(&x) + let ref = _MutableRef(&x) expectEqual(ref.value, 42) } func test_init_and_value_read_string() { var s = "hello" - let ref = MutableRef(&s) + let ref = _MutableRef(&s) expectEqual(ref.value, "hello") } func test_init_and_value_read_noncopyable() { var payload = NoncopyablePayload(99) - let ref = MutableRef(&payload) + let ref = _MutableRef(&payload) expectEqual(ref.value.value, 99) } @@ -64,7 +64,7 @@ final class MutableRefTests: CollectionTestCase { func test_value_write_int() { var x = 0 - var ref = MutableRef(&x) + var ref = _MutableRef(&x) ref.value = 123 expectEqual(ref.value, 123) expectEqual(x, 123) @@ -72,7 +72,7 @@ final class MutableRefTests: CollectionTestCase { func test_value_write_string() { var s = "before" - var ref = MutableRef(&s) + var ref = _MutableRef(&s) ref.value = "after" expectEqual(ref.value, "after") expectEqual(s, "after") @@ -80,7 +80,7 @@ final class MutableRefTests: CollectionTestCase { func test_value_write_noncopyable() { var payload = NoncopyablePayload(1) - var ref = MutableRef(&payload) + var ref = _MutableRef(&payload) ref.value.value = 42 expectEqual(ref.value.value, 42) expectEqual(payload.value, 42) @@ -90,7 +90,7 @@ final class MutableRefTests: CollectionTestCase { func test_value_inplace_mutation() { var x = 10 - var ref = MutableRef(&x) + var ref = _MutableRef(&x) ref.value += 32 expectEqual(ref.value, 42) expectEqual(x, 42) @@ -98,7 +98,7 @@ final class MutableRefTests: CollectionTestCase { func test_value_inplace_mutation_array() { var array = [1, 2, 3] - var ref = MutableRef(&array) + var ref = _MutableRef(&array) ref.value.append(4) expectEqual(ref.value, [1, 2, 3, 4]) expectEqual(array, [1, 2, 3, 4]) @@ -109,7 +109,7 @@ final class MutableRefTests: CollectionTestCase { func test_init_unsafeAddress_mutating() { var x = 789 let pointer = unsafe withUnsafeMutablePointer(to: &x) { $0 } - var ref = unsafe MutableRef(unsafeAddress: pointer, mutating: &x) + var ref = unsafe _MutableRef(unsafeAddress: pointer, mutating: &x) expectEqual(ref.value, 789) ref.value = 111 expectEqual(ref.value, 111) @@ -120,7 +120,7 @@ final class MutableRefTests: CollectionTestCase { func test_init_unsafeImmortalAddress() { var x = 555 let pointer = unsafe withUnsafeMutablePointer(to: &x) { $0 } - var ref = unsafe MutableRef(unsafeImmortalAddress: pointer) + var ref = unsafe _MutableRef(unsafeImmortalAddress: pointer) expectEqual(ref.value, 555) ref.value = 666 expectEqual(ref.value, 666) @@ -131,16 +131,16 @@ final class MutableRefTests: CollectionTestCase { func test_reference_type_identity() { let tracker = LifetimeTracker() var obj: LifetimeTracked? = tracker.instance(for: 7) - let ref = MutableRef(&obj) + let ref = _MutableRef(&obj) expectNotNil(ref.value) expectEqual(ref.value!.payload, 7) } - // MARK: Multiple writes through MutableRef + // MARK: Multiple writes through _MutableRef func test_multiple_writes() { var x = 0 - var ref = MutableRef(&x) + var ref = _MutableRef(&x) ref.value = 1 expectEqual(ref.value, 1) ref.value = 2 @@ -152,36 +152,40 @@ final class MutableRefTests: CollectionTestCase { // MARK: Optional.mutate() + @available(SwiftStdlib 6.4, *) func test_optional_mutate_some() { var x: Int? = 77 if var ref = x.mutate() { expectEqual(ref.value, 77) ref.value = 88 } else { - expectTrue(false, "Expected non-nil MutableRef from .some optional") + expectTrue(false, "Expected non-nil _MutableRef from .some optional") } expectEqual(x, 88) } + @available(SwiftStdlib 6.4, *) func test_optional_mutate_none() { var x: Int? = nil let ref = x.mutate() expectNil(ref) } + @available(SwiftStdlib 6.4, *) func test_optional_mutate_noncopyable() { var x: NoncopyablePayload? = NoncopyablePayload(33) if var ref = x.mutate() { expectEqual(ref.value.value, 33) ref.value.value = 44 } else { - expectTrue(false, "Expected non-nil MutableRef from .some optional") + expectTrue(false, "Expected non-nil _MutableRef from .some optional") } expectEqual(x!.value, 44) } // MARK: Optional.insert(_:) + @available(SwiftStdlib 6.4, *) func test_optional_insert_into_nil() { var x: Int? = nil do { @@ -192,6 +196,7 @@ final class MutableRefTests: CollectionTestCase { expectEqual(x, 99) } + @available(SwiftStdlib 6.4, *) func test_optional_insert_into_some() { var x: Int? = 10 do { @@ -202,6 +207,7 @@ final class MutableRefTests: CollectionTestCase { expectEqual(x, 30) } + @available(SwiftStdlib 6.4, *) func test_optional_insert_noncopyable() { var x: NoncopyablePayload? = nil do { @@ -211,40 +217,5 @@ final class MutableRefTests: CollectionTestCase { } expectEqual(x!.value, 66) } - - // MARK: Deprecated API - - @available(*, deprecated) - func test_subscript_deprecated_read() { - var x = 11 - let ref = MutableRef(&x) - expectEqual(ref[], 11) - } - - @available(*, deprecated) - func test_subscript_deprecated_write() { - var x = 0 - var ref = MutableRef(&x) - ref[] = 22 - expectEqual(ref[], 22) - expectEqual(x, 22) - } - - @available(*, deprecated) - func test_Inout_typealias() { - var x = 88 - var ref: Inout = Inout(&x) - ref.value = 99 - expectEqual(ref.value, 99) - expectEqual(x, 99) - } - - @available(*, deprecated) - func test_Target_typealias() { - var x = 77 - let ref = MutableRef(&x) - let v: MutableRef.Target = ref.value - expectEqual(v, 77) - } } #endif diff --git a/Tests/ContainersTests/RefTests.swift b/Tests/ContainersTests/RefTests.swift index eef90ac8..b83376d6 100644 --- a/Tests/ContainersTests/RefTests.swift +++ b/Tests/ContainersTests/RefTests.swift @@ -11,9 +11,7 @@ // //===----------------------------------------------------------------------===// -#if false - -#if compiler(>=6.3) && UnstableContainersPreview +#if compiler(>=6.4) && UnstableContainersPreview import XCTest #if COLLECTIONS_SINGLE_MODULE import Collections @@ -22,6 +20,7 @@ import _CollectionsTestSupport import ContainersPreview #endif +@available(SwiftStdlib 6.4, *) final class RefTests: CollectionTestCase { struct NoncopyablePayload: ~Copyable { var value: Int @@ -30,35 +29,35 @@ final class RefTests: CollectionTestCase { // MARK: init(_:) and value - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_init_and_value_int() { let x = 42 let ref = Ref(x) expectEqual(ref.value, 42) } - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_init_and_value_string() { let s = "hello" let ref = Ref(s) expectEqual(ref.value, "hello") } - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_init_and_value_noncopyable() { let payload = NoncopyablePayload(99) let ref = Ref(payload) expectEqual(ref.value.value, 99) } - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_value_reflects_original() { let array = [1, 2, 3] let ref = Ref(array) expectEqual(ref.value, [1, 2, 3]) } - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_value_reference_type() { let tracker = LifetimeTracker() let obj = tracker.instance(for: 7) @@ -69,7 +68,7 @@ final class RefTests: CollectionTestCase { // MARK: init(unsafeAddress:borrowing:) - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_init_unsafeAddress_borrowing() { let x = 123 unsafe withUnsafePointer(to: x) { pointer in @@ -78,20 +77,9 @@ final class RefTests: CollectionTestCase { } } - // MARK: init(unsafeAddress:copying:) - - @available(SwiftStdlib 5.0, *) - func test_init_unsafeAddress_copying() { - let x = 456 - unsafe withUnsafePointer(to: x) { pointer in - let ref = Ref(unsafeAddress: pointer, copying: x) - expectEqual(ref.value, 456) - } - } - // MARK: Ref is Copyable - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_ref_is_copyable() { let x = 42 let ref1 = Ref(x) @@ -102,7 +90,7 @@ final class RefTests: CollectionTestCase { // MARK: Optional.borrow() - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_optional_borrow_some() { let x: Int? = 77 if let ref = x.borrow() { @@ -112,14 +100,14 @@ final class RefTests: CollectionTestCase { } } - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_optional_borrow_none() { let x: Int? = nil let ref = x.borrow() expectNil(ref) } - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_optional_borrow_string() { let x: String? = "world" if let ref = x.borrow() { @@ -129,7 +117,7 @@ final class RefTests: CollectionTestCase { } } - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_optional_borrow_noncopyable() { let x: NoncopyablePayload? = NoncopyablePayload(33) if let ref = x.borrow() { @@ -139,7 +127,7 @@ final class RefTests: CollectionTestCase { } } - @available(SwiftStdlib 5.0, *) + @available(SwiftStdlib 6.4, *) func test_optional_borrow_reference_type() { let tracker = LifetimeTracker() let obj = tracker.instance(for: 5) @@ -151,33 +139,5 @@ final class RefTests: CollectionTestCase { expectFailure("Expected non-nil Ref from .some optional") } } - - // MARK: Deprecated API - - @available(*, deprecated) - @available(SwiftStdlib 5.0, *) - func test_subscript_deprecated() { - let x = 11 - let ref = Ref(x) - expectEqual(ref[], 11) - } - - @available(*, deprecated) - func test_Borrow_typealias() { - let x = 88 - let ref: Borrow = Borrow(x) - expectEqual(ref.value, 88) - } - - @available(*, deprecated) - @available(SwiftStdlib 5.0, *) - func test_Target_typealias() { - let x = 99 - let ref = Ref(x) - let v: Ref.Target = ref.value - expectEqual(v, 99) - } } #endif - -#endif diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index 3b1bd797..de63f9df 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -12,8 +12,6 @@ 7D2A77392F27F1E4005780C7 /* UniqueBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77332F27F1E4005780C7 /* UniqueBox.swift */; }; 7D2A773A2F27F1E4005780C7 /* Drain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A772F2F27F1E4005780C7 /* Drain.swift */; }; 7D2A773C2F27F1E4005780C7 /* TemporaryAllocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77282F27F1E4005780C7 /* TemporaryAllocation.swift */; }; - 7D2A773F2F27F1E4005780C7 /* Ref.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77322F27F1E4005780C7 /* Ref.swift */; }; - 7D2A77412F27F1E4005780C7 /* MutableRef.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77342F27F1E4005780C7 /* MutableRef.swift */; }; 7D2A77422F27F1E4005780C7 /* OutputSpan+Extras.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77272F27F1E4005780C7 /* OutputSpan+Extras.swift */; }; 7D2A77432F27F1E4005780C7 /* Shared.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77362F27F1E4005780C7 /* Shared.swift */; }; 7D2A77442F27F1E4005780C7 /* InputSpan.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77352F27F1E4005780C7 /* InputSpan.swift */; }; @@ -210,6 +208,8 @@ 7DC037C12FE3492D00CF0CB8 /* IterableIteratorProtocol+Map.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC037B42FE3492D00CF0CB8 /* IterableIteratorProtocol+Map.swift */; }; 7DC037C32FE34B2800CF0CB8 /* Heap+Testing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC037C22FE34B2800CF0CB8 /* Heap+Testing.swift */; }; 7DC037C52FE3717A00CF0CB8 /* IterableIteratorProtocol+MapError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC037C42FE3717200CF0CB8 /* IterableIteratorProtocol+MapError.swift */; }; + 7DC037C72FE378B300CF0CB8 /* Optional+Extras.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC037C62FE378B300CF0CB8 /* Optional+Extras.swift */; }; + 7DC037C92FE3792300CF0CB8 /* MutableRef.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC037C82FE3792300CF0CB8 /* MutableRef.swift */; }; 7DC85F8D2F3FDC91004A4E3D /* String+Padding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC85F8C2F3FDC82004A4E3D /* String+Padding.swift */; }; 7DC85F8F2F43D065004A4E3D /* RigidDictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC85F8E2F43D05F004A4E3D /* RigidDictionaryTests.swift */; }; 7DE448452F2D3705005853A7 /* BinaryInteger extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE448442F2D36F9005853A7 /* BinaryInteger extensions.swift */; }; @@ -621,9 +621,7 @@ 7D2A77282F27F1E4005780C7 /* TemporaryAllocation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TemporaryAllocation.swift; sourceTree = ""; }; 7D2A772F2F27F1E4005780C7 /* Drain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Drain.swift; sourceTree = ""; }; 7D2A77302F27F1E4005780C7 /* Producer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Producer.swift; sourceTree = ""; }; - 7D2A77322F27F1E4005780C7 /* Ref.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Ref.swift; sourceTree = ""; }; 7D2A77332F27F1E4005780C7 /* UniqueBox.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueBox.swift; sourceTree = ""; }; - 7D2A77342F27F1E4005780C7 /* MutableRef.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MutableRef.swift; sourceTree = ""; }; 7D2A77352F27F1E4005780C7 /* InputSpan.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputSpan.swift; sourceTree = ""; }; 7D2A77362F27F1E4005780C7 /* Shared.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shared.swift; sourceTree = ""; }; 7D2A77482F27F271005780C7 /* CustomProducer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomProducer.swift; sourceTree = ""; }; @@ -826,6 +824,8 @@ 7DC037B62FE3492D00CF0CB8 /* IterableIteratorProtocol+SpanwiseZip.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IterableIteratorProtocol+SpanwiseZip.swift"; sourceTree = ""; }; 7DC037C22FE34B2800CF0CB8 /* Heap+Testing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Heap+Testing.swift"; sourceTree = ""; }; 7DC037C42FE3717200CF0CB8 /* IterableIteratorProtocol+MapError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IterableIteratorProtocol+MapError.swift"; sourceTree = ""; }; + 7DC037C62FE378B300CF0CB8 /* Optional+Extras.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Optional+Extras.swift"; sourceTree = ""; }; + 7DC037C82FE3792300CF0CB8 /* MutableRef.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MutableRef.swift; sourceTree = ""; }; 7DC85F8C2F3FDC82004A4E3D /* String+Padding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Padding.swift"; sourceTree = ""; }; 7DC85F8E2F43D05F004A4E3D /* RigidDictionaryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RigidDictionaryTests.swift; sourceTree = ""; }; 7DE448442F2D36F9005853A7 /* BinaryInteger extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BinaryInteger extensions.swift"; sourceTree = ""; }; @@ -1307,6 +1307,7 @@ 7D2A77292F27F1E4005780C7 /* Extensions */ = { isa = PBXGroup; children = ( + 7DC037C62FE378B300CF0CB8 /* Optional+Extras.swift */, 7D2A77272F27F1E4005780C7 /* OutputSpan+Extras.swift */, 7D2A77282F27F1E4005780C7 /* TemporaryAllocation.swift */, ); @@ -1345,8 +1346,7 @@ isa = PBXGroup; children = ( 7D2A77332F27F1E4005780C7 /* UniqueBox.swift */, - 7D2A77322F27F1E4005780C7 /* Ref.swift */, - 7D2A77342F27F1E4005780C7 /* MutableRef.swift */, + 7DC037C82FE3792300CF0CB8 /* MutableRef.swift */, 7D2A77352F27F1E4005780C7 /* InputSpan.swift */, 7D2A77362F27F1E4005780C7 /* Shared.swift */, 7DACFA112F75FBF100291F2E /* SpanIterator.swift */, @@ -2692,6 +2692,7 @@ 7D2A77972F27F2BF005780C7 /* Deque+Testing.swift in Sources */, 7D2A77982F27F2BF005780C7 /* RigidDeque+Replacements.swift in Sources */, 7D7246872F3D9DDB00112FD0 /* _HTable+Iteration.swift in Sources */, + 7DC037C72FE378B300CF0CB8 /* Optional+Extras.swift in Sources */, 7D7246882F3D9DDB00112FD0 /* RigidDictionary.swift in Sources */, 7D7246892F3D9DDB00112FD0 /* _HTable+Find.swift in Sources */, 7D72468A2F3D9DDB00112FD0 /* _HTable.swift in Sources */, @@ -2845,6 +2846,7 @@ 7D9B859A29E4F74400B291CD /* BitArray+RandomBits.swift in Sources */, 7DE920EC29CA70F4004483EB /* BitArray+Collection.swift in Sources */, 7DE9201729CA70F3004483EB /* OrderedDictionary+Codable.swift in Sources */, + 7DC037C92FE3792300CF0CB8 /* MutableRef.swift in Sources */, 7DE9208329CA70F4004483EB /* BigString+CustomDebugStringConvertible.swift in Sources */, 7DE9215D29CA70F4004483EB /* _HashNodeHeader.swift in Sources */, 7DE9213329CA70F4004483EB /* TreeSet+Collection.swift in Sources */, @@ -2967,9 +2969,7 @@ 7D2A773A2F27F1E4005780C7 /* Drain.swift in Sources */, 7D479CC32F46D19600A8CB59 /* RigidSet+Resizing.swift in Sources */, 7D2A773C2F27F1E4005780C7 /* TemporaryAllocation.swift in Sources */, - 7D2A773F2F27F1E4005780C7 /* Ref.swift in Sources */, 7D57A2D02F353946002543BD /* RigidArray+Deprecated.swift in Sources */, - 7D2A77412F27F1E4005780C7 /* MutableRef.swift in Sources */, 7D2A77422F27F1E4005780C7 /* OutputSpan+Extras.swift in Sources */, 7D2A77432F27F1E4005780C7 /* Shared.swift in Sources */, 7D2A77442F27F1E4005780C7 /* InputSpan.swift in Sources */,