* [PATCH] c++: disallow constinit on functions [PR111173]
@ 2023-08-29 19:01 Marek Polacek
2023-08-30 13:11 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2023-08-29 19:01 UTC (permalink / raw)
To: GCC Patches, Jason Merrill
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
-- >8 --
[dcl.constinit]/1: The constinit specifier shall be applied only to a declaration
of a variable with static or thread storage duration.
and while we detect
constinit int fn();
we weren't detecting
using F = int();
constinit F f;
PR c++/111173
gcc/cp/ChangeLog:
* decl.cc (grokdeclarator): Disallow constinit on functions.
gcc/testsuite/ChangeLog:
* g++.dg/cpp2a/constinit19.C: New test.
---
gcc/cp/decl.cc | 3 +++
gcc/testsuite/g++.dg/cpp2a/constinit19.C | 5 +++++
2 files changed, 8 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/cpp2a/constinit19.C
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index bea0ee92106..a0e8a24efc0 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -14639,6 +14639,9 @@ grokdeclarator (const cp_declarator *declarator,
"storage class %<thread_local%> invalid for "
"function %qs", name);
}
+ else if (constinit_p)
+ error_at (declspecs->locations[ds_constinit],
+ "%<constinit%> specifier invalid for function %qs", name);
if (virt_specifiers)
error ("virt-specifiers in %qs not allowed outside a class "
diff --git a/gcc/testsuite/g++.dg/cpp2a/constinit19.C b/gcc/testsuite/g++.dg/cpp2a/constinit19.C
new file mode 100644
index 00000000000..5be610a18a2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constinit19.C
@@ -0,0 +1,5 @@
+// PR c++/111173
+// { dg-do compile { target c++20 } }
+
+using Function = int();
+constinit Function f; // { dg-error ".constinit. specifier invalid for function" }
base-commit: fce74ce2535aa3b7648ba82e7e61eb77d0175546
--
2.41.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] c++: disallow constinit on functions [PR111173]
2023-08-29 19:01 [PATCH] c++: disallow constinit on functions [PR111173] Marek Polacek
@ 2023-08-30 13:11 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-08-30 13:11 UTC (permalink / raw)
To: Marek Polacek, GCC Patches
On 8/29/23 15:01, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
OK.
> -- >8 --
>
> [dcl.constinit]/1: The constinit specifier shall be applied only to a declaration
> of a variable with static or thread storage duration.
>
> and while we detect
>
> constinit int fn();
>
> we weren't detecting
>
> using F = int();
> constinit F f;
>
> PR c++/111173
>
> gcc/cp/ChangeLog:
>
> * decl.cc (grokdeclarator): Disallow constinit on functions.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp2a/constinit19.C: New test.
> ---
> gcc/cp/decl.cc | 3 +++
> gcc/testsuite/g++.dg/cpp2a/constinit19.C | 5 +++++
> 2 files changed, 8 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/cpp2a/constinit19.C
>
> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> index bea0ee92106..a0e8a24efc0 100644
> --- a/gcc/cp/decl.cc
> +++ b/gcc/cp/decl.cc
> @@ -14639,6 +14639,9 @@ grokdeclarator (const cp_declarator *declarator,
> "storage class %<thread_local%> invalid for "
> "function %qs", name);
> }
> + else if (constinit_p)
> + error_at (declspecs->locations[ds_constinit],
> + "%<constinit%> specifier invalid for function %qs", name);
>
> if (virt_specifiers)
> error ("virt-specifiers in %qs not allowed outside a class "
> diff --git a/gcc/testsuite/g++.dg/cpp2a/constinit19.C b/gcc/testsuite/g++.dg/cpp2a/constinit19.C
> new file mode 100644
> index 00000000000..5be610a18a2
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/constinit19.C
> @@ -0,0 +1,5 @@
> +// PR c++/111173
> +// { dg-do compile { target c++20 } }
> +
> +using Function = int();
> +constinit Function f; // { dg-error ".constinit. specifier invalid for function" }
>
> base-commit: fce74ce2535aa3b7648ba82e7e61eb77d0175546
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-08-30 13:11 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-29 19:01 [PATCH] c++: disallow constinit on functions [PR111173] Marek Polacek
2023-08-30 13:11 ` 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).