Skip to content

Commit 1f5f3b8

Browse files
committed
[libc++] Fix triviality of std::pair for trivially copyable types without an assignment operator
Since 83ead2b, std::pair would not be trivially copyable when it holds a trivially copyable type without an assignment operator. That is because pair gained an elligible copy-assignment-operator (the const version) in 83ead2b in C++ >= 23. This means that the trivially copyable property of std::pair for such types would be inconsistent between C++11/14/17/20 (trivially copyable) and C++23/26 (not trivially copyable). This patch makes std::pair's behavior consistent in all Standard modes EXCEPT C++03, which is a pre-existing condition and we have no way of changing (also, it shouldn't matter because the std::is_trivially_copyable trait was introduced in C++11). While this is not technically an ABI break, in practice we do know that folks sometimes use a different representation based on whether a type is trivially copyable. So we're treating 83ead2b as an ABI break and this patch is fixing said breakage. This patch also adds tests stolen from #89652 that pin down the ABI of std::pair with respect to being trivially copyable. Fixes #95428
1 parent 57b8be4 commit 1f5f3b8

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

libcxx/include/__utility/pair.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ struct _LIBCPP_TEMPLATE_VIS pair
267267
}
268268

269269
# if _LIBCPP_STD_VER >= 23
270+
template <class = void>
270271
_LIBCPP_HIDE_FROM_ABI constexpr const pair& operator=(pair const& __p) const
271272
noexcept(is_nothrow_copy_assignable_v<const first_type> && is_nothrow_copy_assignable_v<const second_type>)
272273
requires(is_copy_assignable_v<const first_type> && is_copy_assignable_v<const second_type>)
@@ -276,6 +277,7 @@ struct _LIBCPP_TEMPLATE_VIS pair
276277
return *this;
277278
}
278279

280+
template <class = void>
279281
_LIBCPP_HIDE_FROM_ABI constexpr const pair& operator=(pair&& __p) const
280282
noexcept(is_nothrow_assignable_v<const first_type&, first_type> &&
281283
is_nothrow_assignable_v<const second_type&, second_type>)

libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/trivial_copy_move_ABI.pass.cpp renamed to libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/abi.trivial_copy_move.pass.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ struct Trivial {
7171
static_assert(HasTrivialABI<Trivial>::value, "");
7272
#endif
7373

74+
struct TrivialNoAssignment {
75+
int arr[4];
76+
TrivialNoAssignment& operator=(const TrivialNoAssignment&) = delete;
77+
};
78+
79+
struct TrivialNoConstruction {
80+
int arr[4];
81+
TrivialNoConstruction() = default;
82+
TrivialNoConstruction(const TrivialNoConstruction&) = delete;
83+
TrivialNoConstruction& operator=(const TrivialNoConstruction&) = default;
84+
};
7485

7586
void test_trivial()
7687
{
@@ -135,6 +146,26 @@ void test_trivial()
135146
static_assert(HasTrivialABI<P>::value, "");
136147
}
137148
#endif
149+
{
150+
using P = std::pair<TrivialNoAssignment, int>;
151+
static_assert(std::is_trivially_copy_constructible<P>::value, "");
152+
static_assert(std::is_trivially_move_constructible<P>::value, "");
153+
#if TEST_STD_VER >= 11 // This is https://llvm.org/PR90605
154+
static_assert(!std::is_trivially_copy_assignable<P>::value, "");
155+
static_assert(!std::is_trivially_move_assignable<P>::value, "");
156+
#endif // TEST_STD_VER >= 11
157+
static_assert(std::is_trivially_destructible<P>::value, "");
158+
}
159+
{
160+
using P = std::pair<TrivialNoConstruction, int>;
161+
#if TEST_STD_VER >= 11
162+
static_assert(!std::is_trivially_copy_constructible<P>::value, "");
163+
static_assert(!std::is_trivially_move_constructible<P>::value, "");
164+
#endif // TEST_STD_VER >= 11
165+
static_assert(!std::is_trivially_copy_assignable<P>::value, "");
166+
static_assert(!std::is_trivially_move_assignable<P>::value, "");
167+
static_assert(std::is_trivially_destructible<P>::value, "");
168+
}
138169
}
139170

140171
void test_layout() {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
//
10+
// This test pins down the ABI of std::pair with respect to being "trivially copyable".
11+
//
12+
13+
#include <type_traits>
14+
#include <utility>
15+
16+
#include "test_macros.h"
17+
18+
struct trivially_copyable {
19+
int arr[4];
20+
};
21+
22+
struct trivially_copyable_no_assignment {
23+
int arr[4];
24+
trivially_copyable_no_assignment& operator=(const trivially_copyable_no_assignment&) = delete;
25+
};
26+
static_assert(std::is_trivially_copyable<trivially_copyable_no_assignment>::value, "");
27+
28+
struct trivially_copyable_no_construction {
29+
int arr[4];
30+
trivially_copyable_no_construction() = default;
31+
trivially_copyable_no_construction(const trivially_copyable_no_construction&) = delete;
32+
trivially_copyable_no_construction& operator=(const trivially_copyable_no_construction&) = default;
33+
};
34+
static_assert(std::is_trivially_copyable<trivially_copyable_no_construction>::value, "");
35+
36+
static_assert((!std::is_trivially_copyable<std::pair<int&, int> >::value), "");
37+
static_assert((!std::is_trivially_copyable<std::pair<int, int&> >::value), "");
38+
static_assert((!std::is_trivially_copyable<std::pair<int&, int&> >::value), "");
39+
40+
static_assert((!std::is_trivially_copyable<std::pair<int, int> >::value), "");
41+
static_assert((!std::is_trivially_copyable<std::pair<int, char> >::value), "");
42+
static_assert((!std::is_trivially_copyable<std::pair<char, int> >::value), "");
43+
static_assert((!std::is_trivially_copyable<std::pair<std::pair<char, char>, int> >::value), "");
44+
static_assert((!std::is_trivially_copyable<std::pair<trivially_copyable, int> >::value), "");
45+
#if TEST_STD_VER == 03 // Known ABI difference
46+
static_assert((!std::is_trivially_copyable<std::pair<trivially_copyable_no_assignment, int> >::value), "");
47+
#else
48+
static_assert((std::is_trivially_copyable<std::pair<trivially_copyable_no_assignment, int> >::value), "");
49+
#endif
50+
static_assert((!std::is_trivially_copyable<std::pair<trivially_copyable_no_construction, int> >::value), "");
51+
52+
static_assert((std::is_trivially_copy_constructible<std::pair<int, int> >::value), "");
53+
static_assert((std::is_trivially_move_constructible<std::pair<int, int> >::value), "");
54+
static_assert((!std::is_trivially_copy_assignable<std::pair<int, int> >::value), "");
55+
static_assert((!std::is_trivially_move_assignable<std::pair<int, int> >::value), "");
56+
static_assert((std::is_trivially_destructible<std::pair<int, int> >::value), "");

0 commit comments

Comments
 (0)