From 1a1818df8feafdd6aba786166edadfa488690a0e Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 23 Sep 2026 15:45:26 -0700 Subject: [PATCH] Drop unused `Optional._borrow`, `._mutate` and `_insert` definitions --- .../CMakeLists.txt | 1 - .../Optional+Extras.swift | 59 ---------------- Tests/ContainersTests/MutableRefTests.swift | 70 ------------------- Tests/ContainersTests/RefTests.swift | 51 -------------- 4 files changed, 181 deletions(-) delete mode 100644 Sources/InternalCollectionsUtilities/Optional+Extras.swift diff --git a/Sources/InternalCollectionsUtilities/CMakeLists.txt b/Sources/InternalCollectionsUtilities/CMakeLists.txt index 0deea288..9353672a 100644 --- a/Sources/InternalCollectionsUtilities/CMakeLists.txt +++ b/Sources/InternalCollectionsUtilities/CMakeLists.txt @@ -41,7 +41,6 @@ target_sources(${module_name} PRIVATE "LifetimeOverride.swift" "MutableSpan+Extras.swift" "MutableRef.swift" - "Optional+Extras.swift" "OutputSpan+Extras.swift" "RandomAccessCollection+Offsets.swift" "Span+Extras.swift" diff --git a/Sources/InternalCollectionsUtilities/Optional+Extras.swift b/Sources/InternalCollectionsUtilities/Optional+Extras.swift deleted file mode 100644 index 6f02cd5d..00000000 --- a/Sources/InternalCollectionsUtilities/Optional+Extras.swift +++ /dev/null @@ -1,59 +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.4) -import Builtin - -extension Optional where Wrapped: ~Copyable /* FIXME: ~Escapable */ { - @available(SwiftStdlib 6.4, *) - @_lifetime(borrow self) - @_addressableSelf - package 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/Tests/ContainersTests/MutableRefTests.swift b/Tests/ContainersTests/MutableRefTests.swift index 7bdd4ec3..929ca9ea 100644 --- a/Tests/ContainersTests/MutableRefTests.swift +++ b/Tests/ContainersTests/MutableRefTests.swift @@ -150,75 +150,5 @@ final class MutableRefTests: CollectionTestCase { expectEqual(ref.value, 3) expectEqual(x, 3) } - - // MARK: Optional.mutate() - -#if compiler(>=6.4) - @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") - } - 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") - } - expectEqual(x!.value, 44) - } - - // MARK: Optional.insert(_:) - - @available(SwiftStdlib 6.4, *) - func test_optional_insert_into_nil() { - var x: Int? = nil - do { - var ref = x._insert(42) - expectEqual(ref.value, 42) - ref.value = 99 - } - expectEqual(x, 99) - } - - @available(SwiftStdlib 6.4, *) - func test_optional_insert_into_some() { - var x: Int? = 10 - do { - var ref = x._insert(20) - expectEqual(ref.value, 20) - ref.value = 30 - } - expectEqual(x, 30) - } - - @available(SwiftStdlib 6.4, *) - func test_optional_insert_noncopyable() { - var x: NoncopyablePayload? = nil - do { - var ref = x._insert(NoncopyablePayload(55)) - expectEqual(ref.value.value, 55) - ref.value.value = 66 - } - expectEqual(x!.value, 66) - } -#endif } #endif diff --git a/Tests/ContainersTests/RefTests.swift b/Tests/ContainersTests/RefTests.swift index ecf0eda9..27df26f6 100644 --- a/Tests/ContainersTests/RefTests.swift +++ b/Tests/ContainersTests/RefTests.swift @@ -89,56 +89,5 @@ final class RefTests: CollectionTestCase { expectEqual(ref2.value, 42) } - // MARK: Optional.borrow() - - @available(SwiftStdlib 6.4, *) - func test_optional_borrow_some() { - let x: Int? = 77 - if let ref = x._borrow() { - expectEqual(ref.value, 77) - } else { - expectFailure("Expected non-nil Ref from .some optional") - } - } - - @available(SwiftStdlib 6.4, *) - func test_optional_borrow_none() { - let x: Int? = nil - let ref = x._borrow() - expectNil(ref) - } - - @available(SwiftStdlib 6.4, *) - func test_optional_borrow_string() { - let x: String? = "world" - if let ref = x._borrow() { - expectEqual(ref.value, "world") - } else { - expectFailure("Expected non-nil Ref from .some optional") - } - } - - @available(SwiftStdlib 6.4, *) - func test_optional_borrow_noncopyable() { - let x: NoncopyablePayload? = NoncopyablePayload(33) - if let ref = x._borrow() { - expectEqual(ref.value.value, 33) - } else { - expectFailure("Expected non-nil Ref from .some optional") - } - } - - @available(SwiftStdlib 6.4, *) - func test_optional_borrow_reference_type() { - let tracker = LifetimeTracker() - let obj = tracker.instance(for: 5) - let x: LifetimeTracked? = obj - if let ref = x._borrow() { - expectTrue(ref.value === obj) - expectEqual(ref.value.payload, 5) - } else { - expectFailure("Expected non-nil Ref from .some optional") - } - } } #endif