r295698 - When deducing an array bound from the length of an initializer list, don't
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 20 23:22:31 PST 2017
Author: rsmith
Date: Tue Feb 21 01:22:31 2017
New Revision: 295698
URL: http://llvm.org/viewvc/llvm-project?rev=295698&view=rev
Log:
When deducing an array bound from the length of an initializer list, don't
assume the bound has a non-dependent integral type.
Modified:
cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp
cfe/trunk/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp
cfe/trunk/test/SemaTemplate/deduction.cpp
Modified: cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp?rev=295698&r1=295697&r2=295698&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateDeduction.cpp Tue Feb 21 01:22:31 2017
@@ -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<typename ...T, T ...V> 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 Ded
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;
}
Modified: cfe/trunk/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp?rev=295698&r1=295697&r2=295698&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp (original)
+++ cfe/trunk/test/SemaCXX/cxx1z-class-template-argument-deduction.cpp Tue Feb 21 01:22:31 2017
@@ -194,11 +194,9 @@ namespace transform_params {
A a(qn, qn); // expected-error {{no matching constructor for initialization of 'transform_params::A<int, 12, Q, &transform_params::n>'}}
static_assert(a.v == 12);
- // FIXME: This causes a crash right now (not class template deduction related).
-#if 0
- template<typename ...T> struct B {
- template<T ...V> B(T (&...p)[V]);
+ // FIXME: This should be accepted.
+ template<typename ...T> struct B { // expected-note {{candidate}}
+ template<T ...V> 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}}
}
Modified: cfe/trunk/test/SemaTemplate/deduction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/deduction.cpp?rev=295698&r1=295697&r2=295698&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/deduction.cpp (original)
+++ cfe/trunk/test/SemaTemplate/deduction.cpp Tue Feb 21 01:22:31 2017
@@ -494,3 +494,45 @@ namespace dependent_template_template_pa
// 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<typename T, T V> void a(const int (&)[V]) {
+ static_assert(is_same<T, decltype(sizeof(0))>::value, "");
+ static_assert(V == 3, "");
+ }
+ template<typename T, T V> void b(const T (&)[V]) {
+ static_assert(is_same<T, int>::value, "");
+ static_assert(V == 3, "");
+ }
+ template<typename T, T V> void c(const T (&)[V]) {
+ static_assert(is_same<T, decltype(sizeof(0))>::value, "");
+ static_assert(V == 3, "");
+ }
+ void d() {
+ a({1, 2, 3});
+#if __cplusplus <= 201402L
+ // expected-error at -2 {{no match}} expected-note at -15 {{couldn't infer template argument 'T'}}
+#endif
+ b({1, 2, 3});
+ c({{}, {}, {}});
+#if __cplusplus <= 201402L
+ // expected-error at -2 {{no match}} expected-note at -12 {{couldn't infer template argument 'T'}}
+#endif
+ }
+
+ template<typename ...T> struct X;
+ template<int ...T> struct Y;
+ template<typename ...T, T ...V> void f(const T (&...p)[V]) {
+ static_assert(is_same<X<T...>, X<int, char, char>>::value, "");
+ static_assert(is_same<Y<V...>, Y<3, 2, 4>>::value, "");
+ }
+ template<typename ...T, T ...V> void g(const T (&...p)[V]) { // expected-note {{deduced incomplete pack}}
+ static_assert(is_same<X<T...>, X<int, decltype(sizeof(0))>>::value, "");
+ static_assert(is_same<Y<V...>, 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}}
+ }
+}
More information about the cfe-commits
mailing list