https://github.com/zwuis updated https://github.com/llvm/llvm-project/pull/124793
>From 16596add29b63ee0282e026dec7b1d5946863113 Mon Sep 17 00:00:00 2001 From: Yanzuo Liu <zw...@outlook.com> Date: Wed, 29 Jan 2025 00:38:15 +0800 Subject: [PATCH 1/4] Fix wrong initialization kind --- clang/lib/Sema/SemaInit.cpp | 5 +++-- clang/test/SemaCXX/cxx1z-decomposition.cpp | 21 ++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index b95cbbf42220568..5552fce55f13106 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -4861,8 +4861,9 @@ static void TryListInitialization(Sema &S, S.Context.hasSameUnqualifiedType(SubInit[0]->getType(), DestType) && "Deduced to other type?"); TryArrayCopy(S, - InitializationKind::CreateCopy(Kind.getLocation(), - InitList->getLBraceLoc()), + InitializationKind::CreateDirect(Kind.getLocation(), + InitList->getLBraceLoc(), + InitList->getRBraceLoc()), Entity, SubInit[0], DestType, Sequence, TreatUnavailableAsInvalid); if (Sequence) diff --git a/clang/test/SemaCXX/cxx1z-decomposition.cpp b/clang/test/SemaCXX/cxx1z-decomposition.cpp index a8914fe4e9cd823..b3d98e44990f610 100644 --- a/clang/test/SemaCXX/cxx1z-decomposition.cpp +++ b/clang/test/SemaCXX/cxx1z-decomposition.cpp @@ -200,38 +200,37 @@ namespace lambdas { namespace by_value_array_copy { struct explicit_copy { - explicit_copy() = default; // expected-note 2{{candidate constructor not viable: requires 0 arguments, but 1 was provided}} - explicit explicit_copy(const explicit_copy&) = default; // expected-note 2{{explicit constructor is not a candidate}} + explicit_copy() = default; // expected-note {{candidate constructor not viable: requires 0 arguments, but 1 was provided}} + explicit explicit_copy(const explicit_copy&) = default; // expected-note {{explicit constructor is not a candidate}} }; constexpr int direct_initialization_for_elements() { explicit_copy ec_arr[2]; auto [a1, b1](ec_arr); + auto [a2, b2]{ec_arr}; int arr[3]{1, 2, 3}; - auto [a2, b2, c2](arr); + auto [a3, b3, c3](arr); + auto [a4, b4, c4]{arr}; // GH31813 arr[0]--; - return a2 + b2 + c2 + arr[0]; + return a3 + b3 + c3 + a4 + b4 + c4 + arr[0]; } - static_assert(direct_initialization_for_elements() == 6); + static_assert(direct_initialization_for_elements() == 12); constexpr int copy_initialization_for_elements() { int arr[2]{4, 5}; auto [a1, b1] = arr; - auto [a2, b2]{arr}; // GH31813 arr[0] = 0; - return a1 + b1 + a2 + b2 + arr[0]; + return a1 + b1 + arr[0]; } - static_assert(copy_initialization_for_elements() == 18); + static_assert(copy_initialization_for_elements() == 9); void copy_initialization_for_elements_with_explicit_copy_ctor() { explicit_copy ec_arr[2]; auto [a1, b1] = ec_arr; // expected-error {{no matching constructor for initialization of 'explicit_copy[2]'}} - auto [a2, b2]{ec_arr}; // expected-error {{no matching constructor for initialization of 'explicit_copy[2]'}} // Test prvalue using T = explicit_copy[2]; - auto [a3, b3] = T{}; - auto [a4, b4]{T{}}; + auto [a2, b2] = T{}; } } // namespace by_value_array_copy >From ad033c917db457ebe68b4556a482e9ba56b4746d Mon Sep 17 00:00:00 2001 From: Yanzuo Liu <zw...@outlook.com> Date: Wed, 29 Jan 2025 10:28:21 +0800 Subject: [PATCH 2/4] Make tests more clear --- clang/test/SemaCXX/cxx1z-decomposition.cpp | 39 ++++++++++------------ 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/clang/test/SemaCXX/cxx1z-decomposition.cpp b/clang/test/SemaCXX/cxx1z-decomposition.cpp index b3d98e44990f610..95c64bc3b8bff61 100644 --- a/clang/test/SemaCXX/cxx1z-decomposition.cpp +++ b/clang/test/SemaCXX/cxx1z-decomposition.cpp @@ -204,33 +204,28 @@ namespace by_value_array_copy { explicit explicit_copy(const explicit_copy&) = default; // expected-note {{explicit constructor is not a candidate}} }; - constexpr int direct_initialization_for_elements() { - explicit_copy ec_arr[2]; - auto [a1, b1](ec_arr); - auto [a2, b2]{ec_arr}; - - int arr[3]{1, 2, 3}; - auto [a3, b3, c3](arr); - auto [a4, b4, c4]{arr}; // GH31813 - arr[0]--; - return a3 + b3 + c3 + a4 + b4 + c4 + arr[0]; - } - static_assert(direct_initialization_for_elements() == 12); + constexpr int simple_array_elements() { + int arr[2]{1, 2}; + + auto [a1, a2] = arr; + auto [b1, b2](arr); + auto [c1, c2]{arr}; // GH31813 - constexpr int copy_initialization_for_elements() { - int arr[2]{4, 5}; - auto [a1, b1] = arr; arr[0] = 0; - return a1 + b1 + arr[0]; + return arr[0] + a1 + a2 + b1 + b2 + c1 + c2; } - static_assert(copy_initialization_for_elements() == 9); + static_assert(simple_array_elements() == 9); - void copy_initialization_for_elements_with_explicit_copy_ctor() { - explicit_copy ec_arr[2]; - auto [a1, b1] = ec_arr; // expected-error {{no matching constructor for initialization of 'explicit_copy[2]'}} + void explicit_copy_ctor_array_elements() { + explicit_copy ec_arr[1]; + + auto [a] = ec_arr; // expected-error {{no matching constructor for initialization of 'explicit_copy[1]'}} + auto [b](ec_arr); + auto [c]{ec_arr}; // Test prvalue - using T = explicit_copy[2]; - auto [a2, b2] = T{}; + using T = explicit_copy[1]; + auto [d] = T{}; } + } // namespace by_value_array_copy >From 88f702e64396300ec2f07f6960379f6617bcbf0e Mon Sep 17 00:00:00 2001 From: Yanzuo Liu <zw...@outlook.com> Date: Tue, 4 Feb 2025 12:27:57 +0800 Subject: [PATCH 3/4] Add assertion --- clang/lib/Sema/SemaInit.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 5552fce55f13106..916afbbe80e7af4 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -4860,6 +4860,9 @@ static void TryListInitialization(Sema &S, assert( S.Context.hasSameUnqualifiedType(SubInit[0]->getType(), DestType) && "Deduced to other type?"); + assert(Kind.getKind() == clang::InitializationKind::IK_DirectList && + "List-initialize structured bindings but not " + "direct-list-initialization?"); TryArrayCopy(S, InitializationKind::CreateDirect(Kind.getLocation(), InitList->getLBraceLoc(), >From 8d6b58e15ba0153bfdec3a7e6a03c182e257bdfd Mon Sep 17 00:00:00 2001 From: Yanzuo Liu <zw...@outlook.com> Date: Tue, 4 Feb 2025 16:56:10 +0800 Subject: [PATCH 4/4] Add release note --- clang/docs/ReleaseNotes.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index d8b7145986a2afd..0b988fe360345d8 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -1009,6 +1009,8 @@ Bug Fixes to C++ Support - Fix type of expression when calling a template which returns an ``__array_rank`` querying a type depending on a template parameter. Now, such expression can be used with ``static_assert`` and ``constexpr``. (#GH123498) - Correctly determine the implicit constexprness of lambdas in dependent contexts. (#GH97958) (#GH114234) +- The initialization kind of elements of structured bindings + direct-list-initialized from an array is corrected to direct-initialization. Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits