- Notifications
You must be signed in to change notification settings - Fork 14.9k
[Clang] disallow constexpr with auto and explicit type in C23 #163469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-clang Author: Oleksandr T. (a-tarasyuk) ChangesFixes #163090 This PR addresses the issue of Clang not diagnosing the invalid combination of constexpr auto int x = 0 Full diff: https://github.com/llvm/llvm-project/pull/163469.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index edb872c1f388d..de745e54a0cbd 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -269,6 +269,8 @@ Non-comprehensive list of changes in this release allocation functions with a token ID can be enabled via the ``-fsanitize=alloc-token`` flag. +- Clang now rejects the invalid use of ``constexpr`` with ``auto`` and an explicit type. (#GH163090) + New Compiler Flags ------------------ - New option ``-fno-sanitize-debug-trap-reasons`` added to disable emitting trap reasons into the debug info when compiling with trapping UBSan (e.g. ``-fsanitize-trap=undefined``). diff --git a/clang/lib/Sema/DeclSpec.cpp b/clang/lib/Sema/DeclSpec.cpp index 184d31ecd1e40..482cb1fe703fe 100644 --- a/clang/lib/Sema/DeclSpec.cpp +++ b/clang/lib/Sema/DeclSpec.cpp @@ -1369,7 +1369,7 @@ void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) { if (S.getLangOpts().C23 && getConstexprSpecifier() == ConstexprSpecKind::Constexpr && - StorageClassSpec == SCS_extern) { + (StorageClassSpec == SCS_extern || StorageClassSpec == SCS_auto)) { S.Diag(ConstexprLoc, diag::err_invalid_decl_spec_combination) << DeclSpec::getSpecifierName(getStorageClassSpec()) << SourceRange(getStorageClassSpecLoc()); diff --git a/clang/test/Parser/c2x-auto.c b/clang/test/Parser/c2x-auto.c index b878a5b7c42d4..b33b267841132 100644 --- a/clang/test/Parser/c2x-auto.c +++ b/clang/test/Parser/c2x-auto.c @@ -62,6 +62,8 @@ auto basic_usage(auto auto) { // c23-error {{'auto' not allowed in function pr int auto_cxx_decl = auto(0); // expected-error {{expected expression}} + constexpr auto int x = 0; // c23-error {{cannot combine with previous 'auto' declaration specifier}} \ + c17-error {{use of undeclared identifier 'constexpr'}} return c; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for working on this!
… into fix/163090
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
auto int d1 = 0; | ||
int auto d2 = 0; // c23-error {{cannot combine with previous 'int' declaration specifier}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the order of keywords here significant?
Fixes #163090
This PR addresses the issue of Clang not diagnosing the invalid combination of
constexpr
,auto
, and an explicit type