Skip to content

Commit 9adcc63

Browse files
committed
[Clang] fix false-positive lambda shadow diagnostics in explicit-object member functions
1 parent bd0efca commit 9adcc63

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ Bug Fixes in This Version
432432
- Fixed a failed assertion with empty filename arguments in ``__has_embed``. (#GH159898)
433433
- Fixed a failed assertion with empty filename in ``#embed`` directive. (#GH162951)
434434
- Fixed a crash triggered by unterminated ``__has_embed``. (#GH162953)
435+
- Fixed false-positive shadow diagnostics for lambdas in explicit object member functions. (#GH163731)
435436

436437
Bug Fixes to Compiler Builtins
437438
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8640,6 +8640,11 @@ void Sema::DiagnoseShadowingLambdaDecls(const LambdaScopeInfo *LSI) {
86408640
<< Shadow.VD->getDeclName() << /*explicitly*/ 0;
86418641
Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
86428642
} else if (isa<FieldDecl>(ShadowedDecl)) {
8643+
if (CXXMethodDecl *MD =
8644+
dyn_cast<CXXMethodDecl>(getFunctionLevelDeclContext()))
8645+
if (MD->isExplicitObjectMemberFunction())
8646+
continue;
8647+
86438648
Diag(Shadow.VD->getLocation(),
86448649
LSI->isCXXThisCaptured() ? diag::warn_decl_shadow
86458650
: diag::warn_decl_shadow_uncaptured_local)

clang/test/SemaCXX/warn-shadow-in-lambdas.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// RUN: %clang_cc1 -std=c++14 -verify=expected,cxx14 -fsyntax-only -Wshadow-all %s
44
// RUN: %clang_cc1 -std=c++17 -verify -fsyntax-only -Wshadow-all %s
55
// RUN: %clang_cc1 -std=c++20 -verify -fsyntax-only -Wshadow-all %s
6+
// RUN: %clang_cc1 -std=c++23 -verify -fsyntax-only -Wshadow-all %s
67

78
void foo(int param) { // expected-note 1+ {{previous declaration is here}}
89
int var = 0; // expected-note 1+ {{previous declaration is here}}
@@ -268,5 +269,32 @@ int foo() {
268269
}();
269270
#endif
270271
}
272+
}
273+
274+
#if __cplusplus >= 202302L
275+
namespace GH163731 {
276+
struct S1 {
277+
int a;
278+
void m(this S1 &self) {
279+
auto lambda = [](int a) { return a; };
280+
}
281+
};
282+
283+
struct S2 {
284+
int a;
285+
void m(this S2 &self) {
286+
int a = 1; // expected-note {{previous declaration is here}}
287+
auto lambda = [](int a) { // expected-warning {{declaration shadows a local variable}}
288+
return a;
289+
};
290+
}
291+
};
271292

293+
struct S3 {
294+
int a;
295+
void m(this S3 &self) {
296+
auto lambda = [self](int a) { return a + self.a; };
297+
}
298+
};
272299
}
300+
#endif

0 commit comments

Comments
 (0)