From a6fd64697ad6825f9e41e17c9043df971441b531 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 5 May 2026 18:02:58 -0700 Subject: [PATCH] =?UTF-8?q?[ContainersPreview]=20Borrow=20=E2=9F=B9=20Ref?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also update the type’s public API surface to reflect SE-0519, leaving deprecated aliases for the old names. --- README.md | 4 +- .../RigidArray/RigidArray+Experimental.swift | 4 +- .../RigidDictionary+Indices.swift | 6 +- .../RigidDictionary+Insertions.swift | 2 +- .../RigidDictionary+Lookup.swift | 14 +- .../RigidSet/RigidSet+Lookup.swift | 4 +- .../UniqueArray+Experimental.swift | 2 +- .../UniqueDictionary+Insertions.swift | 2 +- .../UniqueDictionary+Lookup.swift | 4 +- Sources/ContainersPreview/CMakeLists.txt | 2 +- .../Container/Container+Filter.swift | 6 +- .../Container/ContainerIterator.swift | 4 +- Sources/ContainersPreview/Types/Borrow.swift | 94 --------- Sources/ContainersPreview/Types/Ref.swift | 192 ++++++++++++++++++ .../ContainersPreview/Types/UniqueBox.swift | 4 +- .../RigidDeque/RigidDeque+Experimental.swift | 4 +- .../UniqueDeque+Experimental.swift | 2 +- .../{BorrowTests.swift => RefTests.swift} | 12 +- Xcode/Collections.xcodeproj/project.pbxproj | 16 +- Xcode/Shared.xcconfig | 2 +- 20 files changed, 239 insertions(+), 141 deletions(-) delete mode 100644 Sources/ContainersPreview/Types/Borrow.swift create mode 100644 Sources/ContainersPreview/Types/Ref.swift rename Tests/ContainersTests/{BorrowTests.swift => RefTests.swift} (74%) diff --git a/README.md b/README.md index a999dfb4..fb33de4a 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,7 @@ These features are disabled by default. The package provides several package tra This trait enables the following types in the [`ContainersPreview`][ContainersPreview] module: - [`struct InputSpan`][InputSpan] is a reference to a contiguous region of consumable items. -- [`struct Borrow`][Borrow] represents a borrowing reference to an item. +- [`struct Ref`][Ref] represents a borrowing reference to an item. - [`struct Inout`][Inout] represents a mutating reference to an item. - [`protocol BorrowingSequence`][BorrowingSequence] models borrowing sequences with ephemeral lifetimes. @@ -164,7 +164,7 @@ This trait enables the following types in the [`ContainersPreview`][ContainersPr - [`protocol Drain`][Drain] refines `Producer` to model an in-place consumable elements -- primarily for use around container types. [InputSpan]: https://github.com/apple/swift-collections/blob/main/Sources/ContainersPreview/Types/InputSpan.swift -[Borrow]: https://github.com/apple/swift-collections/blob/main/Sources/ContainersPreview/Types/Borrow.swift +[Ref]: https://github.com/apple/swift-collections/blob/main/Sources/ContainersPreview/Types/Ref.swift [Inout]: https://github.com/apple/swift-collections/blob/main/Sources/ContainersPreview/Types/Inout.swift [BorrowingSequence]: https://github.com/apple/swift-collections/blob/main/Sources/ContainersPreview/Protocols/BorrowingSequence.swift [BorrowingIteratorProtocol]: https://github.com/apple/swift-collections/blob/main/Sources/ContainersPreview/Protocols/BorrowingIteratorProtocol.swift diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Experimental.swift b/Sources/BasicContainers/RigidArray/RigidArray+Experimental.swift index f121b0b9..1ce41128 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Experimental.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Experimental.swift @@ -23,9 +23,9 @@ import ContainersPreview extension RigidArray where Element: ~Copyable { @inlinable @_lifetime(borrow self) - public func borrowElement(at index: Int) -> Borrow { + public func borrowElement(at index: Int) -> Ref { _checkItemIndex(index) - return unsafe Borrow( + return unsafe Ref( unsafeAddress: _storage.baseAddress.unsafelyUnwrapped.advanced(by: index), borrowing: self ) diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift index 804c15e0..b5463eae 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift @@ -22,12 +22,12 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { @frozen public struct Indices: ~Escapable { @_alwaysEmitIntoClient - package let _base: Borrow<_HTable> - + package let _base: Ref<_HTable> + @_alwaysEmitIntoClient @_lifetime(borrow _base) package init(_base: borrowing @_addressable _HTable) { - self._base = Borrow(_base) + self._base = Ref(_base) } } diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Insertions.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Insertions.swift index 799672fa..d61f51e2 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Insertions.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Insertions.swift @@ -103,7 +103,7 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { public mutating func memoizedValue( forKey key: consuming Key, _ body: (borrowing Key) throws(E) -> Value - ) throws(E) -> Borrow { + ) throws(E) -> Ref { let r = _find(key) let bucket: _Bucket if let b = r.bucket { diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Lookup.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Lookup.swift index e65b424d..de352579 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Lookup.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Lookup.swift @@ -37,13 +37,13 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { @_lifetime(borrow self) public func value( forKey key: borrowing Key - ) -> Borrow? { + ) -> Ref? { guard let bucket = self._find(key).bucket else { return nil } - return Borrow(unsafeAddress: _valuePtr(at: bucket), borrowing: self) + return Ref(unsafeAddress: _valuePtr(at: bucket), borrowing: self) } #endif - /// A stand-in for a `struct Borrow`-returning lookup operation. + /// A stand-in for a `struct Ref`-returning lookup operation. /// This is quite clumsy to use, but this is the best we can do without a way /// to express optional borrows. @_alwaysEmitIntoClient @@ -59,16 +59,16 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { #if UnstableContainersPreview @_alwaysEmitIntoClient @_lifetime(borrow self) - package func _borrowKey(at bucket: _Bucket) -> Borrow { + package func _borrowKey(at bucket: _Bucket) -> Ref { assert(_keys._table.isOccupied(bucket)) - return Borrow(unsafeAddress: _keyPtr(at: bucket), borrowing: self) + return Ref(unsafeAddress: _keyPtr(at: bucket), borrowing: self) } @_alwaysEmitIntoClient @_lifetime(borrow self) - package func _borrowValue(at bucket: _Bucket) -> Borrow { + package func _borrowValue(at bucket: _Bucket) -> Ref { assert(_keys._table.isOccupied(bucket)) - return Borrow(unsafeAddress: _valuePtr(at: bucket), borrowing: self) + return Ref(unsafeAddress: _valuePtr(at: bucket), borrowing: self) } #endif } diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Lookup.swift b/Sources/BasicContainers/RigidSet/RigidSet+Lookup.swift index f240ad33..8ca866fa 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet+Lookup.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet+Lookup.swift @@ -43,9 +43,9 @@ extension RigidSet where Element: ~Copyable { #if UnstableContainersPreview @_alwaysEmitIntoClient @_lifetime(borrow self) - package func _borrowValue(at bucket: _Bucket) -> Borrow { + package func _borrowValue(at bucket: _Bucket) -> Ref { assert(self._table.isOccupied(bucket)) - return Borrow(unsafeAddress: self._memberPtr(at: bucket), borrowing: self) + return Ref(unsafeAddress: self._memberPtr(at: bucket), borrowing: self) } #endif } diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Experimental.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Experimental.swift index 1c0c71e5..9533a3fc 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Experimental.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Experimental.swift @@ -46,7 +46,7 @@ extension UniqueArray where Element: ~Copyable { @inlinable @inline(__always) @_lifetime(borrow self) - public func borrowElement(at index: Int) -> Borrow { + public func borrowElement(at index: Int) -> Ref { _storage.borrowElement(at: index) } } diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Insertions.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Insertions.swift index bbc691f1..c0e1c7fa 100644 --- a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Insertions.swift +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Insertions.swift @@ -60,7 +60,7 @@ extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { public mutating func memoizedValue( forKey key: consuming Key, _ body: (borrowing Key) throws(E) -> Value - ) throws(E) -> Borrow { + ) throws(E) -> Ref { let r = _storage._find(key) let bucket: _Bucket if let b = r.bucket { diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Lookup.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Lookup.swift index bfdb387b..044b65d0 100644 --- a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Lookup.swift +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Lookup.swift @@ -29,13 +29,13 @@ extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { @_lifetime(borrow self) public func value( forKey key: borrowing Key - ) -> Borrow? { + ) -> Ref? { // FIXME: Why is this override necessary? Is it sound? It was triggered by RigidDictionary becoming `@_addressableForDependencies`. _overrideLifetime(_storage.value(forKey: key), borrowing: self) } #endif - /// A stand-in for a `struct Borrow`-returning lookup operation. + /// A stand-in for a `struct Ref`-returning lookup operation. /// This is quite clumsy to use, but this is the best we can do without a way /// to express optional borrows. @_alwaysEmitIntoClient diff --git a/Sources/ContainersPreview/CMakeLists.txt b/Sources/ContainersPreview/CMakeLists.txt index 17f2bc81..d0cd1afd 100644 --- a/Sources/ContainersPreview/CMakeLists.txt +++ b/Sources/ContainersPreview/CMakeLists.txt @@ -58,7 +58,7 @@ target_sources(${module_name} PRIVATE "Protocols/Producer+Filter.swift" "Protocols/Producer+Map.swift" "Protocols/Producer+Reduce.swift" - "Types/Borrow.swift" + "Types/Ref.swift" "Types/UniqueBox.swift" "Types/Inout.swift" "Types/InputSpan.swift" diff --git a/Sources/ContainersPreview/Protocols/Container/Container+Filter.swift b/Sources/ContainersPreview/Protocols/Container/Container+Filter.swift index 41592694..0d5ced03 100644 --- a/Sources/ContainersPreview/Protocols/Container/Container+Filter.swift +++ b/Sources/ContainersPreview/Protocols/Container/Container+Filter.swift @@ -49,7 +49,7 @@ where Base.Element: ~Copyable public let _isIncluded: (borrowing Element) -> Bool @_alwaysEmitIntoClient - public let _base: Borrow // FIXME: This does not allow escapable Bases + public let _base: Ref // FIXME: This does not allow escapable Bases @_alwaysEmitIntoClient public var _position: Base.Index @@ -60,7 +60,7 @@ where Base.Element: ~Copyable @inlinable @_lifetime(copy _base) internal init( - _base: Borrow, + _base: Ref, index: Base.Index, isIncluded: @escaping (borrowing Element) -> Bool ) { @@ -77,7 +77,7 @@ where Base.Element: ~Copyable isIncluded: @escaping (borrowing Element) -> Bool ) { self._isIncluded = isIncluded - self._base = Borrow(_base) + self._base = Ref(_base) self._position = _base.startIndex self._remainder = .init() } diff --git a/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift b/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift index 6e8b7680..7e68abe0 100644 --- a/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift +++ b/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift @@ -34,12 +34,12 @@ where Base.Element: ~Copyable { public typealias Element = Base.Element - let _base: Borrow // FIXME: This doesn't support nonescapable Bases + let _base: Ref // FIXME: This doesn't support nonescapable Bases var _position: Base.Index @_lifetime(borrow base) init(_borrowing base: borrowing @_addressable Base, from position: Base.Index) { - self._base = Borrow(_borrowing: base) + self._base = Ref(base) self._position = position } } diff --git a/Sources/ContainersPreview/Types/Borrow.swift b/Sources/ContainersPreview/Types/Borrow.swift deleted file mode 100644 index d606a1e7..00000000 --- a/Sources/ContainersPreview/Types/Borrow.swift +++ /dev/null @@ -1,94 +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 compiler(>=6.2) && UnstableContainersPreview -import Builtin - -@available(SwiftStdlib 5.0, *) -@frozen -@safe -public struct Borrow: Copyable, ~Escapable { - @usableFromInline - package let _pointer: UnsafePointer - - @_lifetime(borrow value) - @_alwaysEmitIntoClient - @_transparent - internal init(_borrowing value: borrowing @_addressable Target) { - unsafe _pointer = UnsafePointer(Builtin.unprotectedAddressOfBorrow(value)) - } - -#if compiler(>=6.3) // rdar://161844406 (https://github.com/swiftlang/swift/pull/84748) - @_lifetime(borrow value) - @_alwaysEmitIntoClient - @_transparent - public init(_ value: borrowing @_addressable Target) { - unsafe _pointer = UnsafePointer(Builtin.unprotectedAddressOfBorrow(value)) - } -#endif - - @_lifetime(borrow owner) - @_alwaysEmitIntoClient - @_transparent - public init( - unsafeAddress: UnsafePointer, - borrowing owner: borrowing Owner - ) { - unsafe _pointer = unsafeAddress - } - - @_lifetime(copy owner) - @_alwaysEmitIntoClient - @_transparent - public init( - unsafeAddress: UnsafePointer, - copying owner: borrowing Owner - ) { - unsafe _pointer = unsafeAddress - } - - @_alwaysEmitIntoClient - @available(*, deprecated, renamed: "value") - public subscript() -> Target { - @_transparent - unsafeAddress { - unsafe _pointer - } - } - - @_alwaysEmitIntoClient - public var value: Target { - @_transparent - unsafeAddress { - unsafe _pointer - } - } -} - -extension Optional where Wrapped: ~Copyable /* FIXME: ~Escapable */ { - @available(SwiftStdlib 5.0, *) - @_lifetime(borrow self) - @_addressableSelf - public func borrow() -> Borrow? { - if self == nil { - return nil - } - - let pointer = unsafe UnsafePointer( - Builtin.unprotectedAddressOfBorrow(self) - ) - - return unsafe Borrow(unsafeAddress: pointer, borrowing: self) - } -} -#endif diff --git a/Sources/ContainersPreview/Types/Ref.swift b/Sources/ContainersPreview/Types/Ref.swift new file mode 100644 index 00000000..70eec0d4 --- /dev/null +++ b/Sources/ContainersPreview/Types/Ref.swift @@ -0,0 +1,192 @@ +//===----------------------------------------------------------------------===// +// +// 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.3) && UnstableContainersPreview +import Builtin + +@available(*, deprecated, renamed: "Ref") +public typealias Borrow = Ref + +#if compiler(>=6.4) +/// A safe reference allowing in-place reads to a shared value. +@available(SwiftStdlib 5.0, *) +@frozen +public struct Ref: Copyable, ~Escapable { + @available(*, deprecated, renamed: "Value") + public typealias Target = Value + + @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 // compiler(>=6.3) && compiler(<6.4) + +/// 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 Target) { + 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 Target) { + 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 { + unsafe _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) + _builtin = unsafe Builtin.makeBorrow(pointer.pointee) +#else + _pointer = pointer +#endif + } +} + +@available(SwiftStdlib 5.0, *) +extension Ref where Value: ~Copyable { +#if compiler(>=6.4) + @_alwaysEmitIntoClient + @available(*, deprecated, renamed: "value") + public subscript() -> Value { + @_transparent + borrow { + value + } + } +#else + @_alwaysEmitIntoClient + @available(*, deprecated, renamed: "value") + public subscript() -> Value { + @_transparent + unsafeAddress { + unsafe _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 + } + + let pointer = unsafe UnsafePointer( + Builtin.unprotectedAddressOfBorrow(self) + ) + + return unsafe Ref(unsafeAddress: pointer, borrowing: self) + } +} + +#endif diff --git a/Sources/ContainersPreview/Types/UniqueBox.swift b/Sources/ContainersPreview/Types/UniqueBox.swift index e6d882b5..7fc7b79e 100644 --- a/Sources/ContainersPreview/Types/UniqueBox.swift +++ b/Sources/ContainersPreview/Types/UniqueBox.swift @@ -204,8 +204,8 @@ extension UniqueBox where Value: ~Copyable { @_alwaysEmitIntoClient @_transparent @_lifetime(borrow self) - public func borrow() -> Borrow { - unsafe Borrow(unsafeAddress: UnsafePointer(_pointer), borrowing: self) + public func borrow() -> Ref { + unsafe Ref(unsafeAddress: UnsafePointer(_pointer), borrowing: self) } #endif diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Experimental.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Experimental.swift index 77aca696..2058d0c4 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Experimental.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Experimental.swift @@ -24,10 +24,10 @@ extension RigidDeque where Element: ~Copyable { @_alwaysEmitIntoClient @_transparent @_lifetime(borrow self) - public func borrowElement(at index: Int) -> Borrow { + public func borrowElement(at index: Int) -> Ref { _checkItemIndex(index) let slot = _handle.slot(forOffset: index) - return Borrow(unsafeAddress: _handle.ptr(at: slot), borrowing: self) + return Ref(unsafeAddress: _handle.ptr(at: slot), borrowing: self) } @_alwaysEmitIntoClient diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Experimental.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Experimental.swift index 842cf09c..fe9c1e59 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Experimental.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Experimental.swift @@ -23,7 +23,7 @@ import ContainersPreview @_alwaysEmitIntoClient @_transparent @_lifetime(borrow self) -public func borrowElement(at index: Int) -> Borrow { +public func borrowElement(at index: Int) -> Ref { _storage.borrowElement(at: index) } diff --git a/Tests/ContainersTests/BorrowTests.swift b/Tests/ContainersTests/RefTests.swift similarity index 74% rename from Tests/ContainersTests/BorrowTests.swift rename to Tests/ContainersTests/RefTests.swift index 236267ff..31138263 100644 --- a/Tests/ContainersTests/BorrowTests.swift +++ b/Tests/ContainersTests/RefTests.swift @@ -11,18 +11,18 @@ // //===----------------------------------------------------------------------===// -#if COLLECTIONS_CONTAINERS_PREVIEW +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW import XCTest import ContainersPreview import Synchronization -final class BorrowTests: XCTestCase { +final class RefTests: XCTestCase { @available(SwiftStdlib 5.0, *) - func test_basic() { - let x: Atomic? = Atomic(0) - + func test_optional() { + let x: Atomic? = Atomic(42) + if let y = x.borrow() { - XCTAssertEqual(y[].load(ordering: .relaxed), 0) + XCTAssertEqual(y[].load(ordering: .relaxed), 42) } } } diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index aaf0f2ad..3a38c144 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -14,7 +14,7 @@ 7D2A773C2F27F1E4005780C7 /* TemporaryAllocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77282F27F1E4005780C7 /* TemporaryAllocation.swift */; }; 7D2A773D2F27F1E4005780C7 /* BorrowingSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A772B2F27F1E4005780C7 /* BorrowingSequence.swift */; }; 7D2A773E2F27F1E4005780C7 /* BorrowingSequence+Standard Conformances.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A772C2F27F1E4005780C7 /* BorrowingSequence+Standard Conformances.swift */; }; - 7D2A773F2F27F1E4005780C7 /* Borrow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77322F27F1E4005780C7 /* Borrow.swift */; }; + 7D2A773F2F27F1E4005780C7 /* Ref.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77322F27F1E4005780C7 /* Ref.swift */; }; 7D2A77402F27F1E4005780C7 /* BorrowingIteratorProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A772A2F27F1E4005780C7 /* BorrowingIteratorProtocol.swift */; }; 7D2A77412F27F1E4005780C7 /* Inout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77342F27F1E4005780C7 /* Inout.swift */; }; 7D2A77422F27F1E4005780C7 /* OutputSpan+Extras.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77272F27F1E4005780C7 /* OutputSpan+Extras.swift */; }; @@ -63,7 +63,7 @@ 7D2A779B2F27F2BF005780C7 /* UniqueDeque+Experimental.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A77712F27F2BF005780C7 /* UniqueDeque+Experimental.swift */; }; 7D2A779C2F27F2BF005780C7 /* Deque+ExpressibleByArrayLiteral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A775C2F27F2BF005780C7 /* Deque+ExpressibleByArrayLiteral.swift */; }; 7D2A779F2F27F367005780C7 /* InoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A779E2F27F367005780C7 /* InoutTests.swift */; }; - 7D2A77A02F27F367005780C7 /* BorrowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A779D2F27F367005780C7 /* BorrowTests.swift */; }; + 7D2A77A02F27F367005780C7 /* RefTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A779D2F27F367005780C7 /* RefTests.swift */; }; 7D479CB02F450E4600A8CB59 /* RigidSet+BorrowingSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CAF2F450E3F00A8CB59 /* RigidSet+BorrowingSequence.swift */; }; 7D479CB32F45296A00A8CB59 /* UniqueSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CB12F45296000A8CB59 /* UniqueSet.swift */; }; 7D479CB52F452D6900A8CB59 /* UniqueSetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CB42F452D6700A8CB59 /* UniqueSetTests.swift */; }; @@ -610,7 +610,7 @@ 7D2A772D2F27F1E4005780C7 /* BorrowingSequence+Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BorrowingSequence+Utilities.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 /* Borrow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Borrow.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 /* Inout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Inout.swift; sourceTree = ""; }; 7D2A77352F27F1E4005780C7 /* InputSpan.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputSpan.swift; sourceTree = ""; }; @@ -656,7 +656,7 @@ 7D2A77742F27F2BF005780C7 /* UniqueDeque+Prepend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDeque+Prepend.swift"; sourceTree = ""; }; 7D2A77752F27F2BF005780C7 /* UniqueDeque+Removals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDeque+Removals.swift"; sourceTree = ""; }; 7D2A77762F27F2BF005780C7 /* UniqueDeque+Replacements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDeque+Replacements.swift"; sourceTree = ""; }; - 7D2A779D2F27F367005780C7 /* BorrowTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BorrowTests.swift; sourceTree = ""; }; + 7D2A779D2F27F367005780C7 /* RefTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefTests.swift; sourceTree = ""; }; 7D2A779E2F27F367005780C7 /* InoutTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InoutTests.swift; sourceTree = ""; }; 7D479CAF2F450E3F00A8CB59 /* RigidSet+BorrowingSequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidSet+BorrowingSequence.swift"; sourceTree = ""; }; 7D479CB12F45296000A8CB59 /* UniqueSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueSet.swift; sourceTree = ""; }; @@ -1284,7 +1284,7 @@ 7D2A77372F27F1E4005780C7 /* Types */ = { isa = PBXGroup; children = ( - 7D2A77322F27F1E4005780C7 /* Borrow.swift */, + 7D2A77322F27F1E4005780C7 /* Ref.swift */, 7D2A77332F27F1E4005780C7 /* UniqueBox.swift */, 7D2A77342F27F1E4005780C7 /* Inout.swift */, 7D2A77352F27F1E4005780C7 /* InputSpan.swift */, @@ -1413,7 +1413,7 @@ children = ( 7D7646052FAAA2B9008639E5 /* UniqueBoxTests.swift */, 7D72F5972F75E5E6004AB792 /* BorrowingSequenceTests.swift */, - 7D2A779D2F27F367005780C7 /* BorrowTests.swift */, + 7D2A779D2F27F367005780C7 /* RefTests.swift */, 7D2A779E2F27F367005780C7 /* InoutTests.swift */, ); path = ContainersTests; @@ -2881,7 +2881,7 @@ 7D2A773C2F27F1E4005780C7 /* TemporaryAllocation.swift in Sources */, 7D2A773D2F27F1E4005780C7 /* BorrowingSequence.swift in Sources */, 7D2A773E2F27F1E4005780C7 /* BorrowingSequence+Standard Conformances.swift in Sources */, - 7D2A773F2F27F1E4005780C7 /* Borrow.swift in Sources */, + 7D2A773F2F27F1E4005780C7 /* Ref.swift in Sources */, 7D57A2D02F353946002543BD /* RigidArray+Deprecated.swift in Sources */, 7D2A77402F27F1E4005780C7 /* BorrowingIteratorProtocol.swift in Sources */, 7D2A77412F27F1E4005780C7 /* Inout.swift in Sources */, @@ -3135,7 +3135,7 @@ 7DE9220729CA8576004483EB /* BitSet.Counted Tests.swift in Sources */, 7DE9220F29CA8576004483EB /* OrderedDictionary Tests.swift in Sources */, 7D2A779F2F27F367005780C7 /* InoutTests.swift in Sources */, - 7D2A77A02F27F367005780C7 /* BorrowTests.swift in Sources */, + 7D2A77A02F27F367005780C7 /* RefTests.swift in Sources */, 7D48BCDF2E51558700D7F6F7 /* TestPrintable.swift in Sources */, 7D48BCE02E51558700D7F6F7 /* LifetimeTrackedStruct.swift in Sources */, 7D2A77492F27F271005780C7 /* CustomProducer.swift in Sources */, diff --git a/Xcode/Shared.xcconfig b/Xcode/Shared.xcconfig index 14518712..0ec77b50 100644 --- a/Xcode/Shared.xcconfig +++ b/Xcode/Shared.xcconfig @@ -37,7 +37,7 @@ OTHER_SWIFT_FLAGS = $(inherited) \ ONLY_ACTIVE_ARCH[config=Debug] = YES -SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) COLLECTIONS_SINGLE_MODULE +SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) COLLECTIONS_SINGLE_MODULE COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW COLLECTIONS_UNSTABLE_HASHED_CONTAINERS SWIFT_ACTIVE_COMPILATION_CONDITIONS[config=Debug] = $(inherited) DEBUG //COLLECTIONS_INTERNAL_CHECKS SWIFT_COMPILATION_MODE[config=Release] = wholemodule