[clang] [clang][Sema] Fix 'FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter' (PR #235816)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 27 20:49:22 PDT 2026


https://github.com/xiaosa-zhz updated https://github.com/llvm/llvm-project/pull/235816

>From 7b535b6bca9766b6692fe279721c8709a591439b Mon Sep 17 00:00:00 2001
From: xiaosaOvO <dr.xiaosa at gmail.com>
Date: Mon, 26 Oct 2026 19:58:20 +0800
Subject: [PATCH 1/6] The Fix

---
 clang/lib/AST/ComputeDependence.cpp |  4 ++--
 clang/lib/Sema/SemaExpr.cpp         | 23 ++++++++++++++++++++---
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/clang/lib/AST/ComputeDependence.cpp b/clang/lib/AST/ComputeDependence.cpp
index 7ea1c67ff77389..c9ea0986500307 100644
--- a/clang/lib/AST/ComputeDependence.cpp
+++ b/clang/lib/AST/ComputeDependence.cpp
@@ -327,7 +327,7 @@ ExprDependence clang::computeDependence(CXXThisExpr *E) {
   // Note: The standard does not mention this case explicitly, but we need
   // to do this so we can mark NSDM accesses as dependent.
   if (E->isCapturedByCopyInLambdaWithExplicitObjectParameter())
-    D |= ExprDependence::Type;
+    D |= ExprDependence::TypeValueInstantiation;
 
   assert(!(D & ExprDependence::UnexpandedPack));
   return D;
@@ -544,7 +544,7 @@ ExprDependence clang::computeDependence(DeclRefExpr *E, const ASTContext &Ctx) {
   //    - an identifier associated by name lookup with an entity captured by
   //    copy ([expr.prim.lambda.capture])
   if (E->isCapturedByCopyInLambdaWithExplicitObjectParameter())
-    Deps |= ExprDependence::Type;
+    Deps |= ExprDependence::TypeValueInstantiation;
 
   //    - a conversion-function-id that specifies a dependent type
   if (Decl->getDeclName().getNameKind() ==
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 00dd8acf1f68dd..4839acb2522cf3 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -21252,9 +21252,26 @@ static void FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter(
       if (MD->getType().isNull())
         continue;
 
-      const auto *Ty = MD->getType()->getAs<FunctionProtoType>();
-      if (!Ty || !MD->isExplicitObjectMemberFunction() ||
-          !Ty->getParamType(0)->isDependentType())
+      if (!MD->isExplicitObjectMemberFunction())
+        continue;
+
+      bool IsDependentObjectParam = [&]() {
+        const ParmVarDecl *ObjectParamDecl = MD->getParamDecl(0);
+        assert(ObjectParamDecl && "Shall explicit object parameter exist here.");
+        QualType ObjectParamType = ObjectParamDecl->getType();
+        // If we are in the middle of instantiation, try to use
+        // the real type of the deduced explicit object parameter.
+        if (auto *CIS = SemaRef.CurrentInstantiationScope) {
+          if (auto *Inst = CIS->getInstantiationOfIfExists(ObjectParamDecl)) {
+            auto *Deduced = dyn_cast<ParmVarDecl>(Inst->dyn_cast<Decl *>());
+            assert(Deduced);
+            ObjectParamType = Deduced->getType();
+          }
+        }
+        return ObjectParamType->isDependentType();
+      }();
+
+      if (!IsDependentObjectParam)
         continue;
 
       if (auto *C = LSI->CaptureMap.count(D) ? &LSI->getCapture(D) : nullptr) {

>From 6a2f227db0ffb046c5dd8559eefbffd7a05bcc9e Mon Sep 17 00:00:00 2001
From: xiaosaOvO <dr.xiaosa at gmail.com>
Date: Mon, 26 Oct 2026 20:13:31 +0800
Subject: [PATCH 2/6] The Docs

---
 clang/docs/ReleaseNotes.md | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 200c41fb9e4a29..34a1deca883535 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -899,6 +899,13 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 #### Bug Fixes to C++ Support
 - Instantiate constexpr functions as needed before they are evaluated. (#GH73232) (#GH35052) (#GH100897)
 
+- Fixed missing dependence flag when try to decide whether by-value capture
+  of lambda with explicit object parameter is dependent. (#GH125439) (#GH163582)
+
+- Fixed incorrectly reference to object parameter of function prototype
+  rather than the one of current instantiation when try to decide whether
+  by-value capture of lambda with explicit object parameter is dependent. (#GH153493)
+
 - Fixed lambdas with specifiers or attributes after the capture list being
   misparsed as function declarations in direct-initialization contexts under
   `-fms-extensions` or in HLSL mode.

>From fb5fa2dac1300a1eb2e3b71d2fb52fdaacd2a969 Mon Sep 17 00:00:00 2001
From: xiaosaOvO <dr.xiaosa at gmail.com>
Date: Mon, 26 Oct 2026 22:52:41 +0800
Subject: [PATCH 3/6] The tests

---
 clang/test/CodeGenCXX/cxx2b-deducing-this.cpp | 39 ++++++++++--
 clang/test/SemaCXX/cxx2b-deducing-this.cpp    | 59 +++++++++++++++++++
 2 files changed, 92 insertions(+), 6 deletions(-)

diff --git a/clang/test/CodeGenCXX/cxx2b-deducing-this.cpp b/clang/test/CodeGenCXX/cxx2b-deducing-this.cpp
index 9664a866376ae8..9865cc159967f7 100644
--- a/clang/test/CodeGenCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/CodeGenCXX/cxx2b-deducing-this.cpp
@@ -265,6 +265,33 @@ void test() {
 // CHECK: call void @_ZN5P27971C1cEi
 }
 
+// This used to crash because capture is not set as instantiation dependent correctly,
+// and then the <dependent-type> leaks to the codegen step.
+namespace GH125439 {
+void main() {
+  int x = 0;
+  auto f = [x](this auto) { decltype(x) n; (void)x; };
+  f();
+}
+}
+// CHECK-LABEL: define {{.*}} void @_ZN8GH1254394mainEv()
+// CHECK: call void @"_ZZN8GH1254394mainEvE{{.*}}"
+// CHECK-LABEL: define {{.*}} void @"_ZZN8GH1254394mainEvE{{.*}}"
+
+// This used to crash because capture in return type did not see
+// the real type of deduced explicit object parameter during instantiation.
+namespace GH153493 {
+void main() {
+  auto f = [b = 0](this auto&&) -> decltype(b + 0) {
+    return b;
+  };
+  f();
+}
+}
+// CHECK-LABEL: define {{.*}} void @_ZN8GH1534934mainEv()
+// CHECK:       call {{.*}} i32 @"_ZZN8GH1534934mainEv{{.*}}"
+// CHECK-LABEL: define {{.*}} i32 @"_ZZN8GH1534934mainEv{{.*}}"
+
 // This used to crash because we weren’t instantiating a dependent 'this'.
 namespace GH154054 {
 struct S {
@@ -285,11 +312,11 @@ void main() {
 
 // CHECK-LABEL: define {{.*}} i32 @_ZZN8GH1540541S5byvalEvENHUlT_E_clIS2_EEDaS1_(i32 %.coerce)
 // CHECK: entry:
-// CHECK:   %0 = alloca %class.anon.11, align 4
-// CHECK:   %coerce.dive = getelementptr inbounds nuw %class.anon.11, ptr %0, i32 0, i32 0
+// CHECK:   %0 = alloca %class.anon.13, align 4
+// CHECK:   %coerce.dive = getelementptr inbounds nuw %class.anon.13, ptr %0, i32 0, i32 0
 // CHECK:   %coerce.dive1 = getelementptr inbounds nuw %"struct.GH154054::S", ptr %coerce.dive, i32 0, i32 0
 // CHECK:   store i32 %.coerce, ptr %coerce.dive1, align 4
-// CHECK:   %1 = getelementptr inbounds nuw %class.anon.11, ptr %0, i32 0, i32 0
+// CHECK:   %1 = getelementptr inbounds nuw %class.anon.13, ptr %0, i32 0, i32 0
 // CHECK:   %x = getelementptr inbounds nuw %"struct.GH154054::S", ptr %1, i32 0, i32 0
 // CHECK:   %2 = load i32, ptr %x, align 4
 // CHECK:   ret i32 %2
@@ -309,10 +336,10 @@ void f() {
 
 // CHECK-LABEL: define {{.*}} ptr @_ZZN8GH1540541s1fEvENHUlT_E_clIS2_EEDaS1_(i32 %.coerce)
 // CHECK: entry:
-// CHECK:   %0 = alloca %class.anon.12, align 4
-// CHECK:   %coerce.dive = getelementptr inbounds nuw %class.anon.12, ptr %0, i32 0, i32 0
+// CHECK:   %0 = alloca %class.anon.14, align 4
+// CHECK:   %coerce.dive = getelementptr inbounds nuw %class.anon.14, ptr %0, i32 0, i32 0
 // CHECK:   %coerce.dive1 = getelementptr inbounds nuw %"struct.GH154054::s", ptr %coerce.dive, i32 0, i32 0
 // CHECK:   store i32 %.coerce, ptr %coerce.dive1, align 4
-// CHECK:   %1 = getelementptr inbounds nuw %class.anon.12, ptr %0, i32 0, i32 0
+// CHECK:   %1 = getelementptr inbounds nuw %class.anon.14, ptr %0, i32 0, i32 0
 // CHECK:   ret ptr %1
 }
diff --git a/clang/test/SemaCXX/cxx2b-deducing-this.cpp b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
index 5dfc5bbf50abd3..097179457bddbb 100644
--- a/clang/test/SemaCXX/cxx2b-deducing-this.cpp
+++ b/clang/test/SemaCXX/cxx2b-deducing-this.cpp
@@ -1404,6 +1404,65 @@ void f() {
 
 }
 
+// Capture is not set as instantiation dependent correctly
+namespace GH163582 {
+
+// Builtin __is_same(T, U) does not triger the bug
+template <class T, class U> constexpr bool is_same_v = false;
+template <class T> constexpr bool is_same_v<T, T> = true;
+
+struct S {
+  int x = 1;
+
+  void f() {
+    [n = 42](this auto&&) {
+      static_assert(is_same_v<decltype(n), int>);
+      static_assert(is_same_v<decltype((n)), int&>);
+      static_assert(is_same_v<decltype(n), short>); // expected-error {{static assertion failed}}
+    }(); // expected-note {{in instantiation of function template specialization}}
+
+    [n = 42](this const auto&) {
+      static_assert(is_same_v<decltype(n), int>);
+      static_assert(is_same_v<decltype((n)), const int&>);
+      static_assert(is_same_v<decltype(n), long>); // expected-error {{static assertion failed}}
+    }(); // expected-note {{in instantiation of function template specialization}}
+
+    [*this](this auto&&) {
+      static_assert(is_same_v<decltype(this), S*>);
+      static_assert(is_same_v<decltype(this), int>); // expected-error {{static assertion failed}}
+    }(); // expected-note {{in instantiation of function template specialization}}
+
+    [*this](this const auto&) {
+      static_assert(is_same_v<decltype(this), const S*>);
+      static_assert(is_same_v<decltype(this), void>); // expected-error {{static assertion failed}}
+    }(); // expected-note {{in instantiation of function template specialization}}
+
+    [&r = x](this const auto&) {
+      static_assert(is_same_v<decltype(r), int&>);
+    }();
+
+    const int y = 0;
+    [=, this](this auto&&) {
+      static_assert(is_same_v<decltype(this), S*>);
+      static_assert(is_same_v<decltype(x), int>);
+      static_assert(is_same_v<decltype((x)), int&>);
+      static_assert(is_same_v<decltype(y), const int>);
+      static_assert(is_same_v<decltype((y)), const int&>);
+      [&, *this](this const auto&) {
+        [=, this](this auto&&) {
+          static_assert(is_same_v<decltype(this), const S*>);
+          static_assert(is_same_v<decltype(x), int>);
+          static_assert(is_same_v<decltype((x)), const int&>);
+          static_assert(is_same_v<decltype(y), const int>);
+          static_assert(is_same_v<decltype((y)), const int&>);
+        }();
+      }();
+    }();
+  }
+};
+
+} // namespace GH163582
+
 namespace GH173943 {
 
 a void Bar(this int) { // expected-note {{candidate function}}

>From d0fdcfa83854de23d823b124910a33c471288bb2 Mon Sep 17 00:00:00 2001
From: xiaosaOvO <dr.xiaosa at gmail.com>
Date: Tue, 27 Oct 2026 04:11:04 +0800
Subject: [PATCH 4/6] Use cast with assert

---
 clang/lib/Sema/SemaExpr.cpp | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 4839acb2522cf3..6851c18128a94e 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -21258,16 +21258,14 @@ static void FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter(
       bool IsDependentObjectParam = [&]() {
         const ParmVarDecl *ObjectParamDecl = MD->getParamDecl(0);
         assert(ObjectParamDecl && "Shall explicit object parameter exist here.");
-        QualType ObjectParamType = ObjectParamDecl->getType();
+
         // If we are in the middle of instantiation, try to use
         // the real type of the deduced explicit object parameter.
-        if (auto *CIS = SemaRef.CurrentInstantiationScope) {
-          if (auto *Inst = CIS->getInstantiationOfIfExists(ObjectParamDecl)) {
-            auto *Deduced = dyn_cast<ParmVarDecl>(Inst->dyn_cast<Decl *>());
-            assert(Deduced);
-            ObjectParamType = Deduced->getType();
-          }
-        }
+        QualType ObjectParamType = ObjectParamDecl->getType();
+        if (auto *CIS = SemaRef.CurrentInstantiationScope)
+          if (auto *Inst = CIS->getInstantiationOfIfExists(ObjectParamDecl))
+            ObjectParamType = cast<ParmVarDecl>(cast<Decl *>(*Inst))->getType();
+        
         return ObjectParamType->isDependentType();
       }();
 

>From 392e97b5f07a7c20c41222af7c1bc62f589053ec Mon Sep 17 00:00:00 2001
From: xiaosaOvO <dr.xiaosa at gmail.com>
Date: Tue, 27 Oct 2026 13:07:27 +0800
Subject: [PATCH 5/6] Add shortcut for already substituted object param e.g. in
 lambda body, not signature

---
 clang/lib/Sema/SemaExpr.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 6851c18128a94e..fb68f90c09b090 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -21259,13 +21259,16 @@ static void FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter(
         const ParmVarDecl *ObjectParamDecl = MD->getParamDecl(0);
         assert(ObjectParamDecl && "Shall explicit object parameter exist here.");
 
-        // If we are in the middle of instantiation, try to use
-        // the real type of the deduced explicit object parameter.
         QualType ObjectParamType = ObjectParamDecl->getType();
+        if (!ObjectParamType->isDependentType())
+          return false;
+
+        // If we are in the middle of instantiation of signature, try to use
+        // the real type of the deduced explicit object parameter.
         if (auto *CIS = SemaRef.CurrentInstantiationScope)
           if (auto *Inst = CIS->getInstantiationOfIfExists(ObjectParamDecl))
             ObjectParamType = cast<ParmVarDecl>(cast<Decl *>(*Inst))->getType();
-        
+
         return ObjectParamType->isDependentType();
       }();
 

>From 94846cacf4cc4df5d02ea8bd980b880490ab5f30 Mon Sep 17 00:00:00 2001
From: xiaosa-zhz <56922836+xiaosa-zhz at users.noreply.github.com>
Date: Wed, 28 Oct 2026 11:49:09 +0800
Subject: [PATCH 6/6] make clang-format happy

---
 clang/lib/Sema/SemaExpr.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index fb68f90c09b090..c11f75a09b5063 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -21257,7 +21257,8 @@ static void FixDependencyOfIdExpressionsInLambdaWithDependentObjectParameter(
 
       bool IsDependentObjectParam = [&]() {
         const ParmVarDecl *ObjectParamDecl = MD->getParamDecl(0);
-        assert(ObjectParamDecl && "Shall explicit object parameter exist here.");
+        assert(ObjectParamDecl &&
+               "Shall explicit object parameter exist here.");
 
         QualType ObjectParamType = ObjectParamDecl->getType();
         if (!ObjectParamType->isDependentType())



More information about the cfe-commits mailing list