Skip to content

Commit 2b9ad0f

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

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8492,12 +8492,12 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
84928492
DeclContext *NewDC = D->getDeclContext();
84938493

84948494
if (FieldDecl *FD = dyn_cast<FieldDecl>(ShadowedDecl)) {
8495-
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC)) {
8495+
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(getFunctionLevelDeclContext())) {
84968496
// Fields are not shadowed by variables in C++ static methods.
84978497
if (MD->isStatic())
84988498
return;
84998499

8500-
if (!MD->getParent()->isLambda() && MD->isExplicitObjectMemberFunction())
8500+
if (MD->isExplicitObjectMemberFunction())
85018501
return;
85028502
}
85038503
// Fields shadowed by constructor parameters are a special case. Usually

clang/test/SemaCXX/cxx2b-warn-shadow.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,29 @@ struct Foo {
1111
}
1212
};
1313
} // namespace GH95707
14+
15+
namespace GH163731 {
16+
struct S1 {
17+
int a;
18+
void m(this S1 &self) {
19+
auto lambda = [](int a) { return a; };
20+
}
21+
};
22+
23+
struct S2 {
24+
int a;
25+
void m(this S2 &self) {
26+
int a = 1; // expected-note {{previous declaration is here}}
27+
auto lambda = [](int a) { // expected-warning {{declaration shadows a local variable}}
28+
return a;
29+
};
30+
}
31+
};
32+
33+
struct S3 {
34+
int a;
35+
void m(this S3 &self) {
36+
auto lambda = [self](int a) { return a + self.a; };
37+
}
38+
};
39+
}

0 commit comments

Comments
 (0)