[clang] [Clang] disallow constexpr with auto and explicit type in C23 (PR #163469)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 14 15:46:09 PDT 2025
https://github.com/a-tarasyuk created https://github.com/llvm/llvm-project/pull/163469
Fixes #163090
---
This PR addresses the issue of Clang not diagnosing the invalid combination of `constexpr`,
`auto`, and an explicit type
```c
constexpr auto int x = 0
```
>From 3856379ddbb59c6dcd813765571a67f635034a3c Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <oleksandr.tarasiuk at outlook.com>
Date: Wed, 15 Oct 2025 01:42:15 +0300
Subject: [PATCH] [Clang] disallow constexpr with auto and explicit type in C23
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/DeclSpec.cpp | 2 +-
clang/test/Parser/c2x-auto.c | 2 ++
3 files changed, 5 insertions(+), 1 deletion(-)
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;
}
More information about the cfe-commits
mailing list