c++: defaulted comparisons and vptr fields [PR95567]

Message ID 20210930140329.4062169-1-ppalka@redhat.com
State Committed
Commit b6bca2e631b54f992c058ca8e445b45e9816690b
Headers
Series c++: defaulted comparisons and vptr fields [PR95567] |

Commit Message

Patrick Palka Sept. 30, 2021, 2:03 p.m. UTC
  We need to skip over vptr fields when synthesizing a defaulted
comparison operator.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk/11?

	PR c++/95567

gcc/cp/ChangeLog:

	* method.c (build_comparison_op): Skip DECL_VIRTUAL_P fields.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/spaceship-virtual1.C: New test.
---
 gcc/cp/method.c                               |  4 ++++
 .../g++.dg/cpp2a/spaceship-virtual1.C         | 20 +++++++++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-virtual1.C
  

Comments

Jason Merrill Sept. 30, 2021, 4:45 p.m. UTC | #1
On 9/30/21 10:03, Patrick Palka wrote:
> We need to skip over vptr fields when synthesizing a defaulted
> comparison operator.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk/11?

OK.

> 	PR c++/95567
> 
> gcc/cp/ChangeLog:
> 
> 	* method.c (build_comparison_op): Skip DECL_VIRTUAL_P fields.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/spaceship-virtual1.C: New test.
> ---
>   gcc/cp/method.c                               |  4 ++++
>   .../g++.dg/cpp2a/spaceship-virtual1.C         | 20 +++++++++++++++++++
>   2 files changed, 24 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-virtual1.C
> 
> diff --git a/gcc/cp/method.c b/gcc/cp/method.c
> index 32f7186a774..3c3495227ce 100644
> --- a/gcc/cp/method.c
> +++ b/gcc/cp/method.c
> @@ -1426,6 +1426,10 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain)
>   	   field;
>   	   field = next_initializable_field (DECL_CHAIN (field)))
>   	{
> +	  if (DECL_VIRTUAL_P (field))
> +	    /* Don't compare vptr fields.  */
> +	    continue;
> +
>   	  tree expr_type = TREE_TYPE (field);
>   
>   	  location_t field_loc = DECL_SOURCE_LOCATION (field);
> diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-virtual1.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-virtual1.C
> new file mode 100644
> index 00000000000..8067d3cd9d1
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-virtual1.C
> @@ -0,0 +1,20 @@
> +// PR c++/95567
> +// { dg-do run { target c++20 } }
> +
> +struct B {
> +  B(int i) : i(i) {}
> +  virtual ~B() = default;
> +
> +  bool operator==(B const&) const = default;
> +  int i;
> +};
> +
> +struct D : B {
> +  D(int i, int j) : B(i), j(j) {}
> +  int j;
> +};
> +
> +int main() {
> +  if (B(2) != D(2, 3))
> +    __builtin_abort();
> +}
>
  

Patch

diff --git a/gcc/cp/method.c b/gcc/cp/method.c
index 32f7186a774..3c3495227ce 100644
--- a/gcc/cp/method.c
+++ b/gcc/cp/method.c
@@ -1426,6 +1426,10 @@  build_comparison_op (tree fndecl, tsubst_flags_t complain)
 	   field;
 	   field = next_initializable_field (DECL_CHAIN (field)))
 	{
+	  if (DECL_VIRTUAL_P (field))
+	    /* Don't compare vptr fields.  */
+	    continue;
+
 	  tree expr_type = TREE_TYPE (field);
 
 	  location_t field_loc = DECL_SOURCE_LOCATION (field);
diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-virtual1.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-virtual1.C
new file mode 100644
index 00000000000..8067d3cd9d1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-virtual1.C
@@ -0,0 +1,20 @@ 
+// PR c++/95567
+// { dg-do run { target c++20 } }
+
+struct B {
+  B(int i) : i(i) {}
+  virtual ~B() = default;
+
+  bool operator==(B const&) const = default;
+  int i;
+};
+
+struct D : B {
+  D(int i, int j) : B(i), j(j) {}
+  int j;
+};
+
+int main() {
+  if (B(2) != D(2, 3))
+    __builtin_abort();
+}