1 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -std=c++11 -fblocks -fms-extensions -fsyntax-only -verify %s
2 
3 template<typename T, typename U> struct pair;
4 template<typename ...> struct tuple;
5 
6 // A parameter pack whose name appears within the pattern of a pack
7 // expansion is expanded by that pack expansion. An appearance of the
8 // name of a parameter pack is only expanded by the innermost
9 // enclosing pack expansion. The pattern of a pack expansion shall
10 // name one or more parameter packs that are not expanded by a nested
11 // pack expansion.
12 template<typename... Types>
13 struct Expansion {
14   typedef pair<Types..., int> expand_with_pacs; // okay
15   typedef pair<Types, int...> expand_no_packs;  // expected-error{{pack expansion does not contain any unexpanded parameter packs}}
16   typedef pair<pair<Types..., int>..., int> expand_with_expanded_nested; // expected-error{{pack expansion does not contain any unexpanded parameter packs}}
17 };
18 
19 // All of the parameter packs expanded by a pack expansion shall have
20 // the same number of arguments specified.
21 template<typename ...Types>
22 struct ExpansionLengthMismatch {
23   template<typename ...OtherTypes>
24   struct Inner {
25     typedef tuple<pair<Types, OtherTypes>...> type; // expected-error{{pack expansion contains parameter packs 'Types' and 'OtherTypes' that have different lengths (3 vs. 2)}}
26   };
27 };
28 
29 ExpansionLengthMismatch<int, long>::Inner<unsigned int, unsigned long>::type
30   *il_pairs;
31 tuple<pair<int, unsigned int>, pair<long, unsigned long> >*il_pairs_2 = il_pairs;
32 
33 ExpansionLengthMismatch<short, int, long>::Inner<unsigned int, unsigned long>::type // expected-note{{in instantiation of template class 'ExpansionLengthMismatch<short, int, long>::Inner<unsigned int, unsigned long>' requested here}}
34   *il_pairs_bad;
35 
36 
37 // An appearance of a name of a parameter pack that is not expanded is
38 // ill-formed.
39 
40 // Test for unexpanded parameter packs in each of the type nodes.
41 template<typename T, int N, typename ... Types>
42 struct TestPPName
43   : public Types, public T  // expected-error{{base type contains unexpanded parameter pack 'Types'}}
44 {
45   // BuiltinType is uninteresting
46   // FIXME: ComplexType is uninteresting?
47   // PointerType
48   typedef Types *types_pointer; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
49 
50   // BlockPointerType
51   typedef Types (^block_pointer_1)(int); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
52   typedef int (^block_pointer_2)(Types); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
53 
54   // LValueReferenceType
55   typedef Types &lvalue_ref; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
56 
57   // RValueReferenceType
58   typedef Types &&rvalue_ref; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
59 
60   // MemberPointerType
61   typedef Types TestPPName::* member_pointer_1; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
62   typedef int Types::*member_pointer_2; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
63 
64   // ConstantArrayType
65   typedef Types constant_array[17]; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
66 
67   // IncompleteArrayType
68   typedef Types incomplete_array[]; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
69 
70   // VariableArrayType
fTestPPName71   void f(int i) {
72     Types variable_array[i]; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
73   }
74 
75   // DependentSizedArrayType
76   typedef Types dependent_sized_array[N]; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
77 
78   // DependentSizedExtVectorType
79   typedef Types dependent_sized_ext_vector __attribute__((ext_vector_type(N))); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
80 
81   // VectorType is uninteresting
82 
83   // ExtVectorType
84   typedef Types ext_vector __attribute__((ext_vector_type(4))); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
85 
86   // FunctionProtoType
87   typedef Types (function_type_1)(int); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
88   typedef int (function_type_2)(Types); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
89 
90   // FunctionNoProtoType is uninteresting
91   // UnresolvedUsingType is uninteresting
92   // ParenType is uninteresting
93   // TypedefType is uninteresting
94 
95   // TypeOfExprType
96   typedef __typeof__((static_cast<Types>(0))) typeof_expr; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
97 
98   // TypeOfType
99   typedef __typeof__(Types) typeof_type;  // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
100 
101   // DecltypeType
102   typedef decltype((static_cast<Types>(0))) typeof_expr; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
103 
104   // RecordType is uninteresting
105   // EnumType is uninteresting
106   // ElaboratedType is uninteresting
107 
108   // TemplateTypeParmType
109   typedef Types template_type_parm; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
110 
111   // SubstTemplateTypeParmType is uninteresting
112 
113   // TemplateSpecializationType
114   typedef pair<Types, int> template_specialization; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
115 
116   // InjectedClassName is uninteresting.
117 
118   // DependentNameType
119   typedef typename Types::type dependent_name; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
120 
121   // DependentTemplateSpecializationType
122   typedef typename Types::template apply<int> dependent_name_1; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
123   typedef typename T::template apply<Types> dependent_name_2; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
124 
125   // ObjCObjectType is uninteresting
126   // ObjCInterfaceType is uninteresting
127   // ObjCObjectPointerType is uninteresting
128 };
129 
130 // FIXME: Test for unexpanded parameter packs in each of the expression nodes.
131 template<int ...Values>
test_unexpanded_in_exprs()132 void test_unexpanded_in_exprs() {
133   // PredefinedExpr is uninteresting
134   // DeclRefExpr
135   Values; // expected-error{{expression contains unexpanded parameter pack 'Values'}}
136   // IntegerLiteral is uninteresting
137   // FloatingLiteral is uninteresting
138   // ImaginaryLiteral is uninteresting
139   // StringLiteral is uninteresting
140   // CharacterLiteral is uninteresting
141   (Values); // expected-error{{expression contains unexpanded parameter pack 'Values'}}
142   // UnaryOperator
143   -Values; // expected-error{{expression contains unexpanded parameter pack 'Values'}}
144   // OffsetOfExpr
145   struct OffsetMe {
146     int array[17];
147   };
148   __builtin_offsetof(OffsetMe, array[Values]); // expected-error{{expression contains unexpanded parameter pack 'Values'}}
149   // FIXME: continue this...
150 }
151 
152 template<typename ... Types>
TestPPNameFunc(int i)153 void TestPPNameFunc(int i) {
154   f(static_cast<Types>(i)); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
155 }
156 
157 template<typename T, template<class> class ...Meta>
158 struct TestUnexpandedTTP {
159   typedef tuple<typename Meta<T>::type> type; // expected-error{{declaration type contains unexpanded parameter pack 'Meta'}}
160 };
161 
162 // Test for unexpanded parameter packs in declarations.
163 template<typename T, typename... Types>
164 // FIXME: this should test that the diagnostic reads "type contains..."
165 struct alignas(Types) TestUnexpandedDecls : T{ // expected-error{{expression contains unexpanded parameter pack 'Types'}}
166   void member_function(Types);  // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
167   void member_function () throw(Types); // expected-error{{exception type contains unexpanded parameter pack 'Types'}}
168   void member_function2() noexcept(Types()); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
169   operator Types() const; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
170   Types data_member;  // expected-error{{data member type contains unexpanded parameter pack 'Types'}}
171   static Types static_data_member; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
172   unsigned bit_field : static_cast<Types>(0);  // expected-error{{bit-field size contains unexpanded parameter pack 'Types'}}
173   static_assert(static_cast<Types>(0), "Boom"); // expected-error{{static assertion contains unexpanded parameter pack 'Types'}}
174 
175   enum E0 : Types {  // expected-error{{fixed underlying type contains unexpanded parameter pack 'Types'}}
176     EnumValue = static_cast<Types>(0) // expected-error{{enumerator value contains unexpanded parameter pack 'Types'}}
177   };
178 
179   using typename Types::type; // expected-error{{using declaration contains unexpanded parameter pack 'Types'}}
180   using Types::value; // expected-error{{using declaration contains unexpanded parameter pack 'Types'}}
181   using T::operator Types; // expected-error{{using declaration contains unexpanded parameter pack 'Types'}}
182 
183   friend class Types::foo; // expected-error{{friend declaration contains unexpanded parameter pack 'Types'}}
184   friend void friend_func(Types); // expected-error{{friend declaration contains unexpanded parameter pack 'Types'}}
185   friend void Types::other_friend_func(int); // expected-error{{friend declaration contains unexpanded parameter pack 'Types'}}
186 
test_initializersTestUnexpandedDecls187   void test_initializers() {
188     T copy_init = static_cast<Types>(0); // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
189     T direct_init(0, static_cast<Types>(0)); // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
190     T list_init = { static_cast<Types>(0) }; // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
191   }
192 
193   T in_class_member_init = static_cast<Types>(0); // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
TestUnexpandedDeclsTestUnexpandedDecls194   TestUnexpandedDecls() :
195     Types(static_cast<Types>(0)), // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
196     Types(static_cast<Types>(0))...,
197     in_class_member_init(static_cast<Types>(0)) {} // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
198 
199   void default_function_args(T = static_cast<Types>(0)); // expected-error{{default argument contains unexpanded parameter pack 'Types'}}
200 
201   template<typename = Types*> // expected-error{{default argument contains unexpanded parameter pack 'Types'}}
202     struct default_template_args_1;
203   template<int = static_cast<Types>(0)> // expected-error{{default argument contains unexpanded parameter pack 'Types'}}
204     struct default_template_args_2;
205   template<template<typename> class = Types::template apply> // expected-error{{default argument contains unexpanded parameter pack 'Types'}}
206     struct default_template_args_3;
207 
208   template<Types value> // expected-error{{non-type template parameter type contains unexpanded parameter pack 'Types'}}
209   struct non_type_template_param_type;
210 
decls_in_stmtsTestUnexpandedDecls211   void decls_in_stmts() {
212     Types t; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
213     for (Types *t = 0; ; ) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
214     for (; Types *t = 0; ) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
215     T a[] = { T(), T(), T() };
216     for (Types t : a) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
217     switch(Types *t = 0) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
218     while(Types *t = 0) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
219     if (Types *t = 0) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
220     try {
221     } catch (Types*) { // expected-error{{exception type contains unexpanded parameter pack 'Types'}}
222     }
223   }
224 };
225 
226 // FIXME: Test for unexpanded parameter packs in each of the statements.
227 struct X {
228   void f(int, int);
229   template<typename ...Types>
230   void f(Types...);
231 };
232 
233 namespace std {
234   class type_info;
235 }
236 
237 typedef struct _GUID {
238      unsigned long  Data1;
239      unsigned short Data2;
240      unsigned short Data3;
241      unsigned char  Data4[ 8 ];
242 } GUID;
243 
244 template<typename T, typename ...Types>
test_unexpanded_exprs(Types...values)245 void test_unexpanded_exprs(Types ...values) {
246   // CXXOperatorCallExpr
247   (void)(values + 0); // expected-error{{expression contains unexpanded parameter pack 'values'}}
248   (void)(0 + values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
249 
250   // CXXMemberCallExpr
251   values.f(); // expected-error{{expression contains unexpanded parameter pack 'values'}}
252   X x;
253   x.f(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
254   x.Types::f(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
255   x.f<Types>(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
256 
257   // CXXStaticCastExpr
258   (void)static_cast<Types&>(values); // expected-error{{expression contains unexpanded parameter packs 'Types' and 'values'}}
259 
260   // CXXDynamicCastExpr
261   (void)dynamic_cast<Types&>(values); // expected-error{{expression contains unexpanded parameter packs 'Types' and 'values'}}
262 
263   // CXXReinterpretCastExpr
264   (void)reinterpret_cast<Types&>(values); // expected-error{{expression contains unexpanded parameter packs 'Types' and 'values'}}
265 
266   // CXXConstCastExpr
267   (void)const_cast<Types&>(values); // expected-error{{expression contains unexpanded parameter packs 'Types' and 'values'}}
268 
269   // CXXTypeidExpr
270   (void)typeid(Types); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
271   (void)typeid(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
272 
273   // CXXUuidofExpr
274   (void)__uuidof(Types); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
275   (void)__uuidof(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
276 
277   // CXXThisExpr is uninteresting
278 
279   // CXXThrowExpr
280   throw Types(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
281   throw values; // expected-error{{expression contains unexpanded parameter pack 'values'}}
282 
283   // CXXDefaultArgExpr is uninteresting
284 
285   // CXXBindTemporaryExpr is uninteresting
286 
287   // CXXConstructExpr is uninteresting
288 
289   // CXXFunctionalCastExpr
290   (void)Types(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
291 
292   // CXXTemporaryObjectExpr
293   (void)X(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
294 
295   // CXXScalarValueInitExpr is uninteresting
296 
297   // CXXNewExpr
298   (void)new Types; // expected-error{{expression contains unexpanded parameter pack 'Types'}}
299   (void)new X(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
300   (void)new (values) X(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
301   (void)new X [values]; // expected-error{{expression contains unexpanded parameter pack 'values'}}
302 
303   // CXXDeleteExpr
304   delete values; // expected-error{{expression contains unexpanded parameter pack 'values'}}
305   delete [] values; // expected-error{{expression contains unexpanded parameter pack 'values'}}
306 
307   // CXXPseudoDestructorExpr
308   T t;
309   values.~T(); // expected-error{{expression contains unexpanded parameter pack 'values'}}
310   t.~Types(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
311   t.Types::~T(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
312 
313   // Unary TypeTraitExpr
314   __is_pod(Types); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
315 
316   // Binary TypeTraitExpr
317   __is_base_of(Types, T); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
318   __is_base_of(T, Types); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
319 
320   // UnresolvedLookupExpr
321   test_unexpanded_exprs(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
322   test_unexpanded_exprs<Types>(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
323 
324   // DependentScopeDeclRefExpr
325   Types::test_unexpanded_exprs(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
326   T::template test_unexpanded_exprs<Types>(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
327 
328   // CXXUnresolvedConstructExpr
329   Types(5); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
330 
331   // CXXDependentScopeMemberExpr
332   values.foo(); // expected-error{{expression contains unexpanded parameter pack 'values'}}
333   t.foo(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
334 
335   // FIXME: There's an evil ambiguity here, because we don't know if
336   // Types refers to the template type parameter pack in scope or a
337   // non-pack member.
338   //  t.Types::foo();
339 
340   t.template foo<Types>(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
341 
342   // UnresolvedMemberExpr
343   x.f<Types>(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
344   x.f(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
345 
346   // CXXNoexceptExpr
347   noexcept(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
348 
349   // PackExpansionExpr is uninteresting
350   // SizeOfPackExpr is uninteresting
351 
352   // FIXME: Objective-C expressions will need to go elsewhere
353 
354   for (auto t : values) { } // expected-error{{expression contains unexpanded parameter pack 'values'}}
355 
356   switch (values) { } // expected-error{{expression contains unexpanded parameter pack 'values'}}
357   switch (0) { case 0: case values: ; } // expected-error{{expression contains unexpanded parameter pack 'values'}}
358 
359   do { } while (values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
360 
361 test:
362   goto *values; // expected-error{{expression contains unexpanded parameter pack 'values'}}
363 
364   void f(int arg = values); // expected-error{{default argument contains unexpanded parameter pack 'values'}}
365 }
366 
367 // Test unexpanded parameter packs in partial specializations.
368 template<typename ...Types>
369 struct TestUnexpandedDecls<int, Types>; // expected-error{{partial specialization contains unexpanded parameter pack 'Types'}}
370 
371 // Test for diagnostics in the presence of multiple unexpanded
372 // parameter packs.
373 template<typename T, typename U> struct pair;
374 
375 template<typename ...OuterTypes>
376 struct MemberTemplatePPNames {
377   template<typename ...InnerTypes>
378   struct Inner {
379     typedef pair<OuterTypes, InnerTypes>* types; // expected-error{{declaration type contains unexpanded parameter packs 'OuterTypes' and 'InnerTypes'}}
380 
381     template<typename ...VeryInnerTypes>
382     struct VeryInner {
383       typedef pair<pair<VeryInnerTypes, OuterTypes>, pair<InnerTypes, OuterTypes> > types; // expected-error{{declaration type contains unexpanded parameter packs 'VeryInnerTypes', 'OuterTypes', ...}}
384     };
385   };
386 };
387 
388 // Example from working paper
389 namespace WorkingPaperExample {
390   template<typename...> struct Tuple {};
391   template<typename T1, typename T2> struct Pair {};
392 
393   template<class ... Args1> struct zip {
394     template<class ... Args2> struct with {
395       typedef Tuple<Pair<Args1, Args2> ... > type; // expected-error{{pack expansion contains parameter packs 'Args1' and 'Args2' that have different lengths (1 vs. 2)}}
396     };
397   };
398 
399   typedef zip<short, int>::with<unsigned short, unsigned>::type T1; // T1 is Tuple<Pair<short, unsigned short>, Pair<int, unsigned>>
400   typedef Tuple<Pair<short, unsigned short>, Pair<int, unsigned>> T1;
401 
402   typedef zip<short>::with<unsigned short, unsigned>::type T2; // expected-note{{in instantiation of template class}}
403 
404   template<class ... Args> void f(Args...);
405   template<class ... Args> void h(Args...);
406 
407   template<class ... Args>
g(Args...args)408   void g(Args ... args) {
409     f(const_cast<const Args*>(&args)...); // OK: "Args" and "args" are expanded within f
410     f(5 ...); // expected-error{{pack expansion does not contain any unexpanded parameter packs}}
411     f(args); // expected-error{{expression contains unexpanded parameter pack 'args'}}
412     f(h(args ...) + args ...);
413   }
414 }
415 
416 namespace PR16303 {
417   template<int> struct A { A(int); };
418   template<int...N> struct B {
419     template<int...M> struct C : A<N>... {
CPR16303::B::C420       C() : A<N>(M)... {} // expected-error{{pack expansion contains parameter packs 'N' and 'M' that have different lengths (2 vs. 3)}} expected-error{{pack expansion contains parameter packs 'N' and 'M' that have different lengths (4 vs. 3)}}
421     };
422   };
423   B<1,2>::C<4,5,6> c1; // expected-note{{in instantiation of}}
424   B<1,2,3,4>::C<4,5,6> c2; // expected-note{{in instantiation of}}
425 }
426 
427 namespace PR21289 {
428   template<int> using T = int;
429   template<typename> struct S { static const int value = 0; };
430   template<typename> const int vt = 0; // expected-warning {{extension}}
431   int f(...);
g()432   template<int ...Ns> void g() {
433     f(T<Ns>()...);
434     f(S<T<Ns>>::value...);
435     f(vt<T<Ns>>...);
436   }
437   template void g<>();
438   template void g<1, 2, 3>();
439 }
440