[clang] [Clang] Fix handling of placeholder variables name in init captures (PR #107055)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 3 02:56:57 PDT 2024
https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/107055
>From 18c11cbf5a3768f03234df2ec2bdd79d5e971f55 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 3 Sep 2024 09:52:55 +0200
Subject: [PATCH 1/2] [Clang] Fix handling of placeholder variables name in
init captures
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
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaLookup.cpp | 2 +-
clang/test/SemaCXX/cxx2c-placeholder-vars.cpp | 6 ++++--
3 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fc940db4813948..af4388eada8e5a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -339,6 +339,7 @@ Bug Fixes to C++ Support
- Template parameter names are considered in the name lookup of out-of-line class template
specialization right before its declaration context. (#GH64082)
- Fixed a constraint comparison bug for friend declarations. (#GH78101)
+- Fix handling of ``_`` as the name of a lambda's init capture variable. (#GH107024)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 7a6a64529f52ec..d3d4bf27ae7283 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -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.
diff --git a/clang/test/SemaCXX/cxx2c-placeholder-vars.cpp b/clang/test/SemaCXX/cxx2c-placeholder-vars.cpp
index 5cf66b48784e91..29ca3b5ef3df72 100644
--- a/clang/test/SemaCXX/cxx2c-placeholder-vars.cpp
+++ b/clang/test/SemaCXX/cxx2c-placeholder-vars.cpp
@@ -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 {
>From bf6e12d50b78cb3abd5e4e5e4a5709ff98069803 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 3 Sep 2024 11:56:42 +0200
Subject: [PATCH 2/2] Remove duplicated PushOnScopeChains
---
clang/lib/Sema/SemaLambda.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index a8461a7f6cf712..15ed8572e60844 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1323,7 +1323,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;
More information about the cfe-commits
mailing list