[clang] [clang] Reject 'auto' storage class with type specifier in C++ (PR #166004)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 21 07:30:32 PST 2025


================
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++23 %s
+
+// Test that 'auto' cannot be combined with a type specifier in C++.
+void f() {
+  auto int x = 1;        // expected-error {{'auto' cannot be combined with a type specifier}}
----------------
erichkeane wrote:

> > Can someone clarify why we would need new annotation or tentative parsing here? It seems to me that we can handle `auto` incrementally while building the list of decl specifiers without any novel lookahead, and I thought that's what we already did. I would expect that we're just converting an extension warning to an error here, and then doing some simplification if we can.
> 
> The current logic does: `if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {` and this fails with:
> 
> ```
> template <typename Ty>
> void g() {
>   auto Ty x;
> }
> ```
> 
> because the token we get from lookahead is `identifier` for `Ty`. We do not know that `Ty` is a type unless we do the annotation dance. Are you suggesting the better approach is to wait until `DeclSpec::Finish()` and determine it's invalid then?
> 
> > We know Ty is a type,
> 
> We only know that after we do the annotation, unless I'm missing something.

We SHOULD actually know it is a type... Kinda just based on location, but it is a `typename` in the template, so we should know the type.  Also, any type spelled there is supposed to use the `typename` keyword if it isn't obvious (so `auto typename Ty::type x`, though we don't actually enforce that (its a Warning-as-default-error).  Not sure what we could/should do in the latter position, but my kneejerk is to just reject it always.

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


More information about the cfe-commits mailing list