[clang] Fix missing initializer for inline static template member with auto caused by delayed template instantiation. (PR #138122)

Yanzuo Liu via cfe-commits cfe-commits at lists.llvm.org
Fri May 2 07:41:01 PDT 2025


================
@@ -6027,8 +6027,15 @@ void Sema::BuildVariableInstantiation(
   Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar));
   Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar));
 
+  bool VarTemplateWithAutoType = false;
+  QualType VarSourceType = OldVar->getTypeSourceInfo()->getType();
+  if (VarSourceType->getAs<AutoType>()) {
+    VarTemplateWithAutoType = true;
+  }
+
   // Figure out whether to eagerly instantiate the initializer.
-  if (InstantiatingVarTemplate || InstantiatingVarTemplatePartialSpec) {
+  if ((InstantiatingVarTemplate && !VarTemplateWithAutoType) ||
+      InstantiatingVarTemplatePartialSpec) {
     // We're producing a template. Don't instantiate the initializer yet.
   } else if (NewVar->getType()->isUndeducedType()) {
     // We need the type to complete the declaration of the variable.
----------------
zwuis wrote:

It seems that we can just swap these two `if` statements. We need to handle partial specializations as well. E.g.

```cpp
template <typename> struct B {
  template <typename, typrname> inline static auto var = 6;
  template <typename T> inline static auto var<int, T> = 7;
};

int b = B<int>::var<int, int>;
```

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


More information about the cfe-commits mailing list