Skip to content

Commit

Permalink
[Clang] Fix handling of placeholder variables name in init captures (#…
Browse files Browse the repository at this point in the history
…107055)

We were incorrectly not deduplicating results when looking up `_` which,
for a lambda init capture, would result in an ambiguous lookup.

The same bug caused some diagnostic notes to be emitted twice.

Fixes #107024
  • Loading branch information
cor3ntin committed Sep 12, 2024
1 parent 327ca6c commit 8290ce0
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@ Bug Fixes to C++ Support
- Fixed a crash-on-invalid bug involving extraneous template parameter with concept substitution. (#GH73885)
- Fixed assertion failure by skipping the analysis of an invalid field declaration. (#GH99868)
- Fix an issue with dependent source location expressions (#GH106428), (#GH81155), (#GH80210), (#GH85373)
- Fix handling of ``_`` as the name of a lambda's init capture variable. (#GH107024)


Bug Fixes to AST Handling
Expand Down
1 change: 0 additions & 1 deletion clang/lib/Sema/SemaLambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,6 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,

if (C->Init.isUsable()) {
addInitCapture(LSI, cast<VarDecl>(Var), C->Kind == LCK_ByRef);
PushOnScopeChains(Var, CurScope, false);
} else {
TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef
: TryCapture_ExplicitByVal;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ void LookupResult::resolveKind() {

// For non-type declarations, check for a prior lookup result naming this
// canonical declaration.
if (!D->isPlaceholderVar(getSema().getLangOpts()) && !ExistingI) {
if (!ExistingI) {
auto UniqueResult = Unique.insert(std::make_pair(D, I));
if (!UniqueResult.second) {
// We've seen this entity before.
Expand Down
6 changes: 4 additions & 2 deletions clang/test/SemaCXX/cxx2c-placeholder-vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ void f() {

void lambda() {
(void)[_ = 0, _ = 1] { // expected-warning {{placeholder variables are incompatible with C++ standards before C++2c}} \
// expected-note 4{{placeholder declared here}}
// expected-note 2{{placeholder declared here}}
(void)_++; // expected-error {{ambiguous reference to placeholder '_', which is defined multiple times}}
};

{
int _ = 12;
(void)[_ = 0]{}; // no warning (different scope)
(void)[_ = 0]{ return _;}; // no warning (different scope)
}

auto GH107024 = [_ = 42]() { return _; }();
}

namespace global_var {
Expand Down

0 comments on commit 8290ce0

Please sign in to comment.