From: Marek Polacek <polacek@redhat.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>, Jason Merrill <jason@redhat.com>
Subject: [PATCH] c++: disallow constinit on functions [PR111173]
Date: Tue, 29 Aug 2023 15:01:56 -0400 [thread overview]
Message-ID: <20230829190156.56643-1-polacek@redhat.com> (raw)
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
next reply other threads:[~2023-08-29 19:02 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-29 19:01 Marek Polacek [this message]
2023-08-30 13:11 ` Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230829190156.56643-1-polacek@redhat.com \
--to=polacek@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jason@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).