https://github.com/HamzaHassanain created 
https://github.com/llvm/llvm-project/pull/174281

### **The Problem**

Clang crashes when encountering a `static` data member declared with an alias 
template but lacking template arguments (e.g., `static E u;`).

Because of C++ CTAD rules, Clang's parser assigns this a placeholder 
`AutoType`. If this member is later referenced, the backend (`CodeGen`) 
attempts to emit the variable. Since the type remains undeduced and no 
initializer exists, the compiler hits an `llvm_unreachable` in 
`CodeGenTypes.cpp`.

### **The Fix**

The fix is implemented in `Sema::ActOnUninitializedDecl`. We now explicitly 
check if a `static` data member still possesses an undeduced placeholder type 
after all deduction attempts have failed.

* **Logic:** If the type contains an undeduced type, and the variable is a 
static member without an initializer, we emit `err_auto_var_requires_init`.


### **Changes**

* **SemaDecl.cpp**: Added validation logic to `ActOnUninitializedDecl` to catch 
undeduced static members.
* **alias-template-static-member.cpp**: Added a regression test to ensure this 
scenario results in a diagnostic rather than a crash.


### **Notes** on formatting

When I `clang-format` the `SemaDecl.cpp` after applying my changes, I found the 
whole file got changed, so I undid the formatting. 

>From aafd57e112ea07a66e2864c9034ac8bbc328c0b2 Mon Sep 17 00:00:00 2001
From: Hamza Hassanain <[email protected]>
Date: Sat, 3 Jan 2026 17:07:05 +0200
Subject: [PATCH 1/2] [Clang][Sema] reject undeduced static members without
 init

---
 clang/lib/Sema/SemaDecl.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 11323803e1910..50a6a4176d1ec 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14337,8 +14337,18 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
         DeduceVariableDeclarationType(Var, false, nullptr))
       return;
 
+    Type = Var->getType();
     this->CheckAttributesOnDeducedType(RealDecl);
 
+    if (auto *Deduced = Type->getContainedDeducedType()) {
+      if (Var->isStaticDataMember() && Deduced->getDeducedType().isNull()) {
+        Diag(Var->getLocation(), diag::err_auto_var_requires_init)
+            << Var->getDeclName() << Type;
+        Var->setInvalidDecl();
+        return;
+      }
+    }
+
     // C++11 [class.static.data]p3: A static data member can be declared with
     // the constexpr specifier; if so, its declaration shall specify
     // a brace-or-equal-initializer.

>From 74473fd4f581b5538e01b8a3c1e9e068f9c88831 Mon Sep 17 00:00:00 2001
From: Hamza Hassanain <[email protected]>
Date: Sat, 3 Jan 2026 17:31:34 +0200
Subject: [PATCH 2/2] [Clang][Test][SemaCXX] added alias tempalte static member
 hanlding test

---
 clang/test/SemaCXX/alias-template-static-member.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100644 clang/test/SemaCXX/alias-template-static-member.cpp

diff --git a/clang/test/SemaCXX/alias-template-static-member.cpp 
b/clang/test/SemaCXX/alias-template-static-member.cpp
new file mode 100644
index 0000000000000..0089aea33cc80
--- /dev/null
+++ b/clang/test/SemaCXX/alias-template-static-member.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+
+template <class T>
+struct A {
+  template <class U>
+  using E = U;
+
+  static E u; // expected-error {{declaration of variable 'u' with deduced 
type 'E' requires an initializer}}
+};
+
+decltype(A<int>::u) a;
\ No newline at end of file

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to