[clang] [clang] Fix crash parsing delayed lambda default arguments (PR #213556)

via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 2 10:00:51 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Gauarv Chaudhary (ANAMASGARD)

<details>
<summary>Changes</summary>


  Closes #<!-- -->213420.

  This fixes a crash with `-fms-compatibility` when Clang late-parses an invalid default argument in a member function of a lambda-local anonymous struct.

  `getPredefinedExprDecl` assumed an active `LambdaScopeInfo` was always present. During delayed parsing, that scope may already have been popped. Handle that case and continue producing the normal diagnostics.

  Tests:
  - `llvm-lit -sv clang/test/SemaCXX/GH213420.cpp`
  - `llvm-lit -sv clang/test/SemaCXX` (no unexpected failures)
  - Manual assertion-enabled reproducer: no crash; emits the expected semantic errors
  - `git diff --check`

  AI assistance was used to investigate, implement, and test this change. I  reviewed the resulting patch and test coverage.

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


2 Files Affected:

- (modified) clang/lib/Sema/SemaExpr.cpp (+5-2) 
- (added) clang/test/SemaCXX/GH213420.cpp (+12) 


``````````diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 59b8c9b60663c..146dd7e78a27c 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2125,8 +2125,11 @@ static Decl *getPredefinedExprDecl(Sema &S, DeclContext *DC) {
       while (LSI != E && isa<CapturingScopeInfo>(*LSI) &&
              !isa<LambdaScopeInfo>(*LSI))
         ++LSI;
-      assert(LSI != E && "Should be in a lambda scope info");
-      if (dyn_cast<LambdaScopeInfo>(*LSI)->BeforeCompoundStatement)
+      if (LSI == E || !isa<LambdaScopeInfo>(*LSI)) {
+        // The lambda scope may already be popped during delayed parsing.
+        return;
+      }
+      if (cast<LambdaScopeInfo>(*LSI)->BeforeCompoundStatement)
         DC = DC->getParent();
       ++LSI;
     }
diff --git a/clang/test/SemaCXX/GH213420.cpp b/clang/test/SemaCXX/GH213420.cpp
new file mode 100644
index 0000000000000..2d1b9f032e7e4
--- /dev/null
+++ b/clang/test/SemaCXX/GH213420.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fms-compatibility -verify \
+// RUN:   -Wno-missing-declarations -Wno-unused-value %s
+
+void foo() {
+  [] {
+    struct { // expected-error {{anonymous structs and classes must be class members}}
+      void bar(int & = "") {} // expected-error {{non-const lvalue reference to type 'int' cannot bind}}
+                              // expected-note at -1 {{passing argument to parameter here}}
+                              // expected-error at -2 {{functions cannot be declared in an anonymous struct}}
+    };
+  };
+}

``````````

</details>


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


More information about the cfe-commits mailing list