From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 0BF6E3858C55; Thu, 29 Sep 2022 20:11:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0BF6E3858C55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664482302; bh=Cbps8euI46Mh/INSridnjWTnIL6efB4so1gbZQLBlwQ=; h=From:To:Subject:Date:From; b=BQNWWSkoU9MeZI3cXotpXPPOYLi+bE9uXPaRq0C1/3ffkRBrGINz6/KGX7o8nSQH3 Xt0SA5M+i8r43MZ1i2OdEPB5976HZ+BTM5qGRa9AvrZ+COlB20Nw6i1rv3iifVV4af N+pYQayIpsKfpEbfq+6G2NsFuqiTEFJhlS0/BXYk= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-8799] c++: fix triviality of class with unsatisfied op= X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 2773a90c0f4f323e8ace0593893bc8fcbd2266cf X-Git-Newrev: aeba3e009b0abfccaf01797556445dbf891cc8dc Message-Id: <20220929201142.0BF6E3858C55@sourceware.org> Date: Thu, 29 Sep 2022 20:11:42 +0000 (GMT) List-Id: https://gcc.gnu.org/g:aeba3e009b0abfccaf01797556445dbf891cc8dc commit r12-8799-gaeba3e009b0abfccaf01797556445dbf891cc8dc Author: Jason Merrill Date: Thu Sep 29 13:45:02 2022 -0400 c++: fix triviality of class with unsatisfied op= cxx20_pair is trivially copyable because it has a trivial copy constructor and only a deleted copy assignment operator; the non-triviality of the unsatisfied copy assignment overload is not considered. gcc/cp/ChangeLog: * class.cc (check_methods): Call constraints_satisfied_p. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/cond-triv3.C: New test. Diff: --- gcc/cp/class.cc | 13 ++++++++-- gcc/testsuite/g++.dg/cpp2a/cond-triv3.C | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc index bfda0065bf4..b9e0a77f52b 100644 --- a/gcc/cp/class.cc +++ b/gcc/cp/class.cc @@ -4780,8 +4780,9 @@ check_methods (tree t) /* Check whether the eligible special member functions (P0848) are user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only - has the eligible ones; TYPE_FIELDS also contains ineligible overloads, - which is why this needs to be separate from the loop above. */ + has the eligible ones, unless none are eligible; TYPE_FIELDS also contains + ineligible overloads, which is why this needs to be separate from the loop + above. */ if (tree dtor = CLASSTYPE_DESTRUCTOR (t)) { @@ -4804,6 +4805,10 @@ check_methods (tree t) { if (!user_provided_p (fn)) /* Might be trivial. */; + else if (TREE_CODE (fn) == TEMPLATE_DECL) + /* Templates are never special members. */; + else if (!constraints_satisfied_p (fn)) + /* Not eligible. */; else if (copy_fn_p (fn)) TYPE_HAS_COMPLEX_COPY_CTOR (t) = true; else if (move_fn_p (fn)) @@ -4814,6 +4819,10 @@ check_methods (tree t) { if (!user_provided_p (fn)) /* Might be trivial. */; + else if (TREE_CODE (fn) == TEMPLATE_DECL) + /* Templates are never special members. */; + else if (!constraints_satisfied_p (fn)) + /* Not eligible. */; else if (copy_fn_p (fn)) TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true; else if (move_fn_p (fn)) diff --git a/gcc/testsuite/g++.dg/cpp2a/cond-triv3.C b/gcc/testsuite/g++.dg/cpp2a/cond-triv3.C new file mode 100644 index 00000000000..d0711cf2607 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/cond-triv3.C @@ -0,0 +1,44 @@ +// { dg-do compile { target c++20 } } + +template +struct X +{ + T first{}; + + X& operator=(const X&) = delete; + X& operator=(const X&) requires requires (T& t) { t = t; } { return *this; } +}; + +// C++20 std::pair: +using cxx20_pair = X; +static_assert( __is_trivially_constructible(cxx20_pair, const cxx20_pair&), "" ); +static_assert( !__is_assignable(cxx20_pair&, const cxx20_pair&), "" ); +static_assert( __is_trivially_copyable(cxx20_pair), "" ); + +template struct conditional { using type = F; }; +template struct conditional { using type = T; }; + +struct base +{ + base() = default; + ~base() = default; + base(const base&) = default; + base& operator=(const base&) = delete; +}; + +struct nope; + +template +struct Y : base +{ + T first{}; + + Y& operator=(typename conditional<__is_assignable(T&, const T&), const Y&, const nope&>::type) + { return *this; } +}; + +// C++17 std::pair: +using cxx17_pair = Y; +static_assert( __is_trivially_constructible(cxx17_pair, const cxx17_pair&), "" ); +static_assert( ! __is_assignable(cxx17_pair&, const cxx17_pair&), "" ); +static_assert( __is_trivially_copyable(cxx17_pair), "???" );