[clang] Remove overly strict assertion for this-capture in blocks (PR #219080)
Akira Hatanaka via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 28 13:47:23 PDT 2026
https://github.com/ahatanak updated https://github.com/llvm/llvm-project/pull/219080
>From ccadf995d50bc47bfe7351d5b8628df7dce89e4d Mon Sep 17 00:00:00 2001
From: Akira Hatanaka <ahatanak at gmail.com>
Date: Wed, 26 Aug 2026 17:36:45 -0700
Subject: [PATCH 1/2] Remove overly strict assertion for this-capture in blocks
TransformBlockExpr asserted that if the instantiated block captures
'this', the uninstantiated pattern block must also have captured it.
This assumption doesn't always hold: a block that accesses a member
through a dependent qualified-id (e.g., 'T::m' inside a template
deriving from T) has no way to know at parse time that the access
will resolve to an implicit 'this->m', since T is unknown. Once the
template is instantiated and 'T::m' resolves to a non-static data
member, the instantiated block legitimately captures 'this' even
though the pattern never did.
rdar://184776458
---
clang/lib/Sema/TreeTransform.h | 6 ------
clang/test/CodeGenObjCXX/block-in-template-inst.mm | 11 +++++++++++
2 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 024f726b188b7..2de0cf300675b 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -17821,12 +17821,6 @@ TreeTransform<Derived>::TransformBlockExpr(BlockExpr *E) {
oldCapture));
assert(blockScope->CaptureMap.count(newCapture));
}
-
- // The this pointer may not be captured by the instantiated block, even when
- // it's captured by the original block, if the expression causing the
- // capture is in the discarded branch of a constexpr if statement.
- assert((!blockScope->isCXXThisCaptured() || oldBlock->capturesCXXThis()) &&
- "this pointer isn't captured in the old block");
}
#endif
diff --git a/clang/test/CodeGenObjCXX/block-in-template-inst.mm b/clang/test/CodeGenObjCXX/block-in-template-inst.mm
index 1ecd820be4501..50fc0896fd76d 100644
--- a/clang/test/CodeGenObjCXX/block-in-template-inst.mm
+++ b/clang/test/CodeGenObjCXX/block-in-template-inst.mm
@@ -68,3 +68,14 @@ void curry() {
auto t = c(1)(10)(100);
}
}
+
+namespace ThisCaptureViaDependentBase {
+ // This used to crash.
+ struct Base { int m; };
+
+ template <typename T> struct S : T {
+ void f() { ^{ (void)T::m; }(); }
+ };
+
+ template struct S<Base>;
+}
>From 8df220d8f71d067fbf3e89f18b8c442fb263601f Mon Sep 17 00:00:00 2001
From: Akira Hatanaka <ahatanak at gmail.com>
Date: Fri, 28 Aug 2026 13:46:17 -0700
Subject: [PATCH 2/2] Add a release note
---
clang/docs/ReleaseNotes.md | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index cd7f40aec112d..1061742966f59 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -551,6 +551,8 @@ features cannot lower the translation-unit ABI level;
- Fixed an assertion failure in `-extract-api` when a documentation comment
contains invalid UTF-8. (#GH212393)
- Fixed a crash when generating fake uses for parameters of bodyless destructors with `-fextend-variable-liveness`.
+- Fixed an assertion failure when instantiating a block that captures
+ `this` via a member access through a dependent base class.
### OpenACC Specific Changes
More information about the cfe-commits
mailing list