[clang] Fix a crash in handling this-pointer captured in nested lambdas in field initializater. (PR #245491)

Wontae Choi via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 17:04:25 PST 2026


================
@@ -1101,6 +1101,27 @@ Environment::Environment(const Environment &Other)
       ExprToLoc(Other.ExprToLoc), ExprToVal(Other.ExprToVal),
       LocToVal(Other.LocToVal), BoolState(Other.BoolState->clone()) {}
 
+// Returns the field whose default member initializer contains the lambda with
+// closure type `Closure`, or null if there is no such field.
+//
+// `Closure` may be nested inside other lambdas within the default member
+// initializer. In that case only the outermost closure has the `FieldDecl` as
+// its lambda context declaration. So walk out through the enclosing lambdas
+// until a context declaration is found.
+static const FieldDecl *
+getFieldBeingInitializedByLambda(const CXXRecordDecl *Closure) {
+  while (Closure->isLambda()) {
+    if (const Decl *ContextDecl = Closure->getLambdaContextDecl())
+      return dyn_cast<FieldDecl>(ContextDecl);
+    const auto *EnclosingMethod =
+        dyn_cast<CXXMethodDecl>(Closure->getDeclContext());
+    if (EnclosingMethod == nullptr || !EnclosingMethod->getParent()->isLambda())
----------------
wtchoi wrote:

Ah... good catch. Fixed

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


More information about the cfe-commits mailing list