From 8331ce58361a3dcf5feffb409adef05a2a5eff7d Mon Sep 17 00:00:00 2001 From: Daveed Vandevoorde Date: Tue, 1 Dec 2026 16:50:17 +0000 Subject: [PATCH] Masked copy/move functions affecting trivial copyability [GH #628] --- src/class_decl.c | 85 +++++++- ...h-628-masked-copy-functions-triviality.txt | 29 +++ src/types.c | 198 +++++++++++++----- src/types.h | 2 + tests/expectations/edg_x86_64/imported.log | 1 - .../default.1.1.txt | 42 +--- .../cpp/cpp2a/.cond-triv3.rto/default.1.1.txt | 5 - .../regressions/.gh-628.rto/default.1.1.txt | 1 + .../regressions/.gh-628.rto/default.2.1.txt | 1 + .../regressions/.gh-628.rto/default.3.1.txt | 1 + .../regressions/.gh-628.rto/default.4.1.txt | 1 + tests/tests/regressions/gh-628.sft.cpp | 145 +++++++++++++ 12 files changed, 406 insertions(+), 105 deletions(-) create mode 100644 src/pending_changes/gh-628-masked-copy-functions-triviality.txt create mode 100644 tests/tests/regressions/.gh-628.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.gh-628.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.gh-628.rto/default.3.1.txt create mode 100644 tests/tests/regressions/.gh-628.rto/default.4.1.txt create mode 100644 tests/tests/regressions/gh-628.sft.cpp diff --git a/src/class_decl.c b/src/class_decl.c index 30cfd6a7ba..affb51fd29 100644 --- a/src/class_decl.c +++ b/src/class_decl.c @@ -22776,6 +22776,13 @@ member of a union or anonymous union). rout_sym = fundamental_symbol_of(rout_sym); if (symbol_is(rout_sym, sk_member_function)) { a_routine_ptr rp = rout_sym->variant.routine.ptr; + if (!rp->compiler_generated && !rp->is_defaulted) { + /* A user-provided constructor is selected, which is not + necessarily a copy constructor (e.g., a constructor template, if + no copy constructor is eligible). */ + class_symbol_supp(symbol_for(class_type)) + ->makes_copy_construction_nontrivial = TRUE; + } /* if */ if (!rp->is_constexpr) { /* If this special member has to call a non-constexpr special member, it is itself not constexpr. */ @@ -24771,6 +24778,61 @@ before generating declarations for special members. } /* set_move_assign_operator_flags */ +static a_boolean user_provided_copy_functions_are_masked( + a_symbol_ptr member_set, + a_boolean is_move) +/* +member_set is the constructor or assignment operator symbol of a class +(possibly an overloaded function symbol). Return TRUE if member_set includes +user-provided copy (or, if is_move is TRUE, move) constructors or assignment +operators, but none of them is eligible (N5046 [special]/6) because each one +has unsatisfied constraints or is masked by a more constrained member of the +same kind. Such functions are never selected to copy (or move) a subobject +of the class, and so they don't make the corresponding special member of an +enclosing class nontrivial. For example: + template struct B { + B(B&&); + B(B&&) requires true = default; + }; + struct D : B {}; // D's move constructor is trivial. +*/ +{ + a_boolean any_masked = FALSE, any_eligible = FALSE, is_list; + a_symbol_ptr sym = member_set; + + if (sym != NULL && symbol_is(sym, sk_overloaded_function)) { + is_list = TRUE; + sym = sym->variant.overloaded_function.symbols; + } else { + is_list = FALSE; + } /* if */ + for (; !any_eligible && sym != NULL; sym = is_list ? sym->next : NULL) { + a_routine_ptr rp; + a_type_qualifier_set tqs = TQ_NONE; + a_boolean sym_is_move = FALSE; + if (!symbol_is(sym, sk_member_function)) continue; + rp = sym->variant.routine.ptr; + if (rp->compiler_generated || rp->is_defaulted || rp->is_deleted) continue; + if (special_kind_is(rp, sfk_constructor)) { + if (!is_copy_constructor(rp, (a_type_ptr)NULL, &tqs, + /*include_move_ctors=*/TRUE, + /*is_declarative_context=*/TRUE)) continue; + sym_is_move = copy_ctor_is_move_ctor(rp); + } else if (!routine_is_copy_or_move_assign_operator(rp, &tqs, + &sym_is_move)) { + continue; + } /* if */ + if (sym_is_move != is_move) continue; + if (is_ineligible(sym) || is_masked_special_member(sym, member_set)) { + any_masked = TRUE; + } else { + any_eligible = TRUE; + } /* if */ + } /* for */ + return any_masked && !any_eligible; +} /* user_provided_copy_functions_are_masked */ + + static void check_defaulted_members( a_type_ptr class_type, a_generated_special_function_descr *gsfd) @@ -25250,22 +25312,35 @@ The routine body is not generated until it is known to be needed. bitwise copying of the parent class without making the copy functions nontrivial. A class is trivially copyable if it has a trivial destructor, and it is bitwise copyable/assignable, except perhaps for deleted copy - functions. */ + functions. User-provided copy functions that are masked by more + constrained ones don't make the copy functions of enclosing classes + nontrivial. */ if (cssp->has_user_provided_copy_constructor) { - cssp->makes_copy_construction_nontrivial = TRUE; + if (!user_provided_copy_functions_are_masked(cssp->constructor, + /*is_move=*/FALSE)) { + cssp->makes_copy_construction_nontrivial = TRUE; + } /* if */ cssp->construction_by_bitwise_copy_allowed = FALSE; } /* if */ if (cssp->has_user_provided_move_constructor) { - cssp->makes_move_construction_nontrivial = TRUE; + if (!user_provided_copy_functions_are_masked(cssp->constructor, + /*is_move=*/TRUE)) { + cssp->makes_move_construction_nontrivial = TRUE; + } /* if */ cssp->construction_by_bitwise_copy_allowed = FALSE; } /* if */ - if (user_provided_copy_assignment_op) { + if (user_provided_copy_assignment_op && + !user_provided_copy_functions_are_masked(cssp->assignment_operator, + /*is_move=*/FALSE)) { cssp->makes_copy_assignment_nontrivial = TRUE; } /* if */ - if (cssp->has_user_provided_move_assign_operator) { + if (cssp->has_user_provided_move_assign_operator && + !user_provided_copy_functions_are_masked(cssp->assignment_operator, + /*is_move=*/TRUE)) { cssp->makes_move_assignment_nontrivial = TRUE; } /* if */ if (cssp->makes_copy_assignment_nontrivial || + user_provided_copy_assignment_op || cssp->has_user_provided_move_assign_operator) { cssp->assignment_by_bitwise_copy_allowed = FALSE; } /* if */ diff --git a/src/pending_changes/gh-628-masked-copy-functions-triviality.txt b/src/pending_changes/gh-628-masked-copy-functions-triviality.txt new file mode 100644 index 0000000000..35f4b6d2e9 --- /dev/null +++ b/src/pending_changes/gh-628-masked-copy-functions-triviality.txt @@ -0,0 +1,29 @@ +[GH #628] +Masked copy/move functions affecting trivial copyability + +A copy/move constructor or assignment operator that is masked by a more +constrained one of the same kind is not "eligible", and it should therefore +not affect whether the class is trivially copyable. However, two cases were +previously handled incorrectly. First, the masking check compared the +complete function types, so a difference in exception specifications kept a +function from being masked. Second, a masked user-provided function of a base +class or member made the corresponding implicit function of the enclosing +class nontrivial, even though it is not selected to copy or move the subobject. +For example, with --c++20: + + template struct S { + S(const S&) noexcept {} + S(const S&) requires true = default; + }; + static_assert(__is_trivially_copyable(S)); // Previously failed. + template struct B { + B(B&&); + B(B&&) requires true = default; + }; + struct D : B {}; + static_assert(__is_trivially_copyable(D)); // Previously failed. + +The GNU library's std::optional uses this pattern. Also, a copy/move +assignment operator whose constraints are not satisfied no longer keeps a +class from being trivially copyable, and the constraints of constructors that +are not copy/move constructors are no longer checked to determine that. diff --git a/src/types.c b/src/types.c index aef77f2fbb..7b7d5a5671 100644 --- a/src/types.c +++ b/src/types.c @@ -2242,6 +2242,100 @@ trivially copyable. } /* class_fields_are_trivially_copyable */ +static a_boolean special_members_are_of_same_kind(a_symbol_ptr sym1, + a_symbol_ptr sym2) +/* +sym1 and sym2 are member functions that are both copy/move constructors or +both copy/move assignment operators of the same class. Return TRUE if they +are of the same kind (N5046 [special]/5): they have the same first parameter +type and, for assignment operators, the same cv-qualifiers and ref-qualifier. +Unlike a comparison of the function types, this ignores return types and +exception specifications. +*/ +{ + a_type_ptr rtp1 = skip_typerefs(sym1->variant.routine.ptr->type), + rtp2 = skip_typerefs(sym2->variant.routine.ptr->type); + a_param_type_ptr ptp1 = first_nonobject_param(rtp1), + ptp2 = first_nonobject_param(rtp2); + a_boolean expl_this1 = has_explicit_this_parameter(rtp1), + expl_this2 = has_explicit_this_parameter(rtp2); + a_boolean result; + + if (ptp1 == NULL || ptp2 == NULL || + !identical_types(ptp1->type, ptp2->type)) { + result = FALSE; + } else if (expl_this1 || expl_this2) { + /* The types of explicit object parameters carry the cv-qualifiers and + ref-qualifier. */ + result = expl_this1 && expl_this2 && + identical_types(function_type_params(rtp1)->type, + function_type_params(rtp2)->type); + } else { + result = rout_type_supp(rtp1)->qualifiers == + rout_type_supp(rtp2)->qualifiers && + rout_type_supp(rtp1)->ref_qualifiers == + rout_type_supp(rtp2)->ref_qualifiers; + } /* if */ + return result; +} /* special_members_are_of_same_kind */ + + +a_boolean is_masked_special_member(a_symbol_ptr sym, + a_symbol_ptr member_set) +/* +sym is a copy/move constructor or a copy/move assignment operator of a class, +and member_set is the constructor or assignment operator symbol of that class +(possibly an overloaded function symbol). Return TRUE if sym is not eligible +(N5046 [special]/6) because member_set includes a special member function of +the same kind whose constraints are satisfied and that is more constrained +than sym. For example, in + template struct X { + X(X const&) noexcept; // (1) + X(X const&) requires true = default; + }; +copy constructor (1) of X is masked. +*/ +{ + a_boolean result = FALSE, is_list; + a_symbol_ptr fund_sym = fundamental_symbol_of(sym), other; + + if (member_set != NULL && symbol_is(member_set, sk_overloaded_function)) { + is_list = TRUE; + member_set = member_set->variant.overloaded_function.symbols; + } else { + is_list = FALSE; + } /* if */ + for (; !result && member_set != NULL; + member_set = is_list ? member_set->next : NULL) { + a_routine_ptr rp; + a_type_qualifier_set tqs; + a_boolean is_move; + other = fundamental_symbol_of(member_set); + if (other == fund_sym || !symbol_is(other, sk_member_function)) continue; + rp = other->variant.routine.ptr; + if (trailing_requires_clause(rp) == NULL) { + /* An unconstrained function is not more constrained than sym. */ + } else if (rp->is_deleted && + ((gpp_mode && !clang_mode) || microsoft_mode)) { + /* GCC and Microsoft don't let a deleted function mask another. */ + } else if (special_kind_is(rp, sfk_constructor) + ? !is_copy_constructor(rp, (a_type_ptr)NULL, + (a_type_qualifier_set*)NULL, + /*include_move_ctors=*/TRUE, + /*is_declarative_context=*/TRUE) + : !routine_is_copy_or_move_assign_operator(rp, &tqs, + &is_move)) { + /* Not a copy/move function. */ + } else if (special_members_are_of_same_kind(other, fund_sym) && + !(in_front_end && is_ineligible(other)) && + compare_constraints(other, fund_sym) > 0) { + result = TRUE; + } /* if */ + } /* for */ + return result; +} /* is_masked_special_member */ + + a_boolean is_trivially_copyable_type(a_type_ptr tp) /* Return TRUE if the given type is trivially copyable. @@ -2281,9 +2375,11 @@ Return TRUE if the given type is trivially copyable. !(tp->variant.class_struct_union.any_volatile_member && microsoft_mode && microsoft_version <= 1910)) { a_symbol_ptr sym; - a_boolean is_list, has_trivial_copy_function = FALSE; + a_boolean is_list, has_trivial_copy_function = FALSE, + has_nondeleted_trivial_copy_function = FALSE, + any_masked = FALSE; Small_dyn_array - trivials, nontrivials; + nontrivials; result = TRUE; /* Check for eligible nontrivial copy/move constructors. */ sym = cssp->constructor; @@ -2301,6 +2397,7 @@ Return TRUE if the given type is trivially copyable. have no constructors where construction by bitwise copy is allowed as if they have a trivial copy constructor. */ has_trivial_copy_function = TRUE; + has_nondeleted_trivial_copy_function = TRUE; } /* if */ /* Collect eligible trivial and nontrivial copy/move constructors. */ for (; sym != NULL; sym = is_list ? sym->next : NULL) { @@ -2309,13 +2406,19 @@ Return TRUE if the given type is trivially copyable. a_boolean one_param; if (symbol_is(sym, sk_function_template)) continue; check_assertion(symbol_is(sym, sk_member_function)); - if (in_front_end && is_ineligible(sym)) continue; + /* Only the constraints of copy/move constructors are checked: + Checking those of other constructors could cause unexpected + instantiations. */ rp = sym->variant.routine.ptr; if (rp->is_trivial_copy_function) { + if (in_front_end && is_ineligible(sym)) continue; /* Having a trivial copy function is necessary (but not sufficient) to make the type trivially copyable. */ has_trivial_copy_function = TRUE; - trivials.push_back(sym); + if (!rp->is_deleted && !has_nondeleted_trivial_copy_function && + !is_masked_special_member(sym, cssp->constructor)) { + has_nondeleted_trivial_copy_function = TRUE; + } /* if */ continue; } else if (rp->is_deleted && !ms_version_is(<1927)) { /* Deleted copy functions don't affect trivial copyability. */ @@ -2329,12 +2432,13 @@ Return TRUE if the given type is trivially copyable. if (one_param && is_copy_constructor(rp, tp, (a_type_qualifier_set*)NULL, /*include_move_ctors=*/TRUE, - /*is_declarative_context=*/TRUE)) { + /*is_declarative_context=*/TRUE) && + !(in_front_end && is_ineligible(sym))) { nontrivials.push_back(sym); } /* if */ } /* for */ - /* Check that any nontrivial candidate is less constrained than a - matching trivial candidate. For example: + /* Check that any nontrivial candidate is masked by a more constrained + candidate of the same kind. For example: template concept C = true; template concept D = C && true; template struct X { @@ -2345,27 +2449,13 @@ Return TRUE if the given type is trivially copyable. Copy constructor (1) is nontrivial, but the defaulted constructor is trivial and its constraint subsumes that of (1), thereby making (1) neutral in the determination of trivial copyability. */ - if (trivials.is_empty() && !nontrivials.is_empty()) { - result = FALSE; - goto done; - } else { - for (a_symbol_ptr nontrivial: nontrivials) { - a_boolean masked_nontrivial = FALSE; - for (a_symbol_ptr trivial: trivials) { - if (compare_constraints(trivial, nontrivial) > 0 && - identical_types(nontrivial->variant.routine.ptr->type, - trivial->variant.routine.ptr->type)) { - /* This nontrivial candidate is "masked" by a more constrained - trivial candidate. */ - masked_nontrivial = TRUE; - } /* if */ - } /* for */ - if (!masked_nontrivial) { - result = FALSE; - goto done; - } /* if */ - } /* for */ - } /* if */ + for (a_symbol_ptr nontrivial: nontrivials) { + if (!is_masked_special_member(nontrivial, cssp->constructor)) { + result = FALSE; + goto done; + } /* if */ + any_masked = TRUE; + } /* for */ if (result) { /* Now check for assignment operators. Unlike the copy/move constructor, there is currently no quick way to eliminate the @@ -2377,7 +2467,6 @@ Return TRUE if the given type is trivially copyable. } else { is_list = FALSE; } /* if */ - trivials.clear(); nontrivials.clear(); for (; sym != NULL; sym = is_list ? sym->next : NULL) { a_routine_ptr rp; @@ -2399,48 +2488,51 @@ Return TRUE if the given type is trivially copyable. continue; } /* if */ check_assertion(symbol_is(fund_sym, sk_member_function)); + /* As for constructors, only the constraints of copy/move + assignment operators are checked. */ rp = fund_sym->variant.routine.ptr; if (rp->is_trivial_copy_function) { + if (in_front_end && is_ineligible(fund_sym)) continue; /* Having a trivial copy function is necessary (but not sufficient) to make the type trivially copyable. */ has_trivial_copy_function = TRUE; - trivials.push_back(sym); + if (!rp->is_deleted && !has_nondeleted_trivial_copy_function && + !is_masked_special_member(sym, cssp->assignment_operator)) { + has_nondeleted_trivial_copy_function = TRUE; + } /* if */ continue; } else if (rp->is_deleted && !ms_version_is(<1927)) { /* Deleted copy functions don't affect trivial copyability. */ continue; } else if (routine_is_copy_or_move_assign_operator( - rp, &tqs, &is_move)) { + rp, &tqs, &is_move) && + !(in_front_end && is_ineligible(fund_sym))) { nontrivials.push_back(sym); - break; } /* if */ } /* for */ } /* if */ if (!has_trivial_copy_function) { result = FALSE; } /* if */ - /* Check that any nontrivial candidate is less constrained than a - matching trivial candidate. */ - if (trivials.is_empty() && !nontrivials.is_empty()) { + /* Check that any nontrivial candidate is masked by a more constrained + candidate of the same kind. */ + for (a_symbol_ptr nontrivial: nontrivials) { + if (!is_masked_special_member(nontrivial, + cssp->assignment_operator)) { + result = FALSE; + goto done; + } /* if */ + any_masked = TRUE; + } /* for */ + if (any_masked && !has_nondeleted_trivial_copy_function && + !clang_mode) { + /* Like GCC and Clang, a class whose copy/move functions are all + deleted is treated as trivially copyable if they are trivial. + But neither masked nor deleted functions are eligible, so if the + only nontrivial ones are masked, the class must have a nondeleted + trivial copy/move function that is not masked either (except for + Clang, which treats the masked functions like deleted ones). */ result = FALSE; - goto done; - } else { - for (a_symbol_ptr nontrivial: nontrivials) { - a_boolean masked_nontrivial = FALSE; - for (a_symbol_ptr trivial: trivials) { - if (compare_constraints(trivial, nontrivial) > 0 && - identical_types(nontrivial->variant.routine.ptr->type, - trivial->variant.routine.ptr->type)) { - /* This nontrivial candidate is "masked" by a more constrained - trivial candidate. */ - masked_nontrivial = TRUE; - } /* if */ - } /* for */ - if (!masked_nontrivial) { - result = FALSE; - goto done; - } /* if */ - } /* for */ } /* if */ if (result && clang_mode && tp->variant.class_struct_union.any_const_member) { diff --git a/src/types.h b/src/types.h index 3237a001a1..fcebd65ec6 100644 --- a/src/types.h +++ b/src/types.h @@ -357,6 +357,8 @@ extern a_boolean is_class_or_handle_to_class_type(a_type_ptr tp); extern a_boolean is_scalar_type(a_type_ptr tp); extern a_boolean is_simple_scalar_type(a_type_ptr tp); #if !STANDALONE_UTILITY_PROGRAM +extern a_boolean is_masked_special_member(a_symbol_ptr sym, + a_symbol_ptr member_set); extern a_boolean is_trivially_copyable_type(a_type_ptr tp); extern a_boolean is_trivially_copy_constructible_type(a_type_ptr tp); extern a_boolean is_const_default_constructible(a_type_ptr tp); diff --git a/tests/expectations/edg_x86_64/imported.log b/tests/expectations/edg_x86_64/imported.log index b11a3ac81b..955f40e2e0 100644 --- a/tests/expectations/edg_x86_64/imported.log +++ b/tests/expectations/edg_x86_64/imported.log @@ -7049,7 +7049,6 @@ gnu/cpp/cpp2a/concepts-ts6.sft.cpp:s:-DTEST_NUMBER=1 --c++20:SKIPPED: gnu/cpp/cpp2a/concepts-uneval2.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT: gnu/cpp/cpp2a/concepts-uneval4.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: gnu/cpp/cpp2a/concepts4.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: -gnu/cpp/cpp2a/cond-triv3.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: gnu/cpp/cpp2a/consteval-defarg3.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: gnu/cpp/cpp2a/consteval-memfn2.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: gnu/cpp/cpp2a/consteval-prop17.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:FAIL: diff --git a/tests/tests/imported/clang/cpp/SemaCXX/.constrained-special-member-functions.rto/default.1.1.txt b/tests/tests/imported/clang/cpp/SemaCXX/.constrained-special-member-functions.rto/default.1.1.txt index 2d6006a992..9a43a8608c 100644 --- a/tests/tests/imported/clang/cpp/SemaCXX/.constrained-special-member-functions.rto/default.1.1.txt +++ b/tests/tests/imported/clang/cpp/SemaCXX/.constrained-special-member-functions.rto/default.1.1.txt @@ -3,46 +3,6 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers static_assert(__is_trivial(DefaultConstructorChecker<0>)); ^ -"Test_name.c", line 84: error: static assertion failed - static_assert(__is_trivially_copyable(MoveAssignmentChecker<0>)); - ^ - -"Test_name.c", line 86: error: static assertion failed - static_assert(__is_trivially_copyable(MoveAssignmentChecker<1>)); - ^ - -"Test_name.c", line 89: error: static assertion failed - static_assert(__is_trivial(MoveAssignmentChecker<0>)); - ^ - -"Test_name.c", line 91: error: static assertion failed - static_assert(__is_trivial(MoveAssignmentChecker<1>)); - ^ - -"Test_name.c", line 103: error: static assertion failed - static_assert(__is_trivially_copyable(CopyAssignmentChecker<0>)); - ^ - -"Test_name.c", line 105: error: static assertion failed - static_assert(__is_trivially_copyable(CopyAssignmentChecker<1>)); - ^ - -"Test_name.c", line 108: error: static assertion failed - static_assert(__is_trivial(CopyAssignmentChecker<0>)); - ^ - -"Test_name.c", line 110: error: static assertion failed - static_assert(__is_trivial(CopyAssignmentChecker<1>)); - ^ - -"Test_name.c", line 140: error: static assertion failed - static_assert(__is_trivial(KindComparisonChecker3<0>)); - ^ - -"Test_name.c", line 141: error: static assertion failed - static_assert(__is_trivially_copyable(KindComparisonChecker3<0>)); - ^ - "Test_name.c", line 193: error: static assertion failed with "" static_assert(!__is_trivial(ComplexConstraints), ""); ^ @@ -75,4 +35,4 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers static Child c; ^ -16 errors detected in the compilation of "Test_name.c". +6 errors detected in the compilation of "Test_name.c". diff --git a/tests/tests/imported/gnu/cpp/cpp2a/.cond-triv3.rto/default.1.1.txt b/tests/tests/imported/gnu/cpp/cpp2a/.cond-triv3.rto/default.1.1.txt index 480654f30a..2ed4eaf390 100644 --- a/tests/tests/imported/gnu/cpp/cpp2a/.cond-triv3.rto/default.1.1.txt +++ b/tests/tests/imported/gnu/cpp/cpp2a/.cond-triv3.rto/default.1.1.txt @@ -1,6 +1 @@ fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics --c++20 Test_name.c -"Test_name.c", line 18: error: static assertion failed with "" - static_assert( __is_trivially_copyable(cxx20_pair), "" ); - ^ - -1 error detected in the compilation of "Test_name.c". diff --git a/tests/tests/regressions/.gh-628.rto/default.1.1.txt b/tests/tests/regressions/.gh-628.rto/default.1.1.txt new file mode 100644 index 0000000000..32fc82e916 --- /dev/null +++ b/tests/tests/regressions/.gh-628.rto/default.1.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=1 --c++20 Test_name.c diff --git a/tests/tests/regressions/.gh-628.rto/default.2.1.txt b/tests/tests/regressions/.gh-628.rto/default.2.1.txt new file mode 100644 index 0000000000..c74a2672cc --- /dev/null +++ b/tests/tests/regressions/.gh-628.rto/default.2.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=2 --c++20 --g++ Test_name.c diff --git a/tests/tests/regressions/.gh-628.rto/default.3.1.txt b/tests/tests/regressions/.gh-628.rto/default.3.1.txt new file mode 100644 index 0000000000..5b6931a233 --- /dev/null +++ b/tests/tests/regressions/.gh-628.rto/default.3.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=3 --c++20 --clang Test_name.c diff --git a/tests/tests/regressions/.gh-628.rto/default.4.1.txt b/tests/tests/regressions/.gh-628.rto/default.4.1.txt new file mode 100644 index 0000000000..bdb1684804 --- /dev/null +++ b/tests/tests/regressions/.gh-628.rto/default.4.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=4 --ms_c++20 Test_name.c diff --git a/tests/tests/regressions/gh-628.sft.cpp b/tests/tests/regressions/gh-628.sft.cpp new file mode 100644 index 0000000000..b4acaccd49 --- /dev/null +++ b/tests/tests/regressions/gh-628.sft.cpp @@ -0,0 +1,145 @@ +//type:fp +//options:--c++20:--c++20 --g++:--c++20 --clang:--ms_c++20 +//remark:copy/move functions masked by more constrained ones + +// A copy/move constructor or assignment operator that is masked by a more +// constrained one of the same kind is not eligible ([special]/6), so it +// doesn't affect trivial copyability, and it isn't selected to copy or move a +// subobject either. The pattern is used by libstdc++'s std::optional. + +template concept Triv = __is_trivially_constructible(T); +template concept Yes = true; +template concept No = Yes && false; + +// "Of the same kind" doesn't consider exception specifications. +template struct S { + S(const S&) noexcept(false) {} + S(const S&) requires true = default; +}; +static_assert(__is_trivially_copyable(S)); + +template struct SA { + SA& operator=(const SA&) noexcept(false) { return *this; } + SA& operator=(const SA&) requires true = default; +}; +static_assert(__is_trivially_copyable(SA)); + +// A masked user-provided function of a base or member doesn't make the +// copy/move functions of the enclosing class nontrivial. +template struct OB { + OB(OB&&); + OB(OB&&) requires Triv = default; +}; +static_assert(__is_trivially_copyable(OB)); +struct Opt : OB {}; +static_assert(__is_trivially_copyable(Opt)); +static_assert(__is_trivially_constructible(Opt, Opt&&)); +struct OptMem { OB m; }; +static_assert(__is_trivially_copyable(OptMem)); + +template struct AB { + AB& operator=(AB&&); + AB& operator=(AB&&) requires Triv = default; +}; +struct ADer : AB {}; +static_assert(__is_trivially_copyable(ADer)); +static_assert(__is_trivially_assignable(ADer&, ADer&&)); + +// Only the move constructor is masked here. +template struct MB { + MB(const MB&); + MB(MB&&); + MB(MB&&) requires true = default; +}; +struct MDer : MB {}; +static_assert(!__is_trivially_copyable(MDer)); +static_assert(__is_trivially_constructible(MDer, MDer&&)); +static_assert(!__is_trivially_constructible(MDer, const MDer&)); + +// A more constrained function whose constraints aren't satisfied doesn't mask. +struct NT { NT(NT&&); }; +static_assert(!__is_trivially_copyable(OB)); +struct NDer : OB {}; +static_assert(!__is_trivially_copyable(NDer)); +static_assert(!__is_trivially_constructible(NDer, NDer&&)); + +template struct N { + N(const N&) {} + N(const N&) requires false = default; +}; +static_assert(!__is_trivially_copyable(N)); +struct NDer2 : N {}; +static_assert(!__is_trivially_copyable(NDer2)); + +// A function whose constraints aren't satisfied is not eligible, even if it +// is more constrained than the others. +template struct UA { + UA& operator=(const UA&); + UA& operator=(const UA&) requires Yes = default; + UA& operator=(const UA&) requires No; + UA& operator=(UA&&) requires No; +}; +static_assert(__is_trivially_copyable(UA)); + +// Neither a deleted function nor one masked by it is eligible, so MD has no +// eligible copy/move function. GCC and Microsoft don't let a deleted +// function mask another, and Clang treats MD like a class whose copy/move +// functions are all deleted. +template struct MD { + MD(const MD&) {} + MD(const MD&) requires true = delete; + MD& operator=(const MD&) = delete; +}; +template struct MD2 { + MD2(const MD2&) {} + MD2(const MD2&) requires true = delete; + MD2& operator=(const MD2&) = default; +}; +#if defined(__clang__) +static_assert(__is_trivially_copyable(MD)); +static_assert(__is_trivially_copyable(MD2)); +#elif defined(__GNUC__) || defined(_MSC_VER) +static_assert(!__is_trivially_copyable(MD)); +static_assert(!__is_trivially_copyable(MD2)); +#else +static_assert(!__is_trivially_copyable(MD)); +static_assert(__is_trivially_copyable(MD2)); +#endif + +// The trivial copy constructor of MT is masked by the deleted one, so MT has +// no eligible copy/move function either (except in the GCC, Clang, and +// Microsoft modes, as with MD). +template struct MT { + MT(const MT&) {} + MT(const MT&) requires Yes = default; + MT(const MT&) requires Yes && true = delete; + MT& operator=(const MT&) = delete; +}; +#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) +static_assert(__is_trivially_copyable(MT)); +#else +static_assert(!__is_trivially_copyable(MT)); +#endif + +// The copy constructor of TB isn't eligible, so the constructor template is +// selected to copy the base class of TDer. +template struct TB { + TB(const TB&) requires No {} + template TB(const U&) {} +}; +struct TDer : TB {}; +static_assert(!__is_trivially_constructible(TB, const TB&)); +static_assert(!__is_trivially_constructible(TDer, const TDer&)); +static_assert(!__is_trivially_copyable(TDer)); + +// The constraints of constructors and assignment operators that aren't +// copy/move functions are not checked. +template struct Inst { + static_assert(sizeof(T) == 0); + static constexpr bool value = true; +}; +template struct UC { + UC(int) requires Inst::value; + UC& operator=(int) requires Inst::value; +}; +static_assert(__is_trivially_copyable(UC));