[clang] [Clang] Don't skip capture of parameters in default arguments (PR #235529)

Chen Miao via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 15:46:06 PST 2026


================
@@ -20240,8 +20240,16 @@ bool Sema::tryCaptureVariable(
   // Exception: Function parameters are not tied to the function's DeclContext
   // until we enter the function definition. Capturing them anyway would result
   // in an out-of-bounds error while traversing DC and its parents.
-  if (isa<ParmVarDecl>(Var) && !VarDC->isFunctionOrMethod())
-    return true;
+
+  bool InDefaultArg = llvm::any_of(
+    ExprEvalContexts, [](const ExpressionEvaluationContextRecord &C) {
+      return isa_and_nonnull<ParmVarDecl>(C.ManglingContextDecl);
+    });
+  if (isa<ParmVarDecl>(Var) && !VarDC->isFunctionOrMethod() && !InDefaultArg &&
+    llvm::any_of(ExprEvalContexts,
+                 [](const ExpressionEvaluationContextRecord &C) {
+                   return C.isUnevaluated();
+                 })) return true;
----------------
ChenMiaoi wrote:

I’m not familiar with this area, but after testing locally, I found that at least these two cases are missing.

``` cc
int f(int g, int h = [g] { return 0; }());
// and
auto u(int g) -> decltype([] { return g; }());
```

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


More information about the cfe-commits mailing list