From 3dd62b0dc3d913b76072c4dc0a7c0d172fe9d529 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Wed, 18 Dec 2024 16:22:15 +0800 Subject: [PATCH 1/2] [Clang] Don't assume unexpanded PackExpansions' size when expanding packs --- .../clang/Basic/DiagnosticSemaKinds.td | 4 +- clang/lib/Sema/SemaTemplateVariadic.cpp | 36 +++++++++-- clang/test/SemaTemplate/pack-deduction.cpp | 59 +++++++++++++++++++ 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 811265151fa0d..762e01dbaa857 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -5863,10 +5863,10 @@ def err_pack_expansion_without_parameter_packs : Error< "pack expansion does not contain any unexpanded parameter packs">; def err_pack_expansion_length_conflict : Error< "pack expansion contains parameter packs %0 and %1 that have different " - "lengths (%2 vs. %3)">; + "lengths (%2 vs. %select{|at least }3%4))">; def err_pack_expansion_length_conflict_multilevel : Error< "pack expansion contains parameter pack %0 that has a different " - "length (%1 vs. %2) from outer parameter packs">; + "length (%1 vs. %select{|at least }2%3) from outer parameter packs">; def err_pack_expansion_length_conflict_partial : Error< "pack expansion contains parameter pack %0 that has a different " "length (at least %1 vs. %2) from outer parameter packs">; diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp index 88a21240e1c80..2f1dc3d7efd62 100644 --- a/clang/lib/Sema/SemaTemplateVariadic.cpp +++ b/clang/lib/Sema/SemaTemplateVariadic.cpp @@ -780,7 +780,7 @@ bool Sema::CheckParameterPacksForExpansion( } // Determine the size of this argument pack. - unsigned NewPackSize; + unsigned NewPackSize, PendingPackExpansionSize = 0; if (IsVarDeclPack) { // Figure out whether we're instantiating to an argument pack or not. typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; @@ -808,7 +808,25 @@ bool Sema::CheckParameterPacksForExpansion( } // Determine the size of the argument pack. - NewPackSize = TemplateArgs(Depth, Index).pack_size(); + ArrayRef Pack = + TemplateArgs(Depth, Index).getPackAsArray(); + NewPackSize = Pack.size(); + PendingPackExpansionSize = + llvm::count_if(Pack, [](const TemplateArgument &TA) { + if (!TA.isPackExpansion()) + return false; + + if (TA.getKind() == TemplateArgument::Type) + return !TA.getAsType() + ->getAs() + ->getNumExpansions(); + + if (TA.getKind() == TemplateArgument::Expression) + return !cast(TA.getAsExpr()) + ->getNumExpansions(); + + return !TA.getNumTemplateExpansions(); + }); } // C++0x [temp.arg.explicit]p9: @@ -831,7 +849,7 @@ bool Sema::CheckParameterPacksForExpansion( } if (!NumExpansions) { - // The is the first pack we've seen for which we have an argument. + // This is the first pack we've seen for which we have an argument. // Record it. NumExpansions = NewPackSize; FirstPack.first = Name; @@ -841,17 +859,23 @@ bool Sema::CheckParameterPacksForExpansion( } if (NewPackSize != *NumExpansions) { + unsigned LeastNewPackSize = NewPackSize - PendingPackExpansionSize; + if (PendingPackExpansionSize && LeastNewPackSize <= *NumExpansions) { + ShouldExpand = false; + continue; + } // C++0x [temp.variadic]p5: // All of the parameter packs expanded by a pack expansion shall have // the same number of arguments specified. if (HaveFirstPack) Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict) - << FirstPack.first << Name << *NumExpansions << NewPackSize + << FirstPack.first << Name << *NumExpansions + << (LeastNewPackSize != NewPackSize) << LeastNewPackSize << SourceRange(FirstPack.second) << SourceRange(ParmPack.second); else Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel) - << Name << *NumExpansions << NewPackSize - << SourceRange(ParmPack.second); + << Name << *NumExpansions << (LeastNewPackSize != NewPackSize) + << LeastNewPackSize << SourceRange(ParmPack.second); return true; } } diff --git a/clang/test/SemaTemplate/pack-deduction.cpp b/clang/test/SemaTemplate/pack-deduction.cpp index 28fb127a38644..b3104609994a4 100644 --- a/clang/test/SemaTemplate/pack-deduction.cpp +++ b/clang/test/SemaTemplate/pack-deduction.cpp @@ -199,3 +199,62 @@ constexpr auto baz(Int(T())>... x) -> int { return 1; } static_assert(baz, Int<2>, Int<3>>(Int<10>(), Int<10>(), Int<10>()) == 1, ""); } + +namespace GH17042 { + +template struct X { + template using Y = X; // #GH17042_Y +}; + +template +using any_pairs_list = X::Y; // #any_pairs_list + +template +using any_pairs_list_2 = X::Y<>; +// expected-error@#GH17042_Y {{different length (2 vs. 0)}} \ +// expected-note@-1 {{requested here}} + +template +using any_pairs_list_3 = X::Y; // #any_pairs_list_3 + +template +using any_pairs_list_4 = X::Y; +// expected-error@#GH17042_Y {{different length (2 vs. at least 3)}} \ +// expected-note@-1 {{requested here}} + +static_assert(__is_same(any_pairs_list, X), ""); + +static_assert(!__is_same(any_pairs_list, X), ""); +// expected-error@#GH17042_Y {{different length (2 vs. 3)}} \ +// expected-note@#any_pairs_list {{requested here}} \ +// expected-note@-1 {{requested here}} + +static_assert(__is_same(any_pairs_list_3, X), ""); + +static_assert(!__is_same(any_pairs_list_3, X), ""); +// expected-error@#GH17042_Y {{different length (2 vs. 3)}} \ +// expected-note@#any_pairs_list_3 {{requested here}} \ +// expected-note@-1 {{requested here}} + +namespace TemplateTemplateParameters { +template struct C {}; + +template class... Args1> struct Ttp { + template