1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -verify %s -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=gnu++1z %s
4 
5 
6 
7 // Errors
8 export class foo { };   // expected-error {{expected template}}
9 template  x;            // expected-error {{C++ requires a type specifier for all declarations}} \
10                         // expected-error {{does not refer}}
11 export template x;      // expected-error {{expected '<' after 'template'}}
12 export template<class T> class x0; // expected-warning {{exported templates are unsupported}}
13 template < ;            // expected-error {{expected template parameter}} \
14 // expected-error{{expected ',' or '>' in template-parameter-list}} \
15 // expected-warning {{declaration does not declare anything}}
16 template <int +> struct x1; // expected-error {{expected ',' or '>' in template-parameter-list}}
17 
18 // verifies that we only walk to the ',' & still produce errors on the rest of the template parameters
19 template <int +, T> struct x2; // expected-error {{expected ',' or '>' in template-parameter-list}} \
20                                 expected-error {{expected unqualified-id}}
21 template<template<int+>> struct x3; // expected-error {{expected ',' or '>' in template-parameter-list}} \
22                                          expected-error {{template template parameter requires 'class' after the parameter list}}
23 template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \
24 // expected-error{{extraneous}}
25 template <template <typename> > struct Err2;       // expected-error {{template template parameter requires 'class' after the parameter list}}
26 template <template <typename> Foo> struct Err3;    // expected-error {{template template parameter requires 'class' after the parameter list}}
27 
28 template <template <typename> typename Foo> struct Cxx1z;
29 #if __cplusplus <= 201402L
30 // expected-warning@-2 {{extension}}
31 #endif
32 
33 // Template function declarations
34 template <typename T> void foo();
35 template <typename T, typename U> void foo();
36 
37 // Template function definitions.
38 template <typename T> void foo() { }
39 
40 // Template class (forward) declarations
41 template <typename T> struct A;
42 template <typename T, typename U> struct b;
43 template <typename> struct C;
44 template <typename, typename> struct D;
45 
46 // Forward declarations with default parameters?
47 template <typename T = int> class X1;
48 template <typename = int> class X2;
49 
50 // Forward declarations w/template template parameters
51 template <template <typename> class T> class TTP1;
52 template <template <typename> class> class TTP2;
53 template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}}
54 template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}}
55 template <template <typename X, typename Y> class T> class TTP5;
56 
57 // Forward declarations with non-type params
58 template <int> class NTP0;
59 template <int N> class NTP1;
60 template <int N = 5> class NTP2;
61 template <int = 10> class NTP3;
62 template <unsigned int N = 12u> class NTP4;
63 template <unsigned int = 12u> class NTP5;
64 template <unsigned = 15u> class NTP6;
65 template <typename T, T Obj> class NTP7;
66 
67 // Template class declarations
68 template <typename T> struct A { };
69 template <typename T, typename U> struct B { };
70 
71 // Template parameter shadowing
72 template<typename T, // expected-note{{template parameter is declared here}}
73          typename T> // expected-error{{declaration of 'T' shadows template parameter}}
74   void shadow1();
75 
76 template<typename T> // expected-note{{template parameter is declared here}}
77 void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}
78 
79 template<typename T> // expected-note{{template parameter is declared here}}
80 class T { // expected-error{{declaration of 'T' shadows template parameter}}
81 };
82 
83 template<int Size> // expected-note{{template parameter is declared here}}
84 void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
85 
86 // <rdar://problem/6952203>
87 template<typename T> // expected-note{{here}}
88 struct shadow4 {
89   int T; // expected-error{{shadows}}
90 };
91 
92 template<typename T> // expected-note{{here}}
93 struct shadow5 {
94   int T(int, float); // expected-error{{shadows}}
95 };
96 
97 template<typename T, // expected-note{{template parameter is declared here}}
98          T T> // expected-error{{declaration of 'T' shadows template parameter}}
99 void shadow6();
100 
101 template<typename T, // expected-note{{template parameter is declared here}}
102          template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}}
103 void shadow7();
104 
105 // PR8302
106 template<template<typename> class T> struct shadow8 { // expected-note{{template parameter is declared here}}
107   template<template<typename> class T> struct inner; // expected-error{{declaration of 'T' shadows template parameter}}
108 };
109 
110 // Non-type template parameters in scope
111 template<int Size>
112 void f(int& i) {
113   i = Size;
114  #ifdef DELAYED_TEMPLATE_PARSING
115   Size = i;
116  #else
117   Size = i; // expected-error{{expression is not assignable}}
118  #endif
119 }
120 
121 template<typename T>
122 const T& min(const T&, const T&);
123 
124 void f2() {
125   int x;
126   A< typeof(x>1) > a;
127 }
128 
129 
130 // PR3844
131 template <> struct S<int> { }; // expected-error{{explicit specialization of undeclared template struct 'S'}}
132 template <> union U<int> { }; // expected-error{{explicit specialization of undeclared template union 'U'}}
133 
134 struct SS;
135 union UU;
136 template <> struct SS<int> { }; // expected-error{{explicit specialization of non-template struct 'SS'}}
137 template <> union UU<int> { }; // expected-error{{explicit specialization of non-template union 'UU'}}
138 
139 namespace PR6184 {
140   namespace N {
141     template <typename T>
142     void bar(typename T::x);
143   }
144 
145   template <typename T>
146   void N::bar(typename T::x) { }
147 }
148 
149 // This PR occurred only in template parsing mode.
150 namespace PR17637 {
151 template <int>
152 struct L {
153   template <typename T>
154   struct O {
155     template <typename U>
156     static void Fun(U);
157   };
158 };
159 
160 template <int k>
161 template <typename T>
162 template <typename U>
163 void L<k>::O<T>::Fun(U) {}
164 
165 void Instantiate() { L<0>::O<int>::Fun(0); }
166 
167 }
168 
169 namespace explicit_partial_specializations {
170 typedef char (&oneT)[1];
171 typedef char (&twoT)[2];
172 typedef char (&threeT)[3];
173 typedef char (&fourT)[4];
174 typedef char (&fiveT)[5];
175 typedef char (&sixT)[6];
176 
177 char one[1];
178 char two[2];
179 char three[3];
180 char four[4];
181 char five[5];
182 char six[6];
183 
184 template<bool b> struct bool_ { typedef int type; };
185 template<> struct bool_<false> {  };
186 
187 #define XCAT(x,y) x ## y
188 #define CAT(x,y) XCAT(x,y)
189 #define sassert(_b_) bool_<(_b_)>::type CAT(var, __LINE__);
190 
191 
192 template <int>
193 struct L {
194   template <typename T>
195   struct O {
196     template <typename U>
197     static oneT Fun(U);
198 
199   };
200 };
201 template <int k>
202 template <typename T>
203 template <typename U>
204 oneT L<k>::O<T>::Fun(U) { return one; }
205 
206 template<>
207 template<>
208 template<typename U>
209 oneT L<0>::O<char>::Fun(U) { return one; }
210 
211 
212 void Instantiate() {
213   sassert(sizeof(L<0>::O<int>::Fun(0)) == sizeof(one));
214   sassert(sizeof(L<0>::O<char>::Fun(0)) == sizeof(one));
215 }
216 
217 }
218 
219 namespace func_tmpl_spec_def_in_func {
220 // We failed to diagnose function template specialization definitions inside
221 // functions during recovery previously.
222 template <class> void FuncTemplate() {}
223 void TopLevelFunc() {
224   // expected-error@+2 {{expected a qualified name after 'typename'}}
225   // expected-error@+1 {{function definition is not allowed here}}
226   typename template <> void FuncTemplate<void>() { }
227   // expected-error@+1 {{function definition is not allowed here}}
228   void NonTemplateInner() { }
229 }
230 }
231 
232 namespace broken_baseclause {
233 template<typename T>
234 struct base { };
235 
236 struct t1 : base<int, // expected-note {{to match this '<'}}
237   public:  // expected-error {{expected expression}} expected-error {{expected '>'}}
238 };
239 // expected-error@-1 {{expected '{' after base class list}}
240 struct t2 : base<int, // expected-note {{to match this '<'}}
241   public  // expected-error {{expected expression}} expected-error {{expected '>'}}
242 };
243 // expected-error@-1 {{expected '{' after base class list}}
244 
245 }
246 
247 namespace class_scope_instantiation {
248   struct A {
249     template<typename T> void f(T);
250     template void f<int>(int); // expected-error {{expected '<' after 'template'}}
251     template void f(float); // expected-error {{expected '<' after 'template'}}
252     extern template // expected-error {{expected member name or ';'}}
253       void f(double);
254   };
255 }
256 
257 namespace PR42071 {
258   template<int SomeTemplateName<void>> struct A; // expected-error {{parameter name cannot have template arguments}}
259   template<int operator+> struct B; // expected-error {{'operator+' cannot be the name of a parameter}}
260   struct Q {};
261   template<int Q::N> struct C; // expected-error {{parameter declarator cannot be qualified}}
262   template<int f(int a = 0)> struct D; // expected-error {{default arguments can only be specified for parameters in a function declaration}}
263 }
264 
265 namespace AnnotateAfterInvalidTemplateId {
266   template<int I, int J> struct A { };
267   template<int J> struct A<0, J> { }; // expected-note {{J = 0}}
268   template<int I> struct A<I, 0> { }; // expected-note {{I = 0}}
269 
270   void f() { A<0, 0>::f(); } // expected-error {{ambiguous partial specializations}}
271 }
272 
273 namespace PR45063 {
274   template<class=class a::template b<>> struct X {}; // expected-error {{undeclared identifier 'a'}}
275 }
276 
277 namespace NoCrashOnEmptyNestedNameSpecifier {
278   template <typename FnT,
279             typename T = typename ABC<FnT>::template arg_t<0>> // expected-error {{no template named 'ABC'}}
280   void foo(FnT) {}
281 }
282 
283 namespace PR45239 {
284   // Ensure we don't crash here. We used to deallocate the TemplateIdAnnotation
285   // before we'd parsed it.
286   template<int> int b;
287   template<int> auto f() -> b<0>; // expected-error +{{}}
288 }
289