aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2023-08-29 13:16:41 -0400
committerMarek Polacek <polacek@redhat.com>2023-08-30 10:40:12 -0400
commitc121afc3b6c96a1f229ba0c4a4de6bd4b6be9a53 (patch)
tree9c6b8b14a966eee3c25b75f138217389732602ba /gcc
parent7f2ed06ddc825e8a4e0edfd1d66b5156e6dc1d34 (diff)
downloadgcc-c121afc3b6c96a1f229ba0c4a4de6bd4b6be9a53.zip
gcc-c121afc3b6c96a1f229ba0c4a4de6bd4b6be9a53.tar.gz
gcc-c121afc3b6c96a1f229ba0c4a4de6bd4b6be9a53.tar.bz2
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.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/decl.cc3
-rw-r--r--gcc/testsuite/g++.dg/cpp2a/constinit19.C5
2 files changed, 8 insertions, 0 deletions
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index bea0ee92..a0e8a24 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 0000000..5be610a
--- /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" }