xref: /aosp_15_r20/external/clang/test/SemaTemplate/dependent-names.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2*67e74705SXin Li 
3*67e74705SXin Li typedef double A;
4*67e74705SXin Li template<typename T> class B {
5*67e74705SXin Li   typedef int A;
6*67e74705SXin Li };
7*67e74705SXin Li 
8*67e74705SXin Li template<typename T> struct X : B<T> {
9*67e74705SXin Li   static A a;
10*67e74705SXin Li };
11*67e74705SXin Li 
12*67e74705SXin Li int a0[sizeof(X<int>::a) == sizeof(double) ? 1 : -1];
13*67e74705SXin Li 
14*67e74705SXin Li // PR4365.
15*67e74705SXin Li template<class T> class Q;
16*67e74705SXin Li template<class T> class R : Q<T> {T current;};
17*67e74705SXin Li 
18*67e74705SXin Li 
19*67e74705SXin Li namespace test0 {
20*67e74705SXin Li   template <class T> class Base {
21*67e74705SXin Li   public:
22*67e74705SXin Li     void instance_foo();
23*67e74705SXin Li     static void static_foo();
24*67e74705SXin Li     class Inner {
25*67e74705SXin Li     public:
26*67e74705SXin Li       void instance_foo();
27*67e74705SXin Li       static void static_foo();
28*67e74705SXin Li     };
29*67e74705SXin Li   };
30*67e74705SXin Li 
31*67e74705SXin Li   template <class T> class Derived1 : Base<T> {
32*67e74705SXin Li   public:
test0()33*67e74705SXin Li     void test0() {
34*67e74705SXin Li       Base<T>::static_foo();
35*67e74705SXin Li       Base<T>::instance_foo();
36*67e74705SXin Li     }
37*67e74705SXin Li 
test1()38*67e74705SXin Li     void test1() {
39*67e74705SXin Li       Base<T>::Inner::static_foo();
40*67e74705SXin Li       Base<T>::Inner::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
41*67e74705SXin Li     }
42*67e74705SXin Li 
test2()43*67e74705SXin Li     static void test2() {
44*67e74705SXin Li       Base<T>::static_foo();
45*67e74705SXin Li       Base<T>::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
46*67e74705SXin Li     }
47*67e74705SXin Li 
test3()48*67e74705SXin Li     static void test3() {
49*67e74705SXin Li       Base<T>::Inner::static_foo();
50*67e74705SXin Li       Base<T>::Inner::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
51*67e74705SXin Li     }
52*67e74705SXin Li   };
53*67e74705SXin Li 
54*67e74705SXin Li   template <class T> class Derived2 : Base<T>::Inner {
55*67e74705SXin Li   public:
test0()56*67e74705SXin Li     void test0() {
57*67e74705SXin Li       Base<T>::static_foo();
58*67e74705SXin Li       Base<T>::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
59*67e74705SXin Li     }
60*67e74705SXin Li 
test1()61*67e74705SXin Li     void test1() {
62*67e74705SXin Li       Base<T>::Inner::static_foo();
63*67e74705SXin Li       Base<T>::Inner::instance_foo();
64*67e74705SXin Li     }
65*67e74705SXin Li 
test2()66*67e74705SXin Li     static void test2() {
67*67e74705SXin Li       Base<T>::static_foo();
68*67e74705SXin Li       Base<T>::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
69*67e74705SXin Li     }
70*67e74705SXin Li 
test3()71*67e74705SXin Li     static void test3() {
72*67e74705SXin Li       Base<T>::Inner::static_foo();
73*67e74705SXin Li       Base<T>::Inner::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
74*67e74705SXin Li     }
75*67e74705SXin Li   };
76*67e74705SXin Li 
test0()77*67e74705SXin Li   void test0() {
78*67e74705SXin Li     Derived1<int> d1;
79*67e74705SXin Li     d1.test0();
80*67e74705SXin Li     d1.test1(); // expected-note {{in instantiation of member function}}
81*67e74705SXin Li     d1.test2(); // expected-note {{in instantiation of member function}}
82*67e74705SXin Li     d1.test3(); // expected-note {{in instantiation of member function}}
83*67e74705SXin Li 
84*67e74705SXin Li     Derived2<int> d2;
85*67e74705SXin Li     d2.test0(); // expected-note {{in instantiation of member function}}
86*67e74705SXin Li     d2.test1();
87*67e74705SXin Li     d2.test2(); // expected-note {{in instantiation of member function}}
88*67e74705SXin Li     d2.test3(); // expected-note {{in instantiation of member function}}
89*67e74705SXin Li   }
90*67e74705SXin Li }
91*67e74705SXin Li 
92*67e74705SXin Li namespace test1 {
93*67e74705SXin Li   template <class T> struct Base {
94*67e74705SXin Li     void foo(T); // expected-note {{must qualify identifier to find this declaration in dependent base class}}
95*67e74705SXin Li   };
96*67e74705SXin Li 
97*67e74705SXin Li   template <class T> struct Derived : Base<T> {
doFootest1::Derived98*67e74705SXin Li     void doFoo(T v) {
99*67e74705SXin Li       foo(v); // expected-error {{use of undeclared identifier}}
100*67e74705SXin Li     }
101*67e74705SXin Li   };
102*67e74705SXin Li 
103*67e74705SXin Li   template struct Derived<int>; // expected-note {{requested here}}
104*67e74705SXin Li }
105*67e74705SXin Li 
106*67e74705SXin Li namespace PR8966 {
107*67e74705SXin Li   template <class T>
108*67e74705SXin Li   class MyClassCore
109*67e74705SXin Li   {
110*67e74705SXin Li   };
111*67e74705SXin Li 
112*67e74705SXin Li   template <class T>
113*67e74705SXin Li   class MyClass : public MyClassCore<T>
114*67e74705SXin Li   {
115*67e74705SXin Li   public:
116*67e74705SXin Li     enum  {
117*67e74705SXin Li       N
118*67e74705SXin Li     };
119*67e74705SXin Li 
120*67e74705SXin Li     // static member declaration
121*67e74705SXin Li     static const char* array [N];
122*67e74705SXin Li 
f()123*67e74705SXin Li     void f() {
124*67e74705SXin Li       MyClass<T>::InBase = 17;
125*67e74705SXin Li     }
126*67e74705SXin Li   };
127*67e74705SXin Li 
128*67e74705SXin Li   // static member definition
129*67e74705SXin Li   template <class T>
130*67e74705SXin Li   const char* MyClass<T>::array [MyClass<T>::N] = { "A", "B", "C" };
131*67e74705SXin Li }
132*67e74705SXin Li 
133*67e74705SXin Li namespace std {
134*67e74705SXin Li   inline namespace v1 {
135*67e74705SXin Li     template<typename T> struct basic_ostream;
136*67e74705SXin Li   }
137*67e74705SXin Li   namespace inner {
138*67e74705SXin Li     template<typename T> struct vector {};
139*67e74705SXin Li   }
140*67e74705SXin Li   using inner::vector;
141*67e74705SXin Li   template<typename T, typename U> struct pair {};
142*67e74705SXin Li   typedef basic_ostream<char> ostream;
143*67e74705SXin Li   extern ostream cout;
144*67e74705SXin Li   std::ostream &operator<<(std::ostream &out, const char *);
145*67e74705SXin Li }
146*67e74705SXin Li 
147*67e74705SXin Li namespace PR10053 {
148*67e74705SXin Li   template<typename T> struct A {
149*67e74705SXin Li     T t;
APR10053::A150*67e74705SXin Li     A() {
151*67e74705SXin Li       f(t); // expected-error {{call to function 'f' that is neither visible in the template definition nor found by argument-dependent lookup}}
152*67e74705SXin Li     }
153*67e74705SXin Li   };
154*67e74705SXin Li 
155*67e74705SXin Li   void f(int&); // expected-note {{'f' should be declared prior to the call site}}
156*67e74705SXin Li 
157*67e74705SXin Li   A<int> a; // expected-note {{in instantiation of member function}}
158*67e74705SXin Li 
159*67e74705SXin Li 
160*67e74705SXin Li   namespace N {
161*67e74705SXin Li     namespace M {
g(T t)162*67e74705SXin Li       template<typename T> int g(T t) {
163*67e74705SXin Li         f(t); // expected-error {{call to function 'f' that is neither visible in the template definition nor found by argument-dependent lookup}}
164*67e74705SXin Li       };
165*67e74705SXin Li     }
166*67e74705SXin Li 
167*67e74705SXin Li     void f(char&); // expected-note {{'f' should be declared prior to the call site}}
168*67e74705SXin Li   }
169*67e74705SXin Li 
170*67e74705SXin Li   void f(char&);
171*67e74705SXin Li 
172*67e74705SXin Li   int k = N::M::g<char>(0);; // expected-note {{in instantiation of function}}
173*67e74705SXin Li 
174*67e74705SXin Li 
175*67e74705SXin Li   namespace O {
176*67e74705SXin Li     void f(char&); // expected-note {{candidate function not viable}}
177*67e74705SXin Li 
178*67e74705SXin Li     template<typename T> struct C {
179*67e74705SXin Li       static const int n = f(T()); // expected-error {{no matching function}}
180*67e74705SXin Li     };
181*67e74705SXin Li   }
182*67e74705SXin Li 
183*67e74705SXin Li   int f(double); // no note, shadowed by O::f
184*67e74705SXin Li   O::C<double> c; // expected-note {{requested here}}
185*67e74705SXin Li 
186*67e74705SXin Li 
187*67e74705SXin Li   // Example from www/compatibility.html
188*67e74705SXin Li   namespace my_file {
Squared(T x)189*67e74705SXin Li     template <typename T> T Squared(T x) {
190*67e74705SXin Li       return Multiply(x, x); // expected-error {{neither visible in the template definition nor found by argument-dependent lookup}}
191*67e74705SXin Li     }
192*67e74705SXin Li 
Multiply(int x,int y)193*67e74705SXin Li     int Multiply(int x, int y) { // expected-note {{should be declared prior to the call site}}
194*67e74705SXin Li       return x * y;
195*67e74705SXin Li     }
196*67e74705SXin Li 
main()197*67e74705SXin Li     int main() {
198*67e74705SXin Li       Squared(5); // expected-note {{here}}
199*67e74705SXin Li     }
200*67e74705SXin Li   }
201*67e74705SXin Li 
202*67e74705SXin Li   // Example from www/compatibility.html
203*67e74705SXin Li   namespace my_file2 {
204*67e74705SXin Li     template<typename T>
Dump(const T & value)205*67e74705SXin Li     void Dump(const T& value) {
206*67e74705SXin Li       std::cout << value << "\n"; // expected-error {{neither visible in the template definition nor found by argument-dependent lookup}}
207*67e74705SXin Li     }
208*67e74705SXin Li 
209*67e74705SXin Li     namespace ns {
210*67e74705SXin Li       struct Data {};
211*67e74705SXin Li     }
212*67e74705SXin Li 
operator <<(std::ostream & out,ns::Data data)213*67e74705SXin Li     std::ostream& operator<<(std::ostream& out, ns::Data data) { // expected-note {{should be declared prior to the call site or in namespace 'PR10053::my_file2::ns'}}
214*67e74705SXin Li       return out << "Some data";
215*67e74705SXin Li     }
216*67e74705SXin Li 
Use()217*67e74705SXin Li     void Use() {
218*67e74705SXin Li       Dump(ns::Data()); // expected-note {{here}}
219*67e74705SXin Li     }
220*67e74705SXin Li   }
221*67e74705SXin Li 
222*67e74705SXin Li   namespace my_file2_a {
223*67e74705SXin Li     template<typename T>
Dump(const T & value)224*67e74705SXin Li     void Dump(const T &value) {
225*67e74705SXin Li       print(std::cout, value); // expected-error 4{{neither visible in the template definition nor found by argument-dependent lookup}}
226*67e74705SXin Li     }
227*67e74705SXin Li 
228*67e74705SXin Li     namespace ns {
229*67e74705SXin Li       struct Data {};
230*67e74705SXin Li     }
231*67e74705SXin Li     namespace ns2 {
232*67e74705SXin Li       struct Data {};
233*67e74705SXin Li     }
234*67e74705SXin Li 
235*67e74705SXin Li     std::ostream &print(std::ostream &out, int); // expected-note-re {{should be declared prior to the call site{{$}}}}
236*67e74705SXin Li     std::ostream &print(std::ostream &out, ns::Data); // expected-note {{should be declared prior to the call site or in namespace 'PR10053::my_file2_a::ns'}}
237*67e74705SXin Li     std::ostream &print(std::ostream &out, std::vector<ns2::Data>); // expected-note {{should be declared prior to the call site or in namespace 'PR10053::my_file2_a::ns2'}}
238*67e74705SXin Li     std::ostream &print(std::ostream &out, std::pair<ns::Data, ns2::Data>); // expected-note {{should be declared prior to the call site or in an associated namespace of one of its arguments}}
239*67e74705SXin Li 
Use()240*67e74705SXin Li     void Use() {
241*67e74705SXin Li       Dump(0); // expected-note {{requested here}}
242*67e74705SXin Li       Dump(ns::Data()); // expected-note {{requested here}}
243*67e74705SXin Li       Dump(std::vector<ns2::Data>()); // expected-note {{requested here}}
244*67e74705SXin Li       Dump(std::pair<ns::Data, ns2::Data>()); // expected-note {{requested here}}
245*67e74705SXin Li     }
246*67e74705SXin Li   }
247*67e74705SXin Li 
248*67e74705SXin Li   namespace unary {
249*67e74705SXin Li     template<typename T>
Negate(const T & value)250*67e74705SXin Li     T Negate(const T& value) {
251*67e74705SXin Li       return !value; // expected-error {{call to function 'operator!' that is neither visible in the template definition nor found by argument-dependent lookup}}
252*67e74705SXin Li     }
253*67e74705SXin Li 
254*67e74705SXin Li     namespace ns {
255*67e74705SXin Li       struct Data {};
256*67e74705SXin Li     }
257*67e74705SXin Li 
258*67e74705SXin Li     ns::Data operator!(ns::Data); // expected-note {{should be declared prior to the call site or in namespace 'PR10053::unary::ns'}}
259*67e74705SXin Li 
Use()260*67e74705SXin Li     void Use() {
261*67e74705SXin Li       Negate(ns::Data()); // expected-note {{requested here}}
262*67e74705SXin Li     }
263*67e74705SXin Li   }
264*67e74705SXin Li }
265*67e74705SXin Li 
266*67e74705SXin Li namespace PR10187 {
267*67e74705SXin Li   namespace A1 {
268*67e74705SXin Li     template<typename T>
269*67e74705SXin Li     struct S {
fPR10187::A1::S270*67e74705SXin Li       void f() {
271*67e74705SXin Li         for (auto &a : e)
272*67e74705SXin Li           __range(a); // expected-error {{undeclared identifier '__range'}}
273*67e74705SXin Li       }
274*67e74705SXin Li       int e[10];
275*67e74705SXin Li     };
g()276*67e74705SXin Li     void g() {
277*67e74705SXin Li       S<int>().f(); // expected-note {{here}}
278*67e74705SXin Li     }
279*67e74705SXin Li   }
280*67e74705SXin Li 
281*67e74705SXin Li   namespace A2 {
282*67e74705SXin Li     template<typename T>
283*67e74705SXin Li     struct S {
fPR10187::A2::S284*67e74705SXin Li       void f() {
285*67e74705SXin Li         for (auto &a : e)
286*67e74705SXin Li           __range(a); // expected-error {{undeclared identifier '__range'}}
287*67e74705SXin Li       }
288*67e74705SXin Li       T e[10];
289*67e74705SXin Li     };
g()290*67e74705SXin Li     void g() {
291*67e74705SXin Li       S<int>().f(); // expected-note {{here}}
292*67e74705SXin Li     }
293*67e74705SXin Li     struct X {};
294*67e74705SXin Li     void __range(X);
h()295*67e74705SXin Li     void h() {
296*67e74705SXin Li       S<X>().f();
297*67e74705SXin Li     }
298*67e74705SXin Li   }
299*67e74705SXin Li 
300*67e74705SXin Li   namespace B {
301*67e74705SXin Li     template<typename T> void g(); // expected-note {{not viable}}
f()302*67e74705SXin Li     template<typename T> void f() {
303*67e74705SXin Li       g<int>(T()); // expected-error {{no matching function}}
304*67e74705SXin Li     }
305*67e74705SXin Li 
306*67e74705SXin Li     namespace {
307*67e74705SXin Li       struct S {};
308*67e74705SXin Li     }
309*67e74705SXin Li     void g(S);
310*67e74705SXin Li 
311*67e74705SXin Li     template void f<S>(); // expected-note {{here}}
312*67e74705SXin Li   }
313*67e74705SXin Li }
314*67e74705SXin Li 
315*67e74705SXin Li namespace rdar11242625 {
316*67e74705SXin Li 
317*67e74705SXin Li template <typename T>
318*67e74705SXin Li struct Main {
319*67e74705SXin Li   struct default_names {
320*67e74705SXin Li     typedef int id;
321*67e74705SXin Li   };
322*67e74705SXin Li 
323*67e74705SXin Li   template <typename T2 = typename default_names::id>
324*67e74705SXin Li   struct TS {
325*67e74705SXin Li     T2 q;
326*67e74705SXin Li   };
327*67e74705SXin Li };
328*67e74705SXin Li 
329*67e74705SXin Li struct Sub : public Main<int> {
330*67e74705SXin Li   TS<> ff;
331*67e74705SXin Li };
332*67e74705SXin Li 
333*67e74705SXin Li int arr[sizeof(Sub)];
334*67e74705SXin Li 
335*67e74705SXin Li }
336*67e74705SXin Li 
337*67e74705SXin Li namespace PR11421 {
338*67e74705SXin Li template < unsigned > struct X {
339*67e74705SXin Li   static const unsigned dimension = 3;
340*67e74705SXin Li   template<unsigned dim=dimension>
341*67e74705SXin Li   struct Y: Y<dim> { }; // expected-error{{circular inheritance between 'Y<dim>' and 'Y<dim>'}}
342*67e74705SXin Li };
343*67e74705SXin Li typedef X<3> X3;
344*67e74705SXin Li X3::Y<>::iterator it; // expected-error {{no type named 'iterator' in 'PR11421::X<3>::Y<3>'}}
345*67e74705SXin Li }
346*67e74705SXin Li 
347*67e74705SXin Li namespace rdar12629723 {
348*67e74705SXin Li   template<class T>
349*67e74705SXin Li   struct X {
350*67e74705SXin Li     struct C : public C { }; // expected-error{{circular inheritance between 'rdar12629723::X::C' and 'rdar12629723::X::C'}}
351*67e74705SXin Li 
352*67e74705SXin Li     struct B;
353*67e74705SXin Li 
354*67e74705SXin Li     struct A : public B {  // expected-note{{'rdar12629723::X::A' declared here}}
foordar12629723::X::A355*67e74705SXin Li       virtual void foo() { }
356*67e74705SXin Li     };
357*67e74705SXin Li 
358*67e74705SXin Li     struct D : T::foo { };
359*67e74705SXin Li     struct E : D { };
360*67e74705SXin Li   };
361*67e74705SXin Li 
362*67e74705SXin Li   template<class T>
363*67e74705SXin Li   struct X<T>::B : public A {  // expected-error{{circular inheritance between 'rdar12629723::X::A' and 'rdar12629723::X::B'}}
foordar12629723::X::B364*67e74705SXin Li     virtual void foo() { }
365*67e74705SXin Li   };
366*67e74705SXin Li }
367*67e74705SXin Li 
368*67e74705SXin Li namespace test_reserved_identifiers {
tempf(A a,B b)369*67e74705SXin Li   template<typename A, typename B> void tempf(A a, B b) {
370*67e74705SXin Li     a + b;  // expected-error{{call to function 'operator+' that is neither visible in the template definition nor found by argument-dependent lookup}}
371*67e74705SXin Li   }
372*67e74705SXin Li   namespace __gnu_cxx { struct X {}; }
373*67e74705SXin Li   namespace ns { struct Y {}; }
374*67e74705SXin Li   void operator+(__gnu_cxx::X, ns::Y);  // expected-note{{or in namespace 'test_reserved_identifiers::ns'}}
test()375*67e74705SXin Li   void test() {
376*67e74705SXin Li     __gnu_cxx::X x;
377*67e74705SXin Li     ns::Y y;
378*67e74705SXin Li     tempf(x, y);  // expected-note{{in instantiation of}}
379*67e74705SXin Li   }
380*67e74705SXin Li }
381*67e74705SXin Li 
382*67e74705SXin Li // This test must live in the global namespace.
383*67e74705SXin Li struct PR14695_X {};
384*67e74705SXin Li // FIXME: This note is bogus; it is the using directive which would need to move
385*67e74705SXin Li // to prior to the call site to fix the problem.
386*67e74705SXin Li namespace PR14695_A { void PR14695_f(PR14695_X); } // expected-note {{'PR14695_f' should be declared prior to the call site or in the global namespace}}
PR14695_g(T t)387*67e74705SXin Li template<typename T> void PR14695_g(T t) { PR14695_f(t); } // expected-error {{call to function 'PR14695_f' that is neither visible in the template definition nor found by argument-dependent lookup}}
388*67e74705SXin Li using namespace PR14695_A;
389*67e74705SXin Li template void PR14695_g(PR14695_X); // expected-note{{requested here}}
390*67e74705SXin Li 
391*67e74705SXin Li namespace OperatorNew {
f(T t)392*67e74705SXin Li   template<typename T> void f(T t) {
393*67e74705SXin Li     operator new(100, t); // expected-error{{call to function 'operator new' that is neither visible in the template definition nor found by argument-dependent lookup}}
394*67e74705SXin Li     // FIXME: This should give the same error.
395*67e74705SXin Li     new (t) int;
396*67e74705SXin Li   }
397*67e74705SXin Li   struct X {};
398*67e74705SXin Li };
399*67e74705SXin Li using size_t = decltype(sizeof(0));
400*67e74705SXin Li void *operator new(size_t, OperatorNew::X); // expected-note-re {{should be declared prior to the call site{{$}}}}
401*67e74705SXin Li template void OperatorNew::f(OperatorNew::X); // expected-note {{instantiation of}}
402*67e74705SXin Li 
403*67e74705SXin Li namespace PR19936 {
T()404*67e74705SXin Li   template<typename T> decltype(*T()) f() {} // expected-note {{previous}}
T()405*67e74705SXin Li   template<typename T> decltype(T() * T()) g() {} // expected-note {{previous}}
406*67e74705SXin Li 
407*67e74705SXin Li   // Create some overloaded operators so we build an overload operator call
408*67e74705SXin Li   // instead of a builtin operator call for the dependent expression.
409*67e74705SXin Li   enum E {};
410*67e74705SXin Li   int operator*(E);
411*67e74705SXin Li   int operator*(E, E);
412*67e74705SXin Li 
413*67e74705SXin Li   // Check that they still profile the same.
T()414*67e74705SXin Li   template<typename T> decltype(*T()) f() {} // expected-error {{redefinition}}
T()415*67e74705SXin Li   template<typename T> decltype(T() * T()) g() {} // expected-error {{redefinition}}
416*67e74705SXin Li }
417*67e74705SXin Li 
418*67e74705SXin Li template <typename> struct CT2 {
419*67e74705SXin Li   template <class U> struct X;
420*67e74705SXin Li };
421*67e74705SXin Li template <typename T> int CT2<int>::X<>; // expected-error {{template parameter list matching the non-templated nested type 'CT2<int>' should be empty}}
422