On 9/28/21 16:34, Jakub Jelinek wrote:
On Tue, Sep 28, 2021 at 03:33:35PM -0400, Jason Merrill wrote:
According to the function comment for defaulted_late_check, won't
COMPLETE_TYPE_P (ctx) always be false here?

Not for a function defaulted outside the class.

If so, I wonder if we could get away with moving this entire fragment
from defaulted_late_check to finish_struct_1 instead of calling
defaulted_late_check from finish_struct_1.

The comment in check_bases_and_members says that we call it there so that
it's before we clone [cd]tors.  Probably better to leave the call there for
other functions, just skip it for comparisons.

So like this instead then?  Just tested with dg.exp=*spaceship* so far.

I was thinking to keep the checking/synthesis in defaulted_late_check, just change when it's called.

That is, not to change defaulted_late_check at all, to keep the finish_struct_1 change you had in your first patch, and to change check_bases_and_members to not call defaulted_late_check for comparisons.

We probably also want to assert COMPLETE_TYPE_P in build_comparison_op, at least when info.defining.

BTW, the word is "synthesize".

2021-09-28  Jakub Jelinek  <ja...@redhat.com>

        PR c++/102490
        * method.c (defaulted_late_check): Don't synthetize constexpr
        defaulted comparisons.
        (finish_struct_1): Synthetize constexpr defaulted comparisons here
        after layout_class_type.

        * g++.dg/cpp2a/spaceship-eq11.C: New test.
        * g++.dg/cpp2a/spaceship-eq12.C: New test.

--- gcc/cp/method.c.jj  2021-09-28 11:34:10.165412477 +0200
+++ gcc/cp/method.c     2021-09-28 22:28:23.637981709 +0200
@@ -3158,18 +3158,7 @@ defaulted_late_check (tree fn)
    special_function_kind kind = special_function_p (fn);
if (kind == sfk_comparison)
-    {
-      /* If the function was declared constexpr, check that the definition
-        qualifies.  Otherwise we can define the function lazily.  */
-      if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
-       {
-         /* Prevent GC.  */
-         function_depth++;
-         synthesize_method (fn);
-         function_depth--;
-       }
-      return;
-    }
+    return;
bool fn_const_p = (copy_fn_p (fn) == 2);
    tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
--- gcc/cp/class.c.jj   2021-09-28 11:34:10.096413431 +0200
+++ gcc/cp/class.c      2021-09-28 22:29:59.072669058 +0200
@@ -7467,7 +7467,21 @@ finish_struct_1 (tree t)
       for any static member objects of the type we're working on.  */
    for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
      if (DECL_DECLARES_FUNCTION_P (x))
-      DECL_IN_AGGR_P (x) = false;
+      {
+       /* Synthetize constexpr defaulted comparisons.  */
+       if (!DECL_ARTIFICIAL (x)
+           && DECL_DEFAULTED_IN_CLASS_P (x)
+           && special_function_p (x) == sfk_comparison
+           && DECL_DECLARED_CONSTEXPR_P (x)
+           && !DECL_INITIAL (x))
+         {
+           /* Prevent GC.  */
+           function_depth++;
+           synthesize_method (x);
+           function_depth--;
+         }
+       DECL_IN_AGGR_P (x) = false;
+      }
      else if (VAR_P (x) && TREE_STATIC (x)
             && TREE_TYPE (x) != error_mark_node
             && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
--- gcc/testsuite/g++.dg/cpp2a/spaceship-eq11.C.jj      2021-09-28 
22:27:40.524574708 +0200
+++ gcc/testsuite/g++.dg/cpp2a/spaceship-eq11.C 2021-09-28 22:27:40.524574708 
+0200
@@ -0,0 +1,43 @@
+// PR c++/102490
+// { dg-do run { target c++20 } }
+
+struct A
+{
+  unsigned char a : 1;
+  unsigned char b : 1;
+  constexpr bool operator== (const A &) const = default;
+};
+
+struct B
+{
+  unsigned char a : 8;
+  int : 0;
+  unsigned char b : 7;
+  constexpr bool operator== (const B &) const = default;
+};
+
+struct C
+{
+  unsigned char a : 3;
+  unsigned char b : 1;
+  constexpr bool operator== (const C &) const = default;
+};
+
+void
+foo (C &x, int y)
+{
+  x.b = y;
+}
+
+int
+main ()
+{
+  A a{}, b{};
+  B c{}, d{};
+  C e{}, f{};
+  a.b = 1;
+  d.b = 1;
+  foo (e, 0);
+  foo (f, 1);
+  return a == b || c == d || e == f;
+}
--- gcc/testsuite/g++.dg/cpp2a/spaceship-eq12.C.jj      2021-09-28 
22:27:40.524574708 +0200
+++ gcc/testsuite/g++.dg/cpp2a/spaceship-eq12.C 2021-09-28 22:27:40.524574708 
+0200
@@ -0,0 +1,5 @@
+// PR c++/102490
+// { dg-do run { target c++20 } }
+// { dg-options "-O2" }
+
+#include "spaceship-eq11.C"


        Jakub


Reply via email to