[clang] [Clang] disallow constexpr with auto and explicit type in C23 (PR #163469)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 14 15:46:41 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

<details>
<summary>Changes</summary>

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
```

---
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
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;
 }
 

``````````

</details>


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


More information about the cfe-commits mailing list