From: Richard Smith Date: Tue, 21 Feb 2017 07:22:31 +0000 (+0000) Subject: When deducing an array bound from the length of an initializer list, don't X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c6011fab81a4f72fc6a35c3b0b3d88fa57822a96;p=clang When deducing an array bound from the length of an initializer list, don't assume the bound has a non-dependent integral type. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@295698 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaTemplateDeduction.cpp b/lib/Sema/SemaTemplateDeduction.cpp index 6c3a618fbb..32e195b5b9 100644 --- a/lib/Sema/SemaTemplateDeduction.cpp +++ b/lib/Sema/SemaTemplateDeduction.cpp @@ -730,6 +730,11 @@ public: std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); NewPack = DeducedTemplateArgument( TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), + // FIXME: This is wrong, it's possible that some pack elements are + // deduced from an array bound and others are not: + // template void g(const T (&...p)[V]); + // g({1, 2, 3}, {{}, {}}); + // ... should deduce T = {int, size_t (from array bound)}. Pack.New[0].wasDeducedFromArrayBound()); } @@ -3353,10 +3358,12 @@ static Sema::TemplateDeductionResult DeduceFromInitializerList( getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { // We can perform template argument deduction for the given non-type // template parameter. - llvm::APInt Size(S.Context.getIntWidth(NTTP->getType()), - ILE->getNumInits()); + // C++ [temp.deduct.type]p13: + // The type of N in the type T[N] is std::size_t. + QualType T = S.Context.getSizeType(); + llvm::APInt Size(S.Context.getIntWidth(T), ILE->getNumInits()); if (auto Result = DeduceNonTypeTemplateArgument( - S, TemplateParams, NTTP, llvm::APSInt(Size), NTTP->getType(), + S, TemplateParams, NTTP, llvm::APSInt(Size), T, /*ArrayBound=*/true, Info, Deduced)) return Result; } diff --git a/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp b/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp index 4afbd2d7c1..8c96fb4fd0 100644 --- a/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp +++ b/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp @@ -194,11 +194,9 @@ namespace transform_params { A a(qn, qn); // expected-error {{no matching constructor for initialization of 'transform_params::A'}} static_assert(a.v == 12); - // FIXME: This causes a crash right now (not class template deduction related). -#if 0 - template struct B { - template B(T (&...p)[V]); + // FIXME: This should be accepted. + template struct B { // expected-note {{candidate}} + template B(const T (&...p)[V]); // expected-note {{substitution failure}} }; - B b({1, 2, 3}, {"foo", "bar"}, {'x', 'y', 'z', 'w'}); -#endif + B b({1, 2, 3}, {"foo", "bar"}, {'x', 'y', 'z', 'w'}); // expected-error {{no viable constructor or deduction guide}} } diff --git a/test/SemaTemplate/deduction.cpp b/test/SemaTemplate/deduction.cpp index a1180f0988..216b8347be 100644 --- a/test/SemaTemplate/deduction.cpp +++ b/test/SemaTemplate/deduction.cpp @@ -494,3 +494,45 @@ namespace dependent_template_template_param_non_type_param_type { // FIXME: This should be accepted, but we somehow fail to deduce W. A<0> a(qn); // expected-error {{no matching constructor for initialization}} } + +namespace dependent_list_deduction { + template void a(const int (&)[V]) { + static_assert(is_same::value, ""); + static_assert(V == 3, ""); + } + template void b(const T (&)[V]) { + static_assert(is_same::value, ""); + static_assert(V == 3, ""); + } + template void c(const T (&)[V]) { + static_assert(is_same::value, ""); + static_assert(V == 3, ""); + } + void d() { + a({1, 2, 3}); +#if __cplusplus <= 201402L + // expected-error@-2 {{no match}} expected-note@-15 {{couldn't infer template argument 'T'}} +#endif + b({1, 2, 3}); + c({{}, {}, {}}); +#if __cplusplus <= 201402L + // expected-error@-2 {{no match}} expected-note@-12 {{couldn't infer template argument 'T'}} +#endif + } + + template struct X; + template struct Y; + template void f(const T (&...p)[V]) { + static_assert(is_same, X>::value, ""); + static_assert(is_same, Y<3, 2, 4>>::value, ""); + } + template void g(const T (&...p)[V]) { // expected-note {{deduced incomplete pack}} + static_assert(is_same, X>::value, ""); + static_assert(is_same, Y<2, 3>>::value, ""); + } + void h() { + f({1, 2, 3}, {'a', 'b'}, "foo"); + // FIXME: Deduction in this case should succeed. + g({1, 2}, {{}, {}, {}}); // expected-error {{no match}} + } +}