diff options
author | Jason Merrill <jason@redhat.com> | 2020-06-17 16:22:33 -0400 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2020-06-18 16:00:30 -0400 |
commit | b56dc0fc6cc39fba9ed974c58a21cf5f7a264be1 (patch) | |
tree | 4c1ae7089617958e3bb3754a8babd5cb10af711b /gcc/cp/method.c | |
parent | e54353a72a0fd780a2f233a057f3f8121b476192 (diff) | |
download | gcc-b56dc0fc6cc39fba9ed974c58a21cf5f7a264be1.zip gcc-b56dc0fc6cc39fba9ed974c58a21cf5f7a264be1.tar.gz gcc-b56dc0fc6cc39fba9ed974c58a21cf5f7a264be1.tar.bz2 |
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.
Diffstat (limited to 'gcc/cp/method.c')
-rw-r--r-- | gcc/cp/method.c | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/gcc/cp/method.c b/gcc/cp/method.c index 1a819b2..b23764b 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 %<const%>", 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<tree> &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<tree> 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 |