public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: constinit on pointer to function [PR104066]
@ 2022-11-17 18:38 Marek Polacek
  2022-11-17 23:56 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2022-11-17 18:38 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

[dcl.constinit]: "The constinit specifier shall be applied only to
a declaration of a variable with static or thread storage duration."

Thus, this ought to be OK:

  constinit void (*p)() = nullptr;

but the error message I introduced when implementing constinit was
not looking at funcdecl_p, so the code above was rejected.

Fixed thus.  I'm checking constinit_p first because I think that's
far more likely to be false than funcdecl_p.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
I think I'd like to backport this all the way back to 10.

	PR c++/104066

gcc/cp/ChangeLog:

	* decl.cc (grokdeclarator): Check funcdecl_p before complaining
	about constinit.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/constinit18.C: New test.
---
 gcc/cp/decl.cc                           |  2 +-
 gcc/testsuite/g++.dg/cpp2a/constinit18.C | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/constinit18.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index d28889ed865..9a7b1a6c381 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -13071,7 +13071,7 @@ grokdeclarator (const cp_declarator *declarator,
 			  "an array", name);
 		return error_mark_node;
 	      }
-	    if (constinit_p)
+	    if (constinit_p && funcdecl_p)
 	      {
 		error_at (declspecs->locations[ds_constinit],
 			  "%<constinit%> on function return type is not "
diff --git a/gcc/testsuite/g++.dg/cpp2a/constinit18.C b/gcc/testsuite/g++.dg/cpp2a/constinit18.C
new file mode 100644
index 00000000000..51b4f0273be
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constinit18.C
@@ -0,0 +1,12 @@
+// PR c++/104066
+// { dg-do compile { target c++20 } }
+
+constinit void (*p)() = nullptr;
+constinit void (*pp)() = nullptr;
+void fn();
+constinit void (&r)() = fn;
+
+extern constinit long (* const syscall_reexported) (long, ...);
+
+constinit void bad (); // { dg-error ".constinit. on function return type is not allowed" }
+constinit void bad () { } // { dg-error ".constinit. on function return type is not allowed" }

base-commit: ee892832ea19b21a3420ef042e582204fac852a2
-- 
2.38.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: constinit on pointer to function [PR104066]
  2022-11-17 18:38 [PATCH] c++: constinit on pointer to function [PR104066] Marek Polacek
@ 2022-11-17 23:56 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2022-11-17 23:56 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 11/17/22 13:38, Marek Polacek wrote:
> [dcl.constinit]: "The constinit specifier shall be applied only to
> a declaration of a variable with static or thread storage duration."
> 
> Thus, this ought to be OK:
> 
>    constinit void (*p)() = nullptr;
> 
> but the error message I introduced when implementing constinit was
> not looking at funcdecl_p, so the code above was rejected.
> 
> Fixed thus.  I'm checking constinit_p first because I think that's
> far more likely to be false than funcdecl_p.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> I think I'd like to backport this all the way back to 10.

OK for trunk and release branches.

> 	PR c++/104066
> 
> gcc/cp/ChangeLog:
> 
> 	* decl.cc (grokdeclarator): Check funcdecl_p before complaining
> 	about constinit.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/constinit18.C: New test.
> ---
>   gcc/cp/decl.cc                           |  2 +-
>   gcc/testsuite/g++.dg/cpp2a/constinit18.C | 12 ++++++++++++
>   2 files changed, 13 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/constinit18.C
> 
> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> index d28889ed865..9a7b1a6c381 100644
> --- a/gcc/cp/decl.cc
> +++ b/gcc/cp/decl.cc
> @@ -13071,7 +13071,7 @@ grokdeclarator (const cp_declarator *declarator,
>   			  "an array", name);
>   		return error_mark_node;
>   	      }
> -	    if (constinit_p)
> +	    if (constinit_p && funcdecl_p)
>   	      {
>   		error_at (declspecs->locations[ds_constinit],
>   			  "%<constinit%> on function return type is not "
> diff --git a/gcc/testsuite/g++.dg/cpp2a/constinit18.C b/gcc/testsuite/g++.dg/cpp2a/constinit18.C
> new file mode 100644
> index 00000000000..51b4f0273be
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/constinit18.C
> @@ -0,0 +1,12 @@
> +// PR c++/104066
> +// { dg-do compile { target c++20 } }
> +
> +constinit void (*p)() = nullptr;
> +constinit void (*pp)() = nullptr;
> +void fn();
> +constinit void (&r)() = fn;
> +
> +extern constinit long (* const syscall_reexported) (long, ...);
> +
> +constinit void bad (); // { dg-error ".constinit. on function return type is not allowed" }
> +constinit void bad () { } // { dg-error ".constinit. on function return type is not allowed" }
> 
> base-commit: ee892832ea19b21a3420ef042e582204fac852a2


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-17 23:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-17 18:38 [PATCH] c++: constinit on pointer to function [PR104066] Marek Polacek
2022-11-17 23:56 ` Jason Merrill

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).