From 0bd8dec08b8bb484f4a39e8d241a40c8e0c8a051 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 14 May 2025 16:31:21 -0700 Subject: [PATCH] Reimplement _specialize(_:for:) for the 5.9 stdlib --- .../CMakeLists.txt | 1 + .../Specialize.swift.gyb | 35 ++++++++++++ .../autogenerated/Specialize.swift | 54 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 Sources/InternalCollectionsUtilities/Specialize.swift.gyb create mode 100644 Sources/InternalCollectionsUtilities/autogenerated/Specialize.swift diff --git a/Sources/InternalCollectionsUtilities/CMakeLists.txt b/Sources/InternalCollectionsUtilities/CMakeLists.txt index 0adb28f1..1e8119e7 100644 --- a/Sources/InternalCollectionsUtilities/CMakeLists.txt +++ b/Sources/InternalCollectionsUtilities/CMakeLists.txt @@ -24,6 +24,7 @@ target_sources(${module_name} PRIVATE "autogenerated/Debugging.swift" "autogenerated/Descriptions.swift" "autogenerated/RandomAccessCollection+Offsets.swift" + "autogenerated/Specialize.swift" "autogenerated/UnsafeBufferPointer+Extras.swift" "autogenerated/UnsafeMutableBufferPointer+Extras.swift" "IntegerTricks/autogenerated/FixedWidthInteger+roundUpToPowerOfTwo.swift" diff --git a/Sources/InternalCollectionsUtilities/Specialize.swift.gyb b/Sources/InternalCollectionsUtilities/Specialize.swift.gyb new file mode 100644 index 00000000..1ecea5cc --- /dev/null +++ b/Sources/InternalCollectionsUtilities/Specialize.swift.gyb @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2023 - 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 +// +//===----------------------------------------------------------------------===// + +%{ + from gyb_utils import * +}% +${autogenerated_warning()} + +#if compiler(<5.10) +% for modifier in visibility_levels: +${visibility_boilerplate(modifier)} +/// Returns `x` as its concrete type `U`, or `nil` if `x` has a different +/// concrete type. +/// +/// This cast can be useful for dispatching to specializations of generic +/// functions. +@_alwaysEmitIntoClient +@_transparent +${modifier} func _specialize(_ x: T, for: U.Type) -> U? { + guard T.self == U.self else { + return nil + } + return unsafeBitCast(x, to: U.self) +} +% end +${visibility_boilerplate("end")} +#endif diff --git a/Sources/InternalCollectionsUtilities/autogenerated/Specialize.swift b/Sources/InternalCollectionsUtilities/autogenerated/Specialize.swift new file mode 100644 index 00000000..1000363c --- /dev/null +++ b/Sources/InternalCollectionsUtilities/autogenerated/Specialize.swift @@ -0,0 +1,54 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2023 - 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 +// +//===----------------------------------------------------------------------===// + + +// ############################################################################# +// # # +// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. # +// # # +// ############################################################################# + + +#if compiler(<5.10) + +// In single module mode, we need these declarations to be internal, +// but in regular builds we want them to be public. Unfortunately +// the current best way to do this is to duplicate all definitions. +#if COLLECTIONS_SINGLE_MODULE +/// Returns `x` as its concrete type `U`, or `nil` if `x` has a different +/// concrete type. +/// +/// This cast can be useful for dispatching to specializations of generic +/// functions. +@_alwaysEmitIntoClient +@_transparent +internal func _specialize(_ x: T, for: U.Type) -> U? { + guard T.self == U.self else { + return nil + } + return unsafeBitCast(x, to: U.self) +} +#else // !COLLECTIONS_SINGLE_MODULE +/// Returns `x` as its concrete type `U`, or `nil` if `x` has a different +/// concrete type. +/// +/// This cast can be useful for dispatching to specializations of generic +/// functions. +@_alwaysEmitIntoClient +@_transparent +public func _specialize(_ x: T, for: U.Type) -> U? { + guard T.self == U.self else { + return nil + } + return unsafeBitCast(x, to: U.self) +} +#endif // COLLECTIONS_SINGLE_MODULE +#endif