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.
This commit is contained in:
Jason Merrill 2020-06-17 16:22:33 -04:00
parent e54353a72a
commit b56dc0fc6c
4 changed files with 81 additions and 12 deletions

View File

@ -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

View File

@ -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 "" }
};

View File

@ -0,0 +1,20 @@
// P2002: deleted if a subobject <=> has non-category type.
// { dg-do compile { target c++20 } }
#include <compare>
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" }
}

View File

@ -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();
}