xref: /aosp_15_r20/external/clang/test/SemaTemplate/instantiate-local-class.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -verify -std=c++11 %s
2*67e74705SXin Li // RUN: %clang_cc1 -verify -std=c++11 -fdelayed-template-parsing %s
3*67e74705SXin Li 
4*67e74705SXin Li template<typename T>
f0()5*67e74705SXin Li void f0() {
6*67e74705SXin Li   struct X;
7*67e74705SXin Li   typedef struct Y {
8*67e74705SXin Li     T (X::* f1())(int) { return 0; }
9*67e74705SXin Li   } Y2;
10*67e74705SXin Li 
11*67e74705SXin Li   Y2 y = Y();
12*67e74705SXin Li }
13*67e74705SXin Li 
14*67e74705SXin Li template void f0<int>();
15*67e74705SXin Li 
16*67e74705SXin Li // PR5764
17*67e74705SXin Li namespace PR5764 {
18*67e74705SXin Li   struct X {
19*67e74705SXin Li     template <typename T>
BarPR5764::X20*67e74705SXin Li     void Bar() {
21*67e74705SXin Li       typedef T ValueType;
22*67e74705SXin Li       struct Y {
23*67e74705SXin Li         Y() { V = ValueType(); }
24*67e74705SXin Li 
25*67e74705SXin Li         ValueType V;
26*67e74705SXin Li       };
27*67e74705SXin Li 
28*67e74705SXin Li       Y y;
29*67e74705SXin Li     }
30*67e74705SXin Li   };
31*67e74705SXin Li 
test(X x)32*67e74705SXin Li   void test(X x) {
33*67e74705SXin Li     x.Bar<int>();
34*67e74705SXin Li   }
35*67e74705SXin Li }
36*67e74705SXin Li 
37*67e74705SXin Li // Instantiation of local classes with virtual functions.
38*67e74705SXin Li namespace local_class_with_virtual_functions {
39*67e74705SXin Li   template <typename T> struct X { };
40*67e74705SXin Li   template <typename T> struct Y { };
41*67e74705SXin Li 
42*67e74705SXin Li   template <typename T>
f()43*67e74705SXin Li   void f() {
44*67e74705SXin Li     struct Z : public X<Y<T>*> {
45*67e74705SXin Li       virtual void g(Y<T>* y) { }
46*67e74705SXin Li       void g2(int x) {(void)x;}
47*67e74705SXin Li     };
48*67e74705SXin Li     Z z;
49*67e74705SXin Li     (void)z;
50*67e74705SXin Li   }
51*67e74705SXin Li 
52*67e74705SXin Li   struct S { };
test()53*67e74705SXin Li   void test() { f<S>(); }
54*67e74705SXin Li }
55*67e74705SXin Li 
56*67e74705SXin Li namespace PR8801 {
57*67e74705SXin Li   template<typename T>
foo()58*67e74705SXin Li   void foo() {
59*67e74705SXin Li     class X;
60*67e74705SXin Li     typedef int (X::*pmf_type)();
61*67e74705SXin Li     class X : public T { };
62*67e74705SXin Li 
63*67e74705SXin Li     pmf_type pmf = &T::foo;
64*67e74705SXin Li   }
65*67e74705SXin Li 
66*67e74705SXin Li   struct Y { int foo(); };
67*67e74705SXin Li 
68*67e74705SXin Li   template void foo<Y>();
69*67e74705SXin Li }
70*67e74705SXin Li 
71*67e74705SXin Li namespace TemplatePacksAndLambdas {
72*67e74705SXin Li   template <typename ...T> int g(T...);
73*67e74705SXin Li   struct S {
fTemplatePacksAndLambdas::S74*67e74705SXin Li     template <typename ...T> static void f(int f = g([]{ static T t; return ++t; }()...)) {}
75*67e74705SXin Li   };
h()76*67e74705SXin Li   void h() { S::f<int, int, int>(); }
77*67e74705SXin Li }
78*67e74705SXin Li 
79*67e74705SXin Li namespace PR9685 {
forEach(Thing t)80*67e74705SXin Li   template <class Thing> void forEach(Thing t) { t.func(); }
81*67e74705SXin Li 
doIt()82*67e74705SXin Li   template <typename T> void doIt() {
83*67e74705SXin Li     struct Functor {
84*67e74705SXin Li       void func() { (void)i; }
85*67e74705SXin Li       int i;
86*67e74705SXin Li     };
87*67e74705SXin Li 
88*67e74705SXin Li     forEach(Functor());
89*67e74705SXin Li   }
90*67e74705SXin Li 
call()91*67e74705SXin Li   void call() {
92*67e74705SXin Li     doIt<int>();
93*67e74705SXin Li   }
94*67e74705SXin Li }
95*67e74705SXin Li 
96*67e74705SXin Li namespace PR12702 {
97*67e74705SXin Li   struct S {
applyPR12702::S98*67e74705SXin Li     template <typename F> bool apply(F f) { return f(); }
99*67e74705SXin Li   };
100*67e74705SXin Li 
101*67e74705SXin Li   template <typename> struct T {
fooPR12702::T102*67e74705SXin Li     void foo() {
103*67e74705SXin Li       struct F {
104*67e74705SXin Li         int x;
105*67e74705SXin Li 
106*67e74705SXin Li         bool operator()() { return x == 0; }
107*67e74705SXin Li       };
108*67e74705SXin Li 
109*67e74705SXin Li       S().apply(F());
110*67e74705SXin Li     }
111*67e74705SXin Li   };
112*67e74705SXin Li 
call()113*67e74705SXin Li   void call() { T<int>().foo(); }
114*67e74705SXin Li }
115*67e74705SXin Li 
116*67e74705SXin Li namespace PR17139 {
foo(const T & t)117*67e74705SXin Li   template <class T> void foo(const T &t) { t.foo(); }
118*67e74705SXin Li 
bar(F * f)119*67e74705SXin Li   template <class F> void bar(F *f) {
120*67e74705SXin Li     struct B {
121*67e74705SXin Li       F *fn;
122*67e74705SXin Li       void foo() const { fn(); }
123*67e74705SXin Li     } b = { f };
124*67e74705SXin Li     foo(b);
125*67e74705SXin Li   }
126*67e74705SXin Li 
go()127*67e74705SXin Li   void go() {}
128*67e74705SXin Li 
test()129*67e74705SXin Li   void test() { bar(go); }
130*67e74705SXin Li }
131*67e74705SXin Li 
132*67e74705SXin Li namespace PR17740 {
133*67e74705SXin Li class C {
134*67e74705SXin Li public:
135*67e74705SXin Li   template <typename T> static void foo(T function);
136*67e74705SXin Li   template <typename T> static void bar(T function);
137*67e74705SXin Li   template <typename T> static void func(T function);
138*67e74705SXin Li };
139*67e74705SXin Li 
foo(T function)140*67e74705SXin Li template <typename T> void C::foo(T function) { function(); }
141*67e74705SXin Li 
bar(T function)142*67e74705SXin Li template <typename T> void C::bar(T function) {
143*67e74705SXin Li   foo([&function]() { function(); });
144*67e74705SXin Li }
145*67e74705SXin Li 
func(T function)146*67e74705SXin Li template <typename T> void C::func(T function) {
147*67e74705SXin Li   struct Struct {
148*67e74705SXin Li     T mFunction;
149*67e74705SXin Li 
150*67e74705SXin Li     Struct(T function) : mFunction(function) {};
151*67e74705SXin Li 
152*67e74705SXin Li     void operator()() {
153*67e74705SXin Li       mFunction();
154*67e74705SXin Li     };
155*67e74705SXin Li   };
156*67e74705SXin Li 
157*67e74705SXin Li   bar(Struct(function));
158*67e74705SXin Li }
159*67e74705SXin Li 
call()160*67e74705SXin Li void call() {
161*67e74705SXin Li   C::func([]() {});
162*67e74705SXin Li }
163*67e74705SXin Li }
164*67e74705SXin Li 
165*67e74705SXin Li namespace PR14373 {
166*67e74705SXin Li   struct function {
functionPR14373::function167*67e74705SXin Li     template <typename _Functor> function(_Functor __f) { __f(); }
168*67e74705SXin Li   };
exec_func(Func f)169*67e74705SXin Li   template <typename Func> function exec_func(Func f) {
170*67e74705SXin Li     struct functor {
171*67e74705SXin Li       functor(Func f) : func(f) {}
172*67e74705SXin Li       void operator()() const { func(); }
173*67e74705SXin Li       Func func;
174*67e74705SXin Li     };
175*67e74705SXin Li     return functor(f);
176*67e74705SXin Li   }
177*67e74705SXin Li   struct Type {
operator ()PR14373::Type178*67e74705SXin Li     void operator()() const {}
179*67e74705SXin Li   };
call()180*67e74705SXin Li   int call() {
181*67e74705SXin Li     exec_func(Type());
182*67e74705SXin Li     return 0;
183*67e74705SXin Li   }
184*67e74705SXin Li }
185*67e74705SXin Li 
186*67e74705SXin Li namespace PR18907 {
187*67e74705SXin Li template <typename>
188*67e74705SXin Li class C : public C<int> {}; // expected-error{{within its own definition}}
189*67e74705SXin Li 
190*67e74705SXin Li template <typename X>
F()191*67e74705SXin Li void F() {
192*67e74705SXin Li   struct A : C<X> {};
193*67e74705SXin Li }
194*67e74705SXin Li 
195*67e74705SXin Li struct B {
fPR18907::B196*67e74705SXin Li   void f() { F<int>(); }
197*67e74705SXin Li };
198*67e74705SXin Li }
199*67e74705SXin Li 
200*67e74705SXin Li namespace PR23194 {
201*67e74705SXin Li   struct X {
operator ()PR23194::X202*67e74705SXin Li     int operator()() const { return 0; }
203*67e74705SXin Li   };
204*67e74705SXin Li   struct Y {
YPR23194::Y205*67e74705SXin Li     Y(int) {}
206*67e74705SXin Li   };
make_seed_pair()207*67e74705SXin Li   template <bool = true> int make_seed_pair() noexcept {
208*67e74705SXin Li     struct state_t {
209*67e74705SXin Li       X x;
210*67e74705SXin Li       Y y{x()};
211*67e74705SXin Li     };
212*67e74705SXin Li     return 0;
213*67e74705SXin Li   }
func()214*67e74705SXin Li   int func() {
215*67e74705SXin Li     return make_seed_pair();
216*67e74705SXin Li   }
217*67e74705SXin Li }
218*67e74705SXin Li 
219*67e74705SXin Li namespace PR18653 {
220*67e74705SXin Li   // Forward declarations
221*67e74705SXin Li 
f1()222*67e74705SXin Li   template<typename T> void f1() {
223*67e74705SXin Li     void g1(struct x1);
224*67e74705SXin Li     struct x1 {};
225*67e74705SXin Li   }
226*67e74705SXin Li   template void f1<int>();
227*67e74705SXin Li 
f1a()228*67e74705SXin Li   template<typename T> void f1a() {
229*67e74705SXin Li     void g1(union x1);
230*67e74705SXin Li     union x1 {};
231*67e74705SXin Li   }
232*67e74705SXin Li   template void f1a<int>();
233*67e74705SXin Li 
f2()234*67e74705SXin Li   template<typename T> void f2() {
235*67e74705SXin Li     void g2(enum x2);  // expected-error{{ISO C++ forbids forward references to 'enum' types}}
236*67e74705SXin Li     enum x2 { nothing };
237*67e74705SXin Li   }
238*67e74705SXin Li   template void f2<int>();
239*67e74705SXin Li 
f3()240*67e74705SXin Li   template<typename T> void f3() {
241*67e74705SXin Li     void g3(enum class x3);
242*67e74705SXin Li     enum class x3 { nothing };
243*67e74705SXin Li   }
244*67e74705SXin Li   template void f3<int>();
245*67e74705SXin Li 
246*67e74705SXin Li 
f4()247*67e74705SXin Li   template<typename T> void f4() {
248*67e74705SXin Li     void g4(struct x4 {} x);  // expected-error{{'x4' cannot be defined in a parameter type}}
249*67e74705SXin Li   }
250*67e74705SXin Li   template void f4<int>();
251*67e74705SXin Li 
f4a()252*67e74705SXin Li   template<typename T> void f4a() {
253*67e74705SXin Li     void g4(union x4 {} x);  // expected-error{{'x4' cannot be defined in a parameter type}}
254*67e74705SXin Li   }
255*67e74705SXin Li   template void f4a<int>();
256*67e74705SXin Li 
257*67e74705SXin Li 
258*67e74705SXin Li   template <class T> void f();
259*67e74705SXin Li   template <class T> struct S1 {
mPR18653::S1260*67e74705SXin Li     void m() {
261*67e74705SXin Li       f<class newclass>();
262*67e74705SXin Li       f<union newunion>();
263*67e74705SXin Li     }
264*67e74705SXin Li   };
265*67e74705SXin Li   template struct S1<int>;
266*67e74705SXin Li 
267*67e74705SXin Li   template <class T> struct S2 {
mPR18653::S2268*67e74705SXin Li     void m() {
269*67e74705SXin Li       f<enum new_enum>();  // expected-error{{ISO C++ forbids forward references to 'enum' types}}
270*67e74705SXin Li     }
271*67e74705SXin Li   };
272*67e74705SXin Li   template struct S2<int>;
273*67e74705SXin Li 
274*67e74705SXin Li   template <class T> struct S3 {
mPR18653::S3275*67e74705SXin Li     void m() {
276*67e74705SXin Li       f<enum class new_enum>();
277*67e74705SXin Li     }
278*67e74705SXin Li   };
279*67e74705SXin Li   template struct S3<int>;
280*67e74705SXin Li 
281*67e74705SXin Li   template <class T> struct S4 {
282*67e74705SXin Li     struct local {};
mPR18653::S4283*67e74705SXin Li     void m() {
284*67e74705SXin Li       f<local>();
285*67e74705SXin Li     }
286*67e74705SXin Li   };
287*67e74705SXin Li   template struct S4<int>;
288*67e74705SXin Li 
289*67e74705SXin Li   template <class T> struct S4a {
290*67e74705SXin Li     union local {};
mPR18653::S4a291*67e74705SXin Li     void m() {
292*67e74705SXin Li       f<local>();
293*67e74705SXin Li     }
294*67e74705SXin Li   };
295*67e74705SXin Li   template struct S4a<int>;
296*67e74705SXin Li 
297*67e74705SXin Li   template <class T> struct S5 {
298*67e74705SXin Li     enum local { nothing };
mPR18653::S5299*67e74705SXin Li     void m() {
300*67e74705SXin Li       f<local>();
301*67e74705SXin Li     }
302*67e74705SXin Li   };
303*67e74705SXin Li   template struct S5<int>;
304*67e74705SXin Li 
305*67e74705SXin Li   template <class T> struct S7 {
306*67e74705SXin Li     enum class local { nothing };
mPR18653::S7307*67e74705SXin Li     void m() {
308*67e74705SXin Li       f<local>();
309*67e74705SXin Li     }
310*67e74705SXin Li   };
311*67e74705SXin Li   template struct S7<int>;
312*67e74705SXin Li 
313*67e74705SXin Li 
314*67e74705SXin Li   template <class T> void fff(T *x);
315*67e74705SXin Li   template <class T> struct S01 {
316*67e74705SXin Li     struct local { };
mPR18653::S01317*67e74705SXin Li     void m() {
318*67e74705SXin Li       local x;
319*67e74705SXin Li       fff(&x);
320*67e74705SXin Li     }
321*67e74705SXin Li   };
322*67e74705SXin Li   template struct S01<int>;
323*67e74705SXin Li 
324*67e74705SXin Li   template <class T> struct S01a {
325*67e74705SXin Li     union local { };
mPR18653::S01a326*67e74705SXin Li     void m() {
327*67e74705SXin Li       local x;
328*67e74705SXin Li       fff(&x);
329*67e74705SXin Li     }
330*67e74705SXin Li   };
331*67e74705SXin Li   template struct S01a<int>;
332*67e74705SXin Li 
333*67e74705SXin Li   template <class T> struct S02 {
334*67e74705SXin Li     enum local { nothing };
mPR18653::S02335*67e74705SXin Li     void m() {
336*67e74705SXin Li       local x;
337*67e74705SXin Li       fff(&x);
338*67e74705SXin Li     }
339*67e74705SXin Li   };
340*67e74705SXin Li   template struct S02<int>;
341*67e74705SXin Li 
342*67e74705SXin Li   template <class T> struct S03 {
343*67e74705SXin Li     enum class local { nothing };
mPR18653::S03344*67e74705SXin Li     void m() {
345*67e74705SXin Li       local x;
346*67e74705SXin Li       fff(&x);
347*67e74705SXin Li     }
348*67e74705SXin Li   };
349*67e74705SXin Li   template struct S03<int>;
350*67e74705SXin Li 
351*67e74705SXin Li 
352*67e74705SXin Li   template <class T> struct S04 {
mPR18653::S04353*67e74705SXin Li     void m() {
354*67e74705SXin Li       struct { } x;
355*67e74705SXin Li       fff(&x);
356*67e74705SXin Li     }
357*67e74705SXin Li   };
358*67e74705SXin Li   template struct S04<int>;
359*67e74705SXin Li 
360*67e74705SXin Li   template <class T> struct S04a {
mPR18653::S04a361*67e74705SXin Li     void m() {
362*67e74705SXin Li       union { } x;
363*67e74705SXin Li       fff(&x);
364*67e74705SXin Li     }
365*67e74705SXin Li   };
366*67e74705SXin Li   template struct S04a<int>;
367*67e74705SXin Li 
368*67e74705SXin Li   template <class T> struct S05 {
mPR18653::S05369*67e74705SXin Li     void m() {
370*67e74705SXin Li       enum { nothing } x;
371*67e74705SXin Li       fff(&x);
372*67e74705SXin Li     }
373*67e74705SXin Li   };
374*67e74705SXin Li   template struct S05<int>;
375*67e74705SXin Li 
376*67e74705SXin Li   template <class T> struct S06 {
mPR18653::S06377*67e74705SXin Li     void m() {
378*67e74705SXin Li       class { virtual void mmm() {} } x;
379*67e74705SXin Li       fff(&x);
380*67e74705SXin Li     }
381*67e74705SXin Li   };
382*67e74705SXin Li   template struct S06<int>;
383*67e74705SXin Li }
384*67e74705SXin Li 
385*67e74705SXin Li namespace PR20625 {
386*67e74705SXin Li template <typename T>
f()387*67e74705SXin Li void f() {
388*67e74705SXin Li   struct N {
389*67e74705SXin Li     static constexpr int get() { return 42; }
390*67e74705SXin Li   };
391*67e74705SXin Li   constexpr int n = N::get();
392*67e74705SXin Li   static_assert(n == 42, "n == 42");
393*67e74705SXin Li }
394*67e74705SXin Li 
g()395*67e74705SXin Li void g() { f<void>(); }
396*67e74705SXin Li }
397*67e74705SXin Li 
398*67e74705SXin Li 
399*67e74705SXin Li namespace PR21332 {
f1()400*67e74705SXin Li   template<typename T> void f1() {
401*67e74705SXin Li     struct S {  // expected-note{{in instantiation of member class 'S' requested here}}
402*67e74705SXin Li       void g1(int n = T::error);  // expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
403*67e74705SXin Li     };
404*67e74705SXin Li   }
405*67e74705SXin Li   template void f1<int>();  // expected-note{{in instantiation of function template specialization 'PR21332::f1<int>' requested here}}
406*67e74705SXin Li 
f2()407*67e74705SXin Li   template<typename T> void f2() {
408*67e74705SXin Li     struct S {  // expected-note{{in instantiation of member class 'S' requested here}}
409*67e74705SXin Li       void g2() noexcept(T::error);  // expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
410*67e74705SXin Li     };
411*67e74705SXin Li   }
412*67e74705SXin Li   template void f2<int>();  // expected-note{{in instantiation of function template specialization 'PR21332::f2<int>' requested here}}
413*67e74705SXin Li 
f3()414*67e74705SXin Li   template<typename T> void f3() {
415*67e74705SXin Li     enum S {
416*67e74705SXin Li       val = T::error;  // expected-error{{expected '}' or ','}} expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
417*67e74705SXin Li     };
418*67e74705SXin Li   }
419*67e74705SXin Li   template void f3<int>();  //expected-note{{in instantiation of function template specialization 'PR21332::f3<int>' requested here}}
420*67e74705SXin Li 
f4()421*67e74705SXin Li   template<typename T> void f4() {
422*67e74705SXin Li     enum class S {
423*67e74705SXin Li       val = T::error;  // expected-error{{expected '}' or ','}} expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
424*67e74705SXin Li     };
425*67e74705SXin Li   }
426*67e74705SXin Li   template void f4<int>();  // expected-note{{in instantiation of function template specialization 'PR21332::f4<int>' requested here}}
427*67e74705SXin Li 
f5()428*67e74705SXin Li   template<typename T> void f5() {
429*67e74705SXin Li     class S {  // expected-note {{in instantiation of default member initializer 'PR21332::f5()::S::val' requested here}}
430*67e74705SXin Li       int val = T::error;  // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
431*67e74705SXin Li      };
432*67e74705SXin Li   }
433*67e74705SXin Li   template void f5<int>();  // expected-note {{in instantiation of function template specialization 'PR21332::f5<int>' requested here}}
434*67e74705SXin Li 
f6()435*67e74705SXin Li   template<typename T> void f6() {
436*67e74705SXin Li     class S {  // expected-note {{in instantiation of member function 'PR21332::f6()::S::get' requested here}}
437*67e74705SXin Li       void get() {
438*67e74705SXin Li         class S2 {  // expected-note {{in instantiation of member class 'S2' requested here}}
439*67e74705SXin Li           void g1(int n = T::error);  // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
440*67e74705SXin Li         };
441*67e74705SXin Li       }
442*67e74705SXin Li     };
443*67e74705SXin Li   }
444*67e74705SXin Li   template void f6<int>();  // expected-note{{in instantiation of function template specialization 'PR21332::f6<int>' requested here}}
445*67e74705SXin Li 
f7()446*67e74705SXin Li   template<typename T> void f7() {
447*67e74705SXin Li     struct S { void g() noexcept(undefined_val); };  // expected-error{{use of undeclared identifier 'undefined_val'}}
448*67e74705SXin Li   }
449*67e74705SXin Li   template void f7<int>();
450*67e74705SXin Li }
451*67e74705SXin Li 
452*67e74705SXin Li // rdar://23721638: Ensure that we correctly perform implicit
453*67e74705SXin Li // conversions when instantiating the default arguments of local functions.
454*67e74705SXin Li namespace rdar23721638 {
455*67e74705SXin Li   struct A {
456*67e74705SXin Li     A(const char *) = delete;  // expected-note 2 {{explicitly marked deleted here}}
457*67e74705SXin Li   };
458*67e74705SXin Li 
foo()459*67e74705SXin Li   template <typename T> void foo() {
460*67e74705SXin Li     struct Inner { // expected-note {{in instantiation}}
461*67e74705SXin Li       void operator()(T a = "") {} // expected-error {{conversion function from 'const char [1]' to 'rdar23721638::A' invokes a deleted function}}
462*67e74705SXin Li       // expected-note@-1 {{passing argument to parameter 'a' here}}
463*67e74705SXin Li       // expected-note@-2 {{candidate function not viable}}
464*67e74705SXin Li     };
465*67e74705SXin Li     Inner()(); // expected-error {{no matching function}}
466*67e74705SXin Li   }
467*67e74705SXin Li   template void foo<A>(); // expected-note 2 {{in instantiation}}
468*67e74705SXin Li 
bar()469*67e74705SXin Li   template <typename T> void bar() {
470*67e74705SXin Li     auto lambda = [](T a = "") {}; // expected-error {{conversion function from 'const char [1]' to 'rdar23721638::A' invokes a deleted function}}
471*67e74705SXin Li       // expected-note@-1 {{passing argument to parameter 'a' here}}
472*67e74705SXin Li       // expected-note@-2 {{candidate function not viable}}
473*67e74705SXin Li       // expected-note@-3 {{conversion candidate of type}}
474*67e74705SXin Li     lambda(); // expected-error {{no matching function}}
475*67e74705SXin Li   }
476*67e74705SXin Li   template void bar<A>(); // expected-note {{in instantiation}}
477*67e74705SXin Li }
478