]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: defaulted comparisons and vptr fields [PR95567]
authorPatrick Palka <ppalka@redhat.com>
Thu, 30 Sep 2021 21:29:05 +0000 (17:29 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 30 Sep 2021 21:29:05 +0000 (17:29 -0400)
We need to explicitly skip over vptr fields when synthesizing a
defaulted comparison operator, because next_initializable_field
doesn't do so for us.

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
gcc/testsuite/g++.dg/cpp2a/spaceship-virtual1.C [new file with mode: 0644]

index 32f7186a7749931b48f38f471a758329a86bc0b5..3c3495227ce36ae3efc2ce345adc35102e00a413 100644 (file)
@@ -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 (file)
index 0000000..8067d3c
--- /dev/null
@@ -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();
+}