[clang] [clang] Do not substitute parameter pack while retaining the pack expansion (PR #108197)

via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 11 05:00:42 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Utkarsh Saxena (usx95)

<details>
<summary>Changes</summary>

Consider when Input[I] is a VarDecl with parameter pack. We would have already expanded the pack before the code change in the loop`for (unsigned I = 0; I != *NumExpansions; ++I) {`.

Now in `if (RetainExpansion) {`, without this change, we continue to substitute the pack in the pattern even when we do not have meaningful `ArgumentPackSubstitutionIndex` set. 

This leads to use of an invalid pack substitution index in `TemplateInstantiator::TransformFunctionParmPackRefExpr` containing `TransformedDecl = (*Pack)[getSema().ArgumentPackSubstitutionIndex];`

This change set `ArgumentPackSubstitutionIndex` to `-1` while retaining expansion to instruct `TransformFunctionParmPackRefExpr` to build `FunctionParmPackExpr` instead of substituting the param pack.

---

There are other instances of `RetainExpansion` and IIUC, they should also unset the `ArgumentPackSubstitutionIndex`. It would be great if someone can verify my understanding. If this is correct then we could instead have a `ArgumentPackSubstitutionIndexRAII` as part of `ForgetPartiallySubstitutedPackRAII`.


Fixes https://github.com/llvm/llvm-project/issues/63819
Fixes https://github.com/llvm/llvm-project/issues/107560

---
Full diff: https://github.com/llvm/llvm-project/pull/108197.diff


1 Files Affected:

- (modified) clang/lib/Sema/TreeTransform.h (+2-1) 


``````````diff
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 0daf620b4123e4..0de43d2127b12f 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -4361,7 +4361,8 @@ bool TreeTransform<Derived>::TransformExprs(Expr *const *Inputs,
       // forgetting the partially-substituted parameter pack.
       if (RetainExpansion) {
         ForgetPartiallySubstitutedPackRAII Forget(getDerived());
-
+        // Simple transform producing another pack expansion.
+        Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
         ExprResult Out = getDerived().TransformExpr(Pattern);
         if (Out.isInvalid())
           return true;

``````````

</details>


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


More information about the cfe-commits mailing list