diff mbox series

c++: local class memfn synth from uneval context [PR113063]

Message ID 20231218195013.1241371-1-ppalka@redhat.com
State New
Headers show
Series c++: local class memfn synth from uneval context [PR113063] | expand

Commit Message

Patrick Palka Dec. 18, 2023, 7:50 p.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

-- >8 --

Here we first use and therefore synthesize the local class operator<=>
from an unevaluated context, which inadvertently affects synthesization
by causing functions used within the definition (such as the copy
constructor of std::strong_ordering) to not get marked as odr-used.

This patch fixes this by using maybe_push_to_top_level in synthesize_method
which ensures that cp_unevaluated_operand is cleared even in the
function-local case.

	PR c++/113063

gcc/cp/ChangeLog:

	* method.cc (synthesize_method): Use maybe_push_to_top_level
	and maybe_pop_from_top_level.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/spaceship-synth16.C: New test.
---
 gcc/cp/method.cc                               | 12 ++----------
 gcc/testsuite/g++.dg/cpp2a/spaceship-synth16.C | 13 +++++++++++++
 2 files changed, 15 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-synth16.C

Comments

Jason Merrill Dec. 18, 2023, 9:48 p.m. UTC | #1
On 12/18/23 14:50, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?

OK.

> -- >8 --
> 
> Here we first use and therefore synthesize the local class operator<=>
> from an unevaluated context, which inadvertently affects synthesization
> by causing functions used within the definition (such as the copy
> constructor of std::strong_ordering) to not get marked as odr-used.
> 
> This patch fixes this by using maybe_push_to_top_level in synthesize_method
> which ensures that cp_unevaluated_operand is cleared even in the
> function-local case.
> 
> 	PR c++/113063
> 
> gcc/cp/ChangeLog:
> 
> 	* method.cc (synthesize_method): Use maybe_push_to_top_level
> 	and maybe_pop_from_top_level.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/spaceship-synth16.C: New test.
> ---
>   gcc/cp/method.cc                               | 12 ++----------
>   gcc/testsuite/g++.dg/cpp2a/spaceship-synth16.C | 13 +++++++++++++
>   2 files changed, 15 insertions(+), 10 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-synth16.C
> 
> diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
> index 26e6eb79946..f645066077f 100644
> --- a/gcc/cp/method.cc
> +++ b/gcc/cp/method.cc
> @@ -1770,8 +1770,6 @@ decl_remember_implicit_trigger_p (tree decl)
>   void
>   synthesize_method (tree fndecl)
>   {
> -  bool nested = (current_function_decl != NULL_TREE);
> -  tree context = decl_function_context (fndecl);
>     bool need_body = true;
>     tree stmt;
>     location_t save_input_location = input_location;
> @@ -1795,10 +1793,7 @@ synthesize_method (tree fndecl)
>        it now.  */
>     push_deferring_access_checks (dk_no_deferred);
>   
> -  if (! context)
> -    push_to_top_level ();
> -  else if (nested)
> -    push_function_context ();
> +  bool push_to_top = maybe_push_to_top_level (fndecl);
>   
>     input_location = DECL_SOURCE_LOCATION (fndecl);
>   
> @@ -1843,10 +1838,7 @@ synthesize_method (tree fndecl)
>   
>     input_location = save_input_location;
>   
> -  if (! context)
> -    pop_from_top_level ();
> -  else if (nested)
> -    pop_function_context ();
> +  maybe_pop_from_top_level (push_to_top);
>   
>     pop_deferring_access_checks ();
>   
> diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-synth16.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth16.C
> new file mode 100644
> index 00000000000..8e59f7d2e46
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth16.C
> @@ -0,0 +1,13 @@
> +// PR c++/113063
> +// { dg-do link { target c++20 } }
> +
> +#include <compare>
> +
> +int main() {
> +  struct X {
> +    auto operator<=>(const X&) const = default;
> +  };
> +  X x;
> +  static_assert(noexcept(x <=> x));
> +  static_cast<void>(x <=> x);
> +}
diff mbox series

Patch

diff --git a/gcc/cp/method.cc b/gcc/cp/method.cc
index 26e6eb79946..f645066077f 100644
--- a/gcc/cp/method.cc
+++ b/gcc/cp/method.cc
@@ -1770,8 +1770,6 @@  decl_remember_implicit_trigger_p (tree decl)
 void
 synthesize_method (tree fndecl)
 {
-  bool nested = (current_function_decl != NULL_TREE);
-  tree context = decl_function_context (fndecl);
   bool need_body = true;
   tree stmt;
   location_t save_input_location = input_location;
@@ -1795,10 +1793,7 @@  synthesize_method (tree fndecl)
      it now.  */
   push_deferring_access_checks (dk_no_deferred);
 
-  if (! context)
-    push_to_top_level ();
-  else if (nested)
-    push_function_context ();
+  bool push_to_top = maybe_push_to_top_level (fndecl);
 
   input_location = DECL_SOURCE_LOCATION (fndecl);
 
@@ -1843,10 +1838,7 @@  synthesize_method (tree fndecl)
 
   input_location = save_input_location;
 
-  if (! context)
-    pop_from_top_level ();
-  else if (nested)
-    pop_function_context ();
+  maybe_pop_from_top_level (push_to_top);
 
   pop_deferring_access_checks ();
 
diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-synth16.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth16.C
new file mode 100644
index 00000000000..8e59f7d2e46
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth16.C
@@ -0,0 +1,13 @@ 
+// PR c++/113063
+// { dg-do link { target c++20 } }
+
+#include <compare>
+
+int main() {
+  struct X {
+    auto operator<=>(const X&) const = default;
+  };
+  X x;
+  static_assert(noexcept(x <=> x));
+  static_cast<void>(x <=> x);
+}