https://github.com/a-tarasyuk created
https://github.com/llvm/llvm-project/pull/165919
Fixes #163731
---
This PR addresses false-positive shadow diagnostics for lambdas inside explicit
object member functions
```cpp
struct S {
int x;
void m(this S &self) {
auto lambda = [](int x) { return x; }; // ok
}
};
```
>From 9b2073d5b4b6c3654560676f257f4041ada2fb1f Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <[email protected]>
Date: Fri, 31 Oct 2025 22:54:53 +0200
Subject: [PATCH] [Clang] fixes false-positive lambda shadow diagnostics in
explicit-object member functions
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaDecl.cpp | 5 ++++
clang/test/SemaCXX/warn-shadow-in-lambdas.cpp | 28 +++++++++++++++++++
3 files changed, 34 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 92fc9381a5868..593e117777314 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -432,6 +432,7 @@ Bug Fixes in This Version
- Fixed a failed assertion with empty filename arguments in ``__has_embed``.
(#GH159898)
- Fixed a failed assertion with empty filename in ``#embed`` directive.
(#GH162951)
- Fixed a crash triggered by unterminated ``__has_embed``. (#GH162953)
+- Fixed false-positive shadow diagnostics for lambdas in explicit object
member functions. (#GH163731)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index fc3aabf5741ca..96aea9877719e 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8640,6 +8640,11 @@ void Sema::DiagnoseShadowingLambdaDecls(const
LambdaScopeInfo *LSI) {
<< Shadow.VD->getDeclName() << /*explicitly*/ 0;
Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
} else if (isa<FieldDecl>(ShadowedDecl)) {
+ if (CXXMethodDecl *MD =
+ dyn_cast<CXXMethodDecl>(getFunctionLevelDeclContext()))
+ if (MD->isExplicitObjectMemberFunction())
+ continue;
+
Diag(Shadow.VD->getLocation(),
LSI->isCXXThisCaptured() ? diag::warn_decl_shadow
: diag::warn_decl_shadow_uncaptured_local)
diff --git a/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
b/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
index 0042ef035c84c..47653a9c39d15 100644
--- a/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
+++ b/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
@@ -3,6 +3,7 @@
// RUN: %clang_cc1 -std=c++14 -verify=expected,cxx14 -fsyntax-only
-Wshadow-all %s
// RUN: %clang_cc1 -std=c++17 -verify -fsyntax-only -Wshadow-all %s
// RUN: %clang_cc1 -std=c++20 -verify -fsyntax-only -Wshadow-all %s
+// RUN: %clang_cc1 -std=c++23 -verify -fsyntax-only -Wshadow-all %s
void foo(int param) { // expected-note 1+ {{previous declaration is here}}
int var = 0; // expected-note 1+ {{previous declaration is here}}
@@ -268,5 +269,32 @@ int foo() {
}();
#endif
}
+}
+
+#if __cplusplus >= 202302L
+namespace GH163731 {
+struct S1 {
+ int a;
+ void m(this S1 &self) {
+ auto lambda = [](int a) { return a; };
+ }
+};
+
+struct S2 {
+ int a;
+ void m(this S2 &self) {
+ int a = 1; // expected-note {{previous declaration is here}}
+ auto lambda = [](int a) { // expected-warning {{declaration shadows a
local variable}}
+ return a;
+ };
+ }
+};
+struct S3 {
+ int a;
+ void m(this S3 &self) {
+ auto lambda = [self](int a) { return a + self.a; };
+ }
+};
}
+#endif
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits