-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[Clang] fix false-positive lambda shadow diagnostics in explicit object member functions #165919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@llvm/pr-subscribers-clang Author: Oleksandr T. (a-tarasyuk) ChangesFixes #163731 This PR addresses false-positive shadow diagnostics for lambdas inside explicit object member functions struct S {
int x;
void m(this S &self) {
auto lambda = [](int x) { return x; }; // ok
}
};Full diff: https://github.com/llvm/llvm-project/pull/165919.diff 3 Files Affected:
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
|
9b2073d to
9adcc63
Compare
…ct member functions
e22a06d to
2b9ad0f
Compare
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
AaronBallman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM aside from a nit
Fixes #163731
This PR addresses false-positive shadow diagnostics for lambdas inside explicit object member functions