From 779da645d4e10efe30a5eb851e6ab3bdec4e26f5 Mon Sep 17 00:00:00 2001 From: Justin Novosad Date: Fri, 5 Dec 2025 19:11:35 -0500 Subject: [PATCH 1/3] Fix __split_buffer_pointer_layout compatibility with swift's C++ import When the swift compiler generates a bridging module for importing C++ symbols, it instantiates all methods of the class template instances used by the symbols imported from C++. This is because it cannot determine which methods will be called from swift at that stage. This means that it may attempt to generate invalid instantiations (due to missing type traits) that would not be generated in a normal C++ build. The default constructor of `__split_buffer_pointer_layout` has this issue because it does not initialize `__alloc_` which may or may not be a reference depending on the `allocator_type` tempalte parameter. The initialization of class members that are references is mandatory. `vector` uses a reference type for the allocator and `deque` does not. Therefore, only `deque` is allowed to use the default constructor of `__split_buffer_pointer_layout`. When the swift compiler's C++ importer generates an instantiation of `std::vector`, it will also attempt to instantiate the default constructor of `__split_buffer_pointer_layout`, which fails to compile. This change fixes the issue by adding a `requires` clause to suppress the instantiation of the problematic constructor whenever its instatiation would be invalid. There are no tests included in this change because the `requires` statement has no observable effect on pure C++ builds. Test coverage will be automatically assured downstream in swift-project once it updates its fork of libc++. --- libcxx/CREDITS.TXT | 5 +++++ libcxx/include/__split_buffer | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/libcxx/CREDITS.TXT b/libcxx/CREDITS.TXT index aa3c8cf1a8c6e..9cdb9ae992507 100644 --- a/libcxx/CREDITS.TXT +++ b/libcxx/CREDITS.TXT @@ -112,6 +112,11 @@ N: Andrew Morrow E: andrew.c.morrow@gmail.com D: Minor patches and Linux fixes. +N: Justin Novosad +E: junov@google.com +E: justin.novosad@gmail.com +D: Minor fixes, Swift interoperability. + N: Michael Park E: mcypark@gmail.com D: Implementation of . diff --git a/libcxx/include/__split_buffer b/libcxx/include/__split_buffer index 1e05e4df8ba0f..5355dbbff9998 100644 --- a/libcxx/include/__split_buffer +++ b/libcxx/include/__split_buffer @@ -30,6 +30,9 @@ #include <__type_traits/integral_constant.h> #include <__type_traits/is_nothrow_assignable.h> #include <__type_traits/is_nothrow_constructible.h> +#if _LIBCPP_STD_VER >= 20 +# include <__type_traits/is_reference.h> +#endif #include <__type_traits/is_swappable.h> #include <__type_traits/is_trivially_destructible.h> #include <__type_traits/is_trivially_relocatable.h> @@ -68,8 +71,13 @@ protected: public: // Can't be defaulted due to _LIBCPP_COMPRESSED_PAIR not being an aggregate in C++03 and C++11. - _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer_pointer_layout() : __back_cap_(nullptr) {} - + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer_pointer_layout() +#if _LIBCPP_STD_VER >= 20 + // Prevents Swift compiler's C++ importer from implicitly instantiating this ctor when it's not supported. + requires (!is_reference_v) +#endif + : __back_cap_(nullptr) {} + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer_pointer_layout(const allocator_type& __alloc) : __back_cap_(nullptr), __alloc_(__alloc) {} From 1cf60d9b3a9a6b0eab8e8e742a81d289a17d9d15 Mon Sep 17 00:00:00 2001 From: Justin Novosad Date: Tue, 9 Dec 2025 11:28:52 -0500 Subject: [PATCH 2/3] Fix format --- libcxx/include/__split_buffer | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libcxx/include/__split_buffer b/libcxx/include/__split_buffer index 5355dbbff9998..6b53ab88480a8 100644 --- a/libcxx/include/__split_buffer +++ b/libcxx/include/__split_buffer @@ -73,11 +73,12 @@ public: // Can't be defaulted due to _LIBCPP_COMPRESSED_PAIR not being an aggregate in C++03 and C++11. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer_pointer_layout() #if _LIBCPP_STD_VER >= 20 - // Prevents Swift compiler's C++ importer from implicitly instantiating this ctor when it's not supported. - requires (!is_reference_v) + // Prevents Swift compiler's C++ importer from implicitly instantiating this ctor when it's not supported. + requires (!is_reference_v) #endif - : __back_cap_(nullptr) {} - + : __back_cap_(nullptr) { + } + _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer_pointer_layout(const allocator_type& __alloc) : __back_cap_(nullptr), __alloc_(__alloc) {} From 6a88ec20b69b3c63425e5c062f4aeac6275efeb6 Mon Sep 17 00:00:00 2001 From: Justin Novosad Date: Tue, 9 Dec 2025 12:24:11 -0500 Subject: [PATCH 3/3] Include is_reference.h unconditionally --- libcxx/include/__split_buffer | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/libcxx/include/__split_buffer b/libcxx/include/__split_buffer index 6b53ab88480a8..289155b011110 100644 --- a/libcxx/include/__split_buffer +++ b/libcxx/include/__split_buffer @@ -30,9 +30,7 @@ #include <__type_traits/integral_constant.h> #include <__type_traits/is_nothrow_assignable.h> #include <__type_traits/is_nothrow_constructible.h> -#if _LIBCPP_STD_VER >= 20 -# include <__type_traits/is_reference.h> -#endif +#include <__type_traits/is_reference.h> #include <__type_traits/is_swappable.h> #include <__type_traits/is_trivially_destructible.h> #include <__type_traits/is_trivially_relocatable.h> @@ -73,8 +71,8 @@ public: // Can't be defaulted due to _LIBCPP_COMPRESSED_PAIR not being an aggregate in C++03 and C++11. _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer_pointer_layout() #if _LIBCPP_STD_VER >= 20 - // Prevents Swift compiler's C++ importer from implicitly instantiating this ctor when it's not supported. - requires (!is_reference_v) + // Prevents Swift compiler's C++ importer from implicitly instantiating this ctor when it's not supported. + requires(!is_reference_v) #endif : __back_cap_(nullptr) { }