diff --git a/Sources/DequeModule/Deque/_DequeBuffer.swift b/Sources/DequeModule/Deque/_DequeBuffer.swift index a26d4aa0..80eeda60 100644 --- a/Sources/DequeModule/Deque/_DequeBuffer.swift +++ b/Sources/DequeModule/Deque/_DequeBuffer.swift @@ -11,6 +11,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + @_fixed_layout @usableFromInline internal final class _DequeBuffer: ManagedBuffer<_DequeBufferHeader, Element> { @@ -45,9 +49,10 @@ extension _DequeBuffer: CustomStringConvertible { /// The type-punned empty singleton storage instance. @usableFromInline -nonisolated(unsafe) internal let _emptyDequeStorage - = _DequeBuffer.create( - minimumCapacity: 0, - makingHeaderWith: { _ in - _DequeBufferHeader(capacity: 0, count: 0, startSlot: .init(at: 0)) - }) +nonisolated(unsafe) internal let _emptyDequeStorage = ( + _immortalize( + _DequeBuffer.create( + minimumCapacity: 0, + makingHeaderWith: { _ in + _DequeBufferHeader(capacity: 0, count: 0, startSlot: .init(at: 0)) + }))) diff --git a/Sources/InternalCollectionsUtilities/CMakeLists.txt b/Sources/InternalCollectionsUtilities/CMakeLists.txt index 8e8e9895..1dc7af38 100644 --- a/Sources/InternalCollectionsUtilities/CMakeLists.txt +++ b/Sources/InternalCollectionsUtilities/CMakeLists.txt @@ -35,6 +35,7 @@ target_sources(${module_name} PRIVATE "BorrowingIteratorProtocol+Extras.swift" "Debugging.swift" "Descriptions.swift" + "Immortalize.swift" "Iterable+CopyContents.swift" "Iterable+Equality.swift" "LifetimeOverride.swift" diff --git a/Sources/InternalCollectionsUtilities/Immortalize.swift b/Sources/InternalCollectionsUtilities/Immortalize.swift new file mode 100644 index 00000000..d1b220ff --- /dev/null +++ b/Sources/InternalCollectionsUtilities/Immortalize.swift @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2022 - 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 +// +//===----------------------------------------------------------------------===// + +import SwiftShims + +/// Immortalize the given instance, ensuring that it never deinitializes, and +/// rendering its refcount operations noops. +/// +/// This intentionally leaks memory, but it avoids a costly compare/exchange +/// loop on retains/releases. +@inlinable +@_alwaysEmitIntoClient +package func _immortalize(_ object: consuming T) -> T { + unsafe _swift_stdlib_immortalize(Unmanaged.passUnretained(object).toOpaque()) + return object +}