public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: Patrick Palka <ppalka@redhat.com>, gcc-patches@gcc.gnu.org
Subject: [PATCH, v2] c++: Fix up synthetization of defaulted comparison operators on classes with bitfields [PR102490]
Date: Tue, 28 Sep 2021 22:34:39 +0200	[thread overview]
Message-ID: <20210928203439.GG304296@tucnak> (raw)
In-Reply-To: <38a8c792-f599-5802-ffe3-a43eaee81479@redhat.com>

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.

2021-09-28  Jakub Jelinek  <jakub@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	other threads:[~2021-09-28 20:34 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-28  9:24 [PATCH] " Jakub Jelinek
2021-09-28 13:49 ` Patrick Palka
2021-09-28 13:53   ` Patrick Palka
2021-09-28 19:33     ` Jason Merrill
2021-09-28 20:34       ` Jakub Jelinek [this message]
2021-09-29  8:07         ` [PATCH, v2] " Jakub Jelinek
2021-09-29 18:14         ` Jason Merrill
2021-09-29 19:21           ` Jakub Jelinek
2021-09-29 19:38             ` Jason Merrill
2021-09-30 17:24               ` [PATCH, v3] " Jakub Jelinek
2021-09-30 19:01                 ` Jason Merrill
2021-10-01 15:07                   ` [PATCH, v4] " Jakub Jelinek
2021-10-06  2:40                     ` [PATCH, v5] " Jason Merrill
2021-10-06  9:06                       ` Jakub Jelinek
2021-10-06 21:13                         ` Jason Merrill
2021-09-28 13:56   ` [PATCH] " Jakub Jelinek
2021-09-28 16:44     ` Patrick Palka
2021-09-28 16:49       ` Jakub Jelinek
2021-09-28 16:55         ` Jakub Jelinek
2021-09-28 17:25           ` Patrick Palka
2021-09-28 18:04             ` Jakub Jelinek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210928203439.GG304296@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=ppalka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).