Skip to content

Commit 9c06937

Browse files
committed
Reland "[Clang][Sema] Fix invalid redefinition error in if/switch/for statement"
This reverts commit 9f075c3. The broken build has alreasy been fixed in D124012, so reland it now. Signed-off-by: Jun Zhang <jun@junz.org>
1 parent 39ee23e commit 9c06937

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ Bug Fixes
119119
This fixes Issue `Issue 52802 <https://github.com/llvm/llvm-project/issues/52802>`_.
120120
- Unknown type attributes with a ``[[]]`` spelling are no longer diagnosed twice.
121121
This fixes Issue `Issue 54817 <https://github.com/llvm/llvm-project/issues/54817>`_.
122+
- Clang should no longer incorrectly diagnose a variable declaration inside of
123+
a lambda expression that shares the name of a variable in a containing
124+
if/while/for/switch init statement as a redeclaration.
125+
This fixes `Issue 54913 <https://github.com/llvm/llvm-project/issues/54913>`_.
122126

123127
Improvements to Clang's diagnostics
124128
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/IdentifierResolver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S,
123123
assert(S->getParent() && "No TUScope?");
124124
// If the current decl is in a lambda, we shouldn't consider this is a
125125
// redefinition as lambda has its own scope.
126-
if (S->getParent()->isControlScope()) {
126+
if (S->getParent()->isControlScope() && !S->isFunctionScope()) {
127127
S = S->getParent();
128128
if (S->isDeclScope(D))
129129
return true;

clang/test/SemaCXX/cxx1z-init-statement.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,18 @@ void test_constexpr_init_stmt() {
9090
static_assert(constexpr_switch_init(-2) == 0, "");
9191
static_assert(constexpr_switch_init(-5) == -1, "");
9292
}
93+
94+
int test_lambda_init() {
95+
if (int x = []() {int x = 42; return x; }(); x) {
96+
};
97+
98+
switch (int y = []() {int y = 42; return y; }(); y) {
99+
case 42:
100+
return 1;
101+
}
102+
103+
for (int x = [] { int x = 0; return x; }();;)
104+
;
105+
106+
return 0;
107+
}

0 commit comments

Comments
 (0)