]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: disallow constinit on functions [PR111173]
authorMarek Polacek <polacek@redhat.com>
Tue, 29 Aug 2023 17:16:41 +0000 (13:16 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 30 Aug 2023 14:40:12 +0000 (10:40 -0400)
[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
gcc/testsuite/g++.dg/cpp2a/constinit19.C [new file with mode: 0644]

index bea0ee92106889f43b3881cc577f20ade2ccf6fd..a0e8a24efc0eda0ebc4c14e01eef6802aa370dd7 100644 (file)
@@ -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 (file)
index 0000000..5be610a
--- /dev/null
@@ -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" }