[clang] [Clang] [C++26] Implement P1306R5 Expansion Statements (PR #165195)

Daniel M. Katz via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 27 14:24:28 PDT 2025


================
@@ -2393,9 +2413,13 @@ ExprResult
 TemplateInstantiator::TransformFunctionParmPackRefExpr(DeclRefExpr *E,
                                                        ValueDecl *PD) {
   typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
-  llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
-    = getSema().CurrentInstantiationScope->findInstantiationOf(PD);
-  assert(Found && "no instantiation for parameter pack");
+  llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found =
+      getSema().CurrentInstantiationScope->getInstantiationOfIfExists(PD);
+
+  // This can happen when instantiating an expansion statement that contains
+  // a pack (e.g. `template for (auto x : {{ts...}})`).
+  if (!Found)
+    return E;
----------------
katzdm wrote:

Oh man, I spent hours debugging the assertions around this...

...I might be misremembering, but this might be to handle something like:

```cpp
template <typename... Ts>
int fn(Ts... ts) {
  template for (int i : {1, 2, 3}) {
    return (ts, ...);
  }
}
```

which, as it turns out, crashes my implementation lol. Speaking of, I have a _great_ suggestion for a test case! 😄 

But the interesting thing about the above is that the expansion statement can be instantiated even while the function is dependent - my approach was to instantiate expansion statements as eagerly as possible to facilitate early diagnosis.

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


More information about the cfe-commits mailing list