Skip to content

Commit 8331ce5

Browse files
Masked copy/move functions affecting trivial copyability [GH #628]
1 parent 300f579 commit 8331ce5

12 files changed

Lines changed: 406 additions & 105 deletions

File tree

‎src/class_decl.c‎

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22776,6 +22776,13 @@ member of a union or anonymous union).
2277622776
rout_sym = fundamental_symbol_of(rout_sym);
2277722777
if (symbol_is(rout_sym, sk_member_function)) {
2277822778
a_routine_ptr rp = rout_sym->variant.routine.ptr;
22779+
if (!rp->compiler_generated && !rp->is_defaulted) {
22780+
/* A user-provided constructor is selected, which is not
22781+
necessarily a copy constructor (e.g., a constructor template, if
22782+
no copy constructor is eligible). */
22783+
class_symbol_supp(symbol_for(class_type))
22784+
->makes_copy_construction_nontrivial = TRUE;
22785+
} /* if */
2277922786
if (!rp->is_constexpr) {
2278022787
/* If this special member has to call a non-constexpr special
2278122788
member, it is itself not constexpr. */
@@ -24771,6 +24778,61 @@ before generating declarations for special members.
2477124778
} /* set_move_assign_operator_flags */
2477224779

2477324780

24781+
static a_boolean user_provided_copy_functions_are_masked(
24782+
a_symbol_ptr member_set,
24783+
a_boolean is_move)
24784+
/*
24785+
member_set is the constructor or assignment operator symbol of a class
24786+
(possibly an overloaded function symbol). Return TRUE if member_set includes
24787+
user-provided copy (or, if is_move is TRUE, move) constructors or assignment
24788+
operators, but none of them is eligible (N5046 [special]/6) because each one
24789+
has unsatisfied constraints or is masked by a more constrained member of the
24790+
same kind. Such functions are never selected to copy (or move) a subobject
24791+
of the class, and so they don't make the corresponding special member of an
24792+
enclosing class nontrivial. For example:
24793+
template<typename T> struct B {
24794+
B(B&&);
24795+
B(B&&) requires true = default;
24796+
};
24797+
struct D : B<int> {}; // D's move constructor is trivial.
24798+
*/
24799+
{
24800+
a_boolean any_masked = FALSE, any_eligible = FALSE, is_list;
24801+
a_symbol_ptr sym = member_set;
24802+
24803+
if (sym != NULL && symbol_is(sym, sk_overloaded_function)) {
24804+
is_list = TRUE;
24805+
sym = sym->variant.overloaded_function.symbols;
24806+
} else {
24807+
is_list = FALSE;
24808+
} /* if */
24809+
for (; !any_eligible && sym != NULL; sym = is_list ? sym->next : NULL) {
24810+
a_routine_ptr rp;
24811+
a_type_qualifier_set tqs = TQ_NONE;
24812+
a_boolean sym_is_move = FALSE;
24813+
if (!symbol_is(sym, sk_member_function)) continue;
24814+
rp = sym->variant.routine.ptr;
24815+
if (rp->compiler_generated || rp->is_defaulted || rp->is_deleted) continue;
24816+
if (special_kind_is(rp, sfk_constructor)) {
24817+
if (!is_copy_constructor(rp, (a_type_ptr)NULL, &tqs,
24818+
/*include_move_ctors=*/TRUE,
24819+
/*is_declarative_context=*/TRUE)) continue;
24820+
sym_is_move = copy_ctor_is_move_ctor(rp);
24821+
} else if (!routine_is_copy_or_move_assign_operator(rp, &tqs,
24822+
&sym_is_move)) {
24823+
continue;
24824+
} /* if */
24825+
if (sym_is_move != is_move) continue;
24826+
if (is_ineligible(sym) || is_masked_special_member(sym, member_set)) {
24827+
any_masked = TRUE;
24828+
} else {
24829+
any_eligible = TRUE;
24830+
} /* if */
24831+
} /* for */
24832+
return any_masked && !any_eligible;
24833+
} /* user_provided_copy_functions_are_masked */
24834+
24835+
2477424836
static void check_defaulted_members(
2477524837
a_type_ptr class_type,
2477624838
a_generated_special_function_descr *gsfd)
@@ -25250,22 +25312,35 @@ The routine body is not generated until it is known to be needed.
2525025312
bitwise copying of the parent class without making the copy functions
2525125313
nontrivial. A class is trivially copyable if it has a trivial destructor,
2525225314
and it is bitwise copyable/assignable, except perhaps for deleted copy
25253-
functions. */
25315+
functions. User-provided copy functions that are masked by more
25316+
constrained ones don't make the copy functions of enclosing classes
25317+
nontrivial. */
2525425318
if (cssp->has_user_provided_copy_constructor) {
25255-
cssp->makes_copy_construction_nontrivial = TRUE;
25319+
if (!user_provided_copy_functions_are_masked(cssp->constructor,
25320+
/*is_move=*/FALSE)) {
25321+
cssp->makes_copy_construction_nontrivial = TRUE;
25322+
} /* if */
2525625323
cssp->construction_by_bitwise_copy_allowed = FALSE;
2525725324
} /* if */
2525825325
if (cssp->has_user_provided_move_constructor) {
25259-
cssp->makes_move_construction_nontrivial = TRUE;
25326+
if (!user_provided_copy_functions_are_masked(cssp->constructor,
25327+
/*is_move=*/TRUE)) {
25328+
cssp->makes_move_construction_nontrivial = TRUE;
25329+
} /* if */
2526025330
cssp->construction_by_bitwise_copy_allowed = FALSE;
2526125331
} /* if */
25262-
if (user_provided_copy_assignment_op) {
25332+
if (user_provided_copy_assignment_op &&
25333+
!user_provided_copy_functions_are_masked(cssp->assignment_operator,
25334+
/*is_move=*/FALSE)) {
2526325335
cssp->makes_copy_assignment_nontrivial = TRUE;
2526425336
} /* if */
25265-
if (cssp->has_user_provided_move_assign_operator) {
25337+
if (cssp->has_user_provided_move_assign_operator &&
25338+
!user_provided_copy_functions_are_masked(cssp->assignment_operator,
25339+
/*is_move=*/TRUE)) {
2526625340
cssp->makes_move_assignment_nontrivial = TRUE;
2526725341
} /* if */
2526825342
if (cssp->makes_copy_assignment_nontrivial ||
25343+
user_provided_copy_assignment_op ||
2526925344
cssp->has_user_provided_move_assign_operator) {
2527025345
cssp->assignment_by_bitwise_copy_allowed = FALSE;
2527125346
} /* if */
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[GH #628]
2+
Masked copy/move functions affecting trivial copyability
3+
4+
A copy/move constructor or assignment operator that is masked by a more
5+
constrained one of the same kind is not "eligible", and it should therefore
6+
not affect whether the class is trivially copyable. However, two cases were
7+
previously handled incorrectly. First, the masking check compared the
8+
complete function types, so a difference in exception specifications kept a
9+
function from being masked. Second, a masked user-provided function of a base
10+
class or member made the corresponding implicit function of the enclosing
11+
class nontrivial, even though it is not selected to copy or move the subobject.
12+
For example, with --c++20:
13+
14+
template<typename T> struct S {
15+
S(const S&) noexcept {}
16+
S(const S&) requires true = default;
17+
};
18+
static_assert(__is_trivially_copyable(S<int>)); // Previously failed.
19+
template<typename T> struct B {
20+
B(B&&);
21+
B(B&&) requires true = default;
22+
};
23+
struct D : B<int> {};
24+
static_assert(__is_trivially_copyable(D)); // Previously failed.
25+
26+
The GNU library's std::optional uses this pattern. Also, a copy/move
27+
assignment operator whose constraints are not satisfied no longer keeps a
28+
class from being trivially copyable, and the constraints of constructors that
29+
are not copy/move constructors are no longer checked to determine that.

0 commit comments

Comments
 (0)