[clang] [Sema] Preserve ContainsUnexpandedParameterPack in TransformLambdaExpr (PR #86265)

Matheus Izvekov via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 18 09:23:58 PDT 2024


================
@@ -14636,6 +14645,20 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
                                     /*IsInstantiation*/ true);
   SavedContext.pop();
 
+  // Parts other than the capture e.g. the lambda body might still contain a
+  // pattern that an outer fold expression would expand.
+  //
+  // We don't have a way to propagate up the ContainsUnexpandedParameterPack
+  // flag from a Stmt, so we have to revisit the lambda.
+  if (!LSICopy.ContainsUnexpandedParameterPack) {
+    llvm::SmallVector<UnexpandedParameterPack> UnexpandedPacks;
+    getSema().collectUnexpandedParameterPacksFromLambda(NewCallOperator,
+                                                        UnexpandedPacks);
+    // FIXME: Should we call Sema::DiagnoseUnexpandedParameterPacks() instead?
+    // Unfortunately, that requires the LambdaScopeInfo to exist, which has been
+    // removed by ActOnFinishFunctionBody().
+    LSICopy.ContainsUnexpandedParameterPack = !UnexpandedPacks.empty();
+  }
----------------
mizvekov wrote:

I am not sure I understand why we would need to call `Sema::DiagnoseUnexpandedParameterPacks` here.

I think we should be able to diagnose all unexpanded parameter packs during the initial lambda parsing, so what gives?

Also, I am not sure what you mean by leaving it as an NFC.

Regardless, you have a copy of the LSI here, so what would stop you from pushing a new Lambda Scope and copying this LSI back into it? Something like:

```C++
  LambdaScopeInfo *LSI = getSema().PushLambdaScope();
  *LSI = LSICopy;
  Sema::FunctionScopeRAII FuncScopeCleanup(getSema());
```

This is obviously an ugly hack, but I don't think the solution is to simply teach `ActOnFinishFunctionBody` not to pop the function scope.

See this FIXME for a potentially more comprehensive fix: https://github.com/llvm/llvm-project/blob/77ac07444d32668d5826ef27c24180fb10425213/clang/lib/Sema/TreeTransform.h#L14648

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


More information about the cfe-commits mailing list