[clang] 1077a34 - [Clang] Fix handling of using declarations in for loop init statements.

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 5 07:20:59 PDT 2023


Author: Corentin Jabot
Date: 2023-07-05T16:20:53+02:00
New Revision: 1077a343911127452615c6f5441c121de06be6d5

URL: https://github.com/llvm/llvm-project/commit/1077a343911127452615c6f5441c121de06be6d5
DIFF: https://github.com/llvm/llvm-project/commit/1077a343911127452615c6f5441c121de06be6d5.diff

LOG: [Clang] Fix handling of using declarations in for loop init statements.

The type was never saved, and therefore never transformed
in dependent contexts.

Reviewed By: aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D154492

Added: 
    clang/test/SemaCXX/cxx23-init-statement.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Parse/ParseStmt.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 022d51b525c04c..79550f79cc36b5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -635,6 +635,9 @@ Bug Fixes to C++ Support
 - Allow abstract parameter and return types in functions that are
   either deleted or not defined.
   (`#63012 <https://github.com/llvm/llvm-project/issues/63012>`_)
+- Fix handling of using-declarations in the init statements of for
+  loop declarations.
+  (`#63627 <https://github.com/llvm/llvm-project/issues/63627>`_)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 43ff808e296b54..2346470dbdb73d 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -2054,15 +2054,15 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
       Diag(Tok, diag::warn_gcc_variable_decl_in_for_loop);
     }
     DeclGroupPtrTy DG;
+    SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
     if (Tok.is(tok::kw_using)) {
       DG = ParseAliasDeclarationInInitStatement(DeclaratorContext::ForInit,
                                                 attrs);
+      FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
     } else {
       // In C++0x, "for (T NS:a" might not be a typo for ::
       bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
       ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
-
-      SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
       ParsedAttributes DeclSpecAttrs(AttrFactory);
       DG = ParseSimpleDeclaration(
           DeclaratorContext::ForInit, DeclEnd, attrs, DeclSpecAttrs, false,

diff  --git a/clang/test/SemaCXX/cxx23-init-statement.cpp b/clang/test/SemaCXX/cxx23-init-statement.cpp
new file mode 100644
index 00000000000000..bf99a53df15e14
--- /dev/null
+++ b/clang/test/SemaCXX/cxx23-init-statement.cpp
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++23 %s
+
+namespace GH63627 {
+template<class T>
+void ok() {
+  if  (using U = decltype([]{ return 42;}); true) {
+      static_assert(U{}() == 42);
+  }
+  for (using U = decltype([]{ return 42;}); [[maybe_unused]] auto x : "abc") {
+      static_assert(U{}() == 42);
+  }
+  for (using U = decltype([]{ return 42;}); false; ) {
+      static_assert(U{}() == 42);
+  }
+}
+
+template<class T>
+void err() {
+  if  (using U = decltype([]{}.foo); true) {}  // expected-error {{no member named 'foo'}}
+
+  for (using U = decltype([]{}.foo);          // expected-error {{no member named 'foo'}}
+       [[maybe_unused]] auto x : "abc") { }
+
+  for (using U = decltype([]{}.foo);          // expected-error {{no member named 'foo'}}
+       false ; ) { }
+};
+
+void test() {
+  ok<int>();
+  err<int>(); // expected-note {{in instantiation of function template specialization 'GH63627::err<int>'}}
+}
+
+}


        


More information about the cfe-commits mailing list