diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -630,6 +630,9 @@ - Allow abstract parameter and return types in functions that are either deleted or not defined. (`#63012 `_) +- Fix handling of using-declarations in the init statements of for + loop declarations. + (`#63627 `_) Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp --- a/clang/lib/Parse/ParseStmt.cpp +++ b/clang/lib/Parse/ParseStmt.cpp @@ -2054,15 +2054,15 @@ 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 --- /dev/null +++ b/clang/test/SemaCXX/cxx23-init-statement.cpp @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -fsyntax-only -verify=expected -std=c++23 -Wall %s + +namespace GH63627 { +template +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 +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(); + err(); // expected-note {{in instantiation of function template specialization 'GH63627::err'}} +} + +}