From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2100) id 94F3C386EC7A; Sat, 22 Aug 2020 22:50:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 94F3C386EC7A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1598136655; bh=l7bVvblr6xCl46r8IhPsGZbXMFgxz5TnVbxKKXjmt8w=; h=From:To:Subject:Date:From; b=nY+WRnCLpVYPDioL1ENnMbvGb+7xyloEHJZtfnM7KWBgBOPIZvn4xW0q8tG+WxOuX tumkfcLLrwaWuPhN9XwHCX/ZnaD+Rw+OyOsgW3rYn2bWhN2aFfi5XSJQX4KgSgobDX X7tjj1Lf89mGLBNGrhLbsvfKcmDBCC4esXWDXvDA= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Giuliano Belinassi To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/autopar_devel] c++: More P2002 operator<=> refinements. X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/devel/autopar_devel X-Git-Oldrev: 3a7eb528563647cde968ee310eee8fe08cb7ae0b X-Git-Newrev: db8a93d61142936da8126078bc3e95fb221e002c Message-Id: <20200822225055.94F3C386EC7A@sourceware.org> Date: Sat, 22 Aug 2020 22:50:55 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 22 Aug 2020 22:50:55 -0000 https://gcc.gnu.org/g:db8a93d61142936da8126078bc3e95fb221e002c commit db8a93d61142936da8126078bc3e95fb221e002c Author: Jason Merrill Date: Wed Jun 17 16:22:33 2020 -0400 c++: More P2002 operator<=> refinements. * Disallow && references. * Allow empty unions. * Improve diagnostics for a subobject comparison with non-comparison-category type. gcc/cp/ChangeLog: * method.c (early_check_defaulted_comparison): Check for &&. (build_comparison_op): Allow empty union. Diagnose non-category type. (common_comparison_type): Remove handling for non-category type. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/spaceship-ref1.C: New test. * g++.dg/cpp2a/spaceship-synth-neg4.C: New test. * g++.dg/cpp2a/spaceship-union1.C: New test. Diff: --- gcc/cp/method.c | 49 +++++++++++++++++------ gcc/testsuite/g++.dg/cpp2a/spaceship-ref1.C | 12 ++++++ gcc/testsuite/g++.dg/cpp2a/spaceship-synth-neg4.C | 20 +++++++++ gcc/testsuite/g++.dg/cpp2a/spaceship-union1.C | 12 ++++++ 4 files changed, 81 insertions(+), 12 deletions(-) diff --git a/gcc/cp/method.c b/gcc/cp/method.c index 1a819b29173..b23764b3d54 100644 --- a/gcc/cp/method.c +++ b/gcc/cp/method.c @@ -1134,6 +1134,11 @@ early_check_defaulted_comparison (tree fn) error_at (loc, "defaulted %qD must be %", fn); ok = false; } + if (mem && type_memfn_rqual (TREE_TYPE (fn)) == REF_QUAL_RVALUE) + { + error_at (loc, "defaulted %qD must not have %<&&%> ref-qualifier", fn); + ok = false; + } tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn); bool saw_byval = false; bool saw_byref = mem; @@ -1144,6 +1149,7 @@ early_check_defaulted_comparison (tree fn) if (same_type_p (parmtype, ctx)) saw_byval = true; else if (TREE_CODE (parmtype) != REFERENCE_TYPE + || TYPE_REF_IS_RVALUE (parmtype) || TYPE_QUALS (TREE_TYPE (parmtype)) != TYPE_QUAL_CONST || !(same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (parmtype), ctx))) @@ -1186,11 +1192,9 @@ common_comparison_type (vec &comps) tree comp = comps[i]; tree ctype = TREE_TYPE (comp); comp_cat_tag tag = cat_tag_for (ctype); - if (tag < cc_last) - seen[tag] = ctype; - else - /* If any Ti is not a comparison category type, U is void. */ - return void_type_node; + /* build_comparison_op already checked this. */ + gcc_checking_assert (tag < cc_last); + seen[tag] = ctype; } /* Otherwise, if at least one T i is std::partial_ordering, U is @@ -1312,8 +1316,9 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) iloc_sentinel ils (info.loc); /* A defaulted comparison operator function for class C is defined as - deleted if ... C is a union-like class. */ - if (TREE_CODE (ctype) == UNION_TYPE) + deleted if ... C has variant members. */ + if (TREE_CODE (ctype) == UNION_TYPE + && next_initializable_field (TYPE_FIELDS (ctype))) { if (complain & tf_error) inform (info.loc, "cannot default compare union %qT", ctype); @@ -1336,6 +1341,7 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) if (code == EQ_EXPR || code == SPACESHIP_EXPR) { + bool bad = false; auto_vec comps; /* Compare each of the subobjects. Note that we get bases from @@ -1348,21 +1354,22 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) /* A defaulted comparison operator function for class C is defined as deleted if any non-static data member of C is of reference type or - C is a union-like class. */ + C has variant members. */ if (TREE_CODE (expr_type) == REFERENCE_TYPE) { if (complain & tf_error) inform (DECL_SOURCE_LOCATION (field), "cannot default compare " "reference member %qD", field); - DECL_DELETED_FN (fndecl) = true; + bad = true; continue; } - else if (ANON_UNION_TYPE_P (expr_type)) + else if (ANON_UNION_TYPE_P (expr_type) + && next_initializable_field (TYPE_FIELDS (expr_type))) { if (complain & tf_error) inform (DECL_SOURCE_LOCATION (field), "cannot default compare " "anonymous union member"); - DECL_DELETED_FN (fndecl) = true; + bad = true; continue; } @@ -1374,7 +1381,19 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) NULL_TREE, NULL, complain); if (comp == error_mark_node) { - DECL_DELETED_FN (fndecl) = true; + bad = true; + continue; + } + if (code == SPACESHIP_EXPR + && cat_tag_for (TREE_TYPE (comp)) == cc_last) + { + /* The operator function is defined as deleted if ... Ri is not a + comparison category type. */ + if (complain & tf_error) + inform (DECL_SOURCE_LOCATION (field), + "three-way comparison of %qD has type %qT, not a " + "comparison category type", field, TREE_TYPE (comp)); + bad = true; continue; } comps.safe_push (comp); @@ -1384,6 +1403,11 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) rettype = common_comparison_type (comps); apply_deduced_return_type (fndecl, rettype); } + if (bad) + { + DECL_DELETED_FN (fndecl) = true; + goto out; + } for (unsigned i = 0; i < comps.length(); ++i) { tree comp = comps[i]; @@ -1468,6 +1492,7 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) finish_return_stmt (comp2); } + out: if (info.defining) finish_compound_stmt (compound_stmt); else diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-ref1.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-ref1.C new file mode 100644 index 00000000000..5f888dfc3b4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-ref1.C @@ -0,0 +1,12 @@ +// Reject &&. +// { dg-do compile { target c++20 } } + +struct A +{ + bool operator==(const A&) const && = default; // { dg-error "ref-qualifier" } +}; + +struct B +{ + friend bool operator==(const B&&, const B&&) = default; // { dg-error "" } +}; diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-synth-neg4.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth-neg4.C new file mode 100644 index 00000000000..e99aa0a2041 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth-neg4.C @@ -0,0 +1,20 @@ +// P2002: deleted if a subobject <=> has non-category type. +// { dg-do compile { target c++20 } } + +#include + +struct A +{ + bool operator<=>(const A&) const; +}; + +struct B +{ + A a; // { dg-message "bool" } + auto operator<=>(const B&) const = default; +}; + +int main() +{ + auto x = B() <=> B(); // { dg-error "deleted" } +} diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-union1.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-union1.C new file mode 100644 index 00000000000..a08ba0de69c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-union1.C @@ -0,0 +1,12 @@ +// P2002: Allow default comparison of unions with no members. +// { dg-do compile { target c++20 } } + +union A +{ + bool operator==(const A&) const = default; +}; + +int main() +{ + A() == A(); +}