public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: Patrick Palka <ppalka@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: conditional noexcept-spec on defaulted comparison op [PR96242]
Date: Fri, 4 Feb 2022 12:04:04 -0500 (EST)	[thread overview]
Message-ID: <018e7a37-8bf9-9b9d-d6fe-9735263ba086@idea> (raw)
In-Reply-To: <b382af1e-b562-fc58-5f90-a14d8c3b22be@redhat.com>

On Thu, 3 Feb 2022, Jason Merrill wrote:

> On 2/3/22 16:06, Patrick Palka wrote:
> > On Thu, 3 Feb 2022, Jason Merrill wrote:
> > 
> > > On 2/3/22 14:58, Patrick Palka wrote:
> > > > When synthesizing a defaulted comparison op from
> > > > maybe_instantiate_noexcept, we seem to be forgetting to instantiate the
> > > > noexcept-spec afterwards.
> > > 
> > > Hmm, there shouldn't be any need to instantiate the noexcept-spec
> > > afterwards,
> > > it should have been set by ~comp_info.
> > 
> > It appears the comp_info class sets the noexcept-spec only if the
> > comparison function hasn't been declared with an explicit noexcept-spec.
> > Otherwise the class doesn't touch the noexcept-spec, and it remains a
> > DEFERRED_NOEXCEPT with non-NULL DEFERRED_NOEXCEPT_PATTERN.
> 
> Ah, I see.  So perhaps we should entirely skip the current DECL_MAYBE_DELETED
> handling in maybe_instantiate_noexcept if we have DEFERRED_NOEXCEPT with
> non-null DEFERRED_NOEXCEPT_PATTERN (which seems to want another macro)?

Hmm, I tried something to that effect but it looks like mark_used relies
solely on the DECL_MAYBE_DELETED handling in maybe_instantiate_noexcept
to determine deletedness of a defaulted comparison operator (via trying
to synthesize it).  So by sometimes sidestepping this handling, we end
up failing to diagnose the use of the deleted defaulted <=> in e.g.:

  #include <compare>

  struct A { };

  template<bool B>
  struct X {
    auto operator<=>(const X&) const noexcept(B) = default;
    A a;
  };

  X<true> x_t;
  auto c = x_t <=> x_t; // should be error: use of deleted <=> b/c A lacks <=>

In light of this, I suppose mark_used should directly perform
DECL_MAYBE_DELETED synthesization of its own?

And it looks like DECL_MAYBE_DELETED is always false after doing
maybe_synthesize_method, so I think maybe_instantiate_noexcept should
return !DECL_DELETED_FN instead of !DECL_MAYBE_DELETED after synthesization.

How does this look?  Lightly tested so far, bootstrap and regtesting in progress.

-- >8 --

	PR c++/96242

gcc/cp/ChangeLog:

	* decl2.cc (mark_used): Directly synthesize a DECL_MAYBE_DELETED
	fn by calling maybe_synthesize_method instead of relying on
	maybe_instantiate_noexcept.
	* pt.cc (maybe_instantiate_noexcept): Restrict DECL_MAYBE_DELETED
	synthesization to only fns with an implicit noexcept-spec, and
	return !DECL_DELETED_FN instead of !DECL_MAYBE_DELETED afteward.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/spaceship-synth15.C: New test.
---
 gcc/cp/decl2.cc                               | 17 ++++++++++----
 gcc/cp/pt.cc                                  | 11 +++++-----
 .../g++.dg/cpp2a/spaceship-synth15.C          | 22 +++++++++++++++++++
 3 files changed, 41 insertions(+), 9 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-synth15.C

diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index a2aa5f1de4e..4d3798d02fe 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -5772,10 +5772,19 @@ mark_used (tree decl, tsubst_flags_t complain)
   if (TREE_CODE (decl) == CONST_DECL)
     used_types_insert (DECL_CONTEXT (decl));
 
-  if (TREE_CODE (decl) == FUNCTION_DECL
-      && !DECL_DELETED_FN (decl)
-      && !maybe_instantiate_noexcept (decl, complain))
-    return false;
+  if (TREE_CODE (decl) == FUNCTION_DECL)
+    {
+      if (DECL_MAYBE_DELETED (decl))
+	{
+	  ++function_depth;
+	  maybe_synthesize_method (decl);
+	  --function_depth;
+	}
+
+      if (!DECL_DELETED_FN (decl)
+	  && !maybe_instantiate_noexcept (decl, complain))
+	return false;
+    }
 
   if (TREE_CODE (decl) == FUNCTION_DECL
       && DECL_DELETED_FN (decl))
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index d219bba6ac1..584c752529b 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -25982,7 +25982,11 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
       && (!flag_noexcept_type || type_dependent_expression_p (fn)))
     return true;
 
-  if (DECL_MAYBE_DELETED (fn))
+  tree fntype = TREE_TYPE (fn);
+  tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
+
+  if (DECL_MAYBE_DELETED (fn)
+      && (!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec)))
     {
       if (fn == current_function_decl)
 	/* We're in start_preparsed_function, keep going.  */
@@ -25991,12 +25995,9 @@ maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
       ++function_depth;
       maybe_synthesize_method (fn);
       --function_depth;
-      return !DECL_MAYBE_DELETED (fn);
+      return !DECL_DELETED_FN (fn);
     }
 
-  tree fntype = TREE_TYPE (fn);
-  tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
-
   if (!spec || !TREE_PURPOSE (spec))
     return true;
 
diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-synth15.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth15.C
new file mode 100644
index 00000000000..00ea6c10474
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth15.C
@@ -0,0 +1,22 @@
+// PR c++/96242
+// { dg-do compile { target c++20 } }
+
+#include <compare>
+
+template<bool B>
+struct X {
+  auto operator<=>(const X&) const noexcept(B) = default;
+  bool operator==(const X&) const noexcept(!B) = default;
+};
+
+X<true> x_t;
+static_assert(noexcept(x_t <=> x_t));
+static_assert(noexcept(x_t < x_t));
+static_assert(!noexcept(x_t == x_t));
+static_assert(!noexcept(x_t != x_t));
+
+X<false> x_f;
+static_assert(!noexcept(x_f <=> x_f));
+static_assert(!noexcept(x_f < x_f));
+static_assert(noexcept(x_f == x_f));
+static_assert(noexcept(x_f != x_f));
-- 
2.35.0


  reply	other threads:[~2022-02-04 17:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-03 19:58 Patrick Palka
2022-02-03 20:26 ` Jason Merrill
2022-02-03 21:06   ` Patrick Palka
2022-02-03 21:38     ` Jason Merrill
2022-02-04 17:04       ` Patrick Palka [this message]
2022-02-04 22:00         ` Jason Merrill

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=018e7a37-8bf9-9b9d-d6fe-9735263ba086@idea \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@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).