Skip to content

Conversation

a-tarasyuk
Copy link
Member

Fixes #163090


This PR addresses the issue of Clang not diagnosing the invalid combination of constexpr,
auto, and an explicit type

constexpr auto int x = 0
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 14, 2025

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #163090


This PR addresses the issue of Clang not diagnosing the invalid combination of constexpr,
auto, and an explicit type

constexpr auto int x = 0

Full diff: https://github.com/llvm/llvm-project/pull/163469.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/DeclSpec.cpp (+1-1)
  • (modified) clang/test/Parser/c2x-auto.c (+2)
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; } 
Copy link
Contributor

@Fznamznon Fznamznon left a 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!

@a-tarasyuk a-tarasyuk requested a review from Fznamznon October 15, 2025 20:28
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@a-tarasyuk a-tarasyuk merged commit dc27696 into llvm:main Oct 17, 2025
11 checks passed
Comment on lines +157 to +158
auto int d1 = 0;
int auto d2 = 0; // c23-error {{cannot combine with previous 'int' declaration specifier}}

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

6 participants