Reland "[Clang][Sema] Fix invalid redefinition error in if/switch/for statement"

This reverts commit 9f075c3d84.
The broken build has alreasy been fixed in D124012, so reland it now.
Signed-off-by: Jun Zhang <jun@junz.org>
This commit is contained in:
Jun Zhang 2022-04-21 01:17:47 +08:00
parent 39ee23ed5a
commit 9c069374ce
No known key found for this signature in database
GPG Key ID: E19904830B621534
3 changed files with 20 additions and 1 deletions

View File

@ -119,6 +119,10 @@ Bug Fixes
This fixes Issue `Issue 52802 <https://github.com/llvm/llvm-project/issues/52802>`_.
- Unknown type attributes with a ``[[]]`` spelling are no longer diagnosed twice.
This fixes Issue `Issue 54817 <https://github.com/llvm/llvm-project/issues/54817>`_.
- Clang should no longer incorrectly diagnose a variable declaration inside of
a lambda expression that shares the name of a variable in a containing
if/while/for/switch init statement as a redeclaration.
This fixes `Issue 54913 <https://github.com/llvm/llvm-project/issues/54913>`_.
Improvements to Clang's diagnostics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -123,7 +123,7 @@ bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S,
assert(S->getParent() && "No TUScope?");
// If the current decl is in a lambda, we shouldn't consider this is a
// redefinition as lambda has its own scope.
if (S->getParent()->isControlScope()) {
if (S->getParent()->isControlScope() && !S->isFunctionScope()) {
S = S->getParent();
if (S->isDeclScope(D))
return true;

View File

@ -90,3 +90,18 @@ void test_constexpr_init_stmt() {
static_assert(constexpr_switch_init(-2) == 0, "");
static_assert(constexpr_switch_init(-5) == -1, "");
}
int test_lambda_init() {
if (int x = []() {int x = 42; return x; }(); x) {
};
switch (int y = []() {int y = 42; return y; }(); y) {
case 42:
return 1;
}
for (int x = [] { int x = 0; return x; }();;)
;
return 0;
}