Skip to content

Commit 316236b

Browse files
authored
[Clang] fix false-positive lambda shadow diagnostics in explicit object member functions (#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 } }; ```
1 parent 4cd17ee commit 316236b

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ Bug Fixes in This Version
447447
- Fixed a failed assertion with empty filename in ``#embed`` directive. (#GH162951)
448448
- Fixed a crash triggered by unterminated ``__has_embed``. (#GH162953)
449449
- Accept empty enumerations in MSVC-compatible C mode. (#GH114402)
450+
- Fixed false-positive shadow diagnostics for lambdas in explicit object member functions. (#GH163731)
450451

451452
Bug Fixes to Compiler Builtins
452453
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDecl.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8492,12 +8492,11 @@ 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)) {
8496-
// Fields are not shadowed by variables in C++ static methods.
8497-
if (MD->isStatic())
8498-
return;
8499-
8500-
if (!MD->getParent()->isLambda() && MD->isExplicitObjectMemberFunction())
8495+
if (const auto *MD =
8496+
dyn_cast<CXXMethodDecl>(getFunctionLevelDeclContext())) {
8497+
// Fields aren't shadowed in C++ static members or in member functions
8498+
// with an explicit object parameter.
8499+
if (MD->isStatic() || MD->isExplicitObjectMemberFunction())
85018500
return;
85028501
}
85038502
// 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)