dependent-names.cpp 12 KB

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