[PATCH] D123840: [Clang][Sema] Fix invalid redefinition error in if/switch/for statement

Jun Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 15 06:50:44 PDT 2022


junaire updated this revision to Diff 423086.
junaire marked an inline comment as done.
junaire added a comment.

Update release note.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D123840/new/

https://reviews.llvm.org/D123840

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/IdentifierResolver.cpp
  clang/test/SemaCXX/cxx1z-init-statement.cpp


Index: clang/test/SemaCXX/cxx1z-init-statement.cpp
===================================================================
--- clang/test/SemaCXX/cxx1z-init-statement.cpp
+++ clang/test/SemaCXX/cxx1z-init-statement.cpp
@@ -90,3 +90,18 @@
   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;
+}
Index: clang/lib/Sema/IdentifierResolver.cpp
===================================================================
--- clang/lib/Sema/IdentifierResolver.cpp
+++ clang/lib/Sema/IdentifierResolver.cpp
@@ -121,7 +121,10 @@
       // of the controlled statement.
       //
       assert(S->getParent() && "No TUScope?");
-      if (S->getParent()->getFlags() & Scope::ControlScope) {
+      // 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()->getFlags() & Scope::ControlScope &&
+          !S->isFunctionScope()) {
         S = S->getParent();
         if (S->isDeclScope(D))
           return true;
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -119,6 +119,10 @@
   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
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123840.423086.patch
Type: text/x-patch
Size: 2109 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220415/ac4dd23f/attachment-0001.bin>


More information about the cfe-commits mailing list