From: Marek Polacek Date: Tue, 29 Aug 2023 17:16:41 +0000 (-0400) Subject: c++: disallow constinit on functions [PR111173] X-Git-Tag: basepoints/gcc-15~6563 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c121afc3b6c96a1f229ba0c4a4de6bd4b6be9a53;p=thirdparty%2Fgcc.git c++: disallow constinit on functions [PR111173] [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. --- diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index bea0ee921068..a0e8a24efc0e 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -14639,6 +14639,9 @@ grokdeclarator (const cp_declarator *declarator, "storage class % invalid for " "function %qs", name); } + else if (constinit_p) + error_at (declspecs->locations[ds_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 000000000000..5be610a18a26 --- /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" }