instantiate-expr-4.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
  2. // ---------------------------------------------------------------------
  3. // C++ Functional Casts
  4. // ---------------------------------------------------------------------
  5. template<int N>
  6. struct ValueInit0 {
  7. int f() {
  8. return int();
  9. }
  10. };
  11. template struct ValueInit0<5>;
  12. template<int N>
  13. struct FunctionalCast0 {
  14. int f() {
  15. return int(N);
  16. }
  17. };
  18. template struct FunctionalCast0<5>;
  19. struct X { // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
  20. X(int, int); // expected-note 3 {{candidate constructor}}
  21. };
  22. template<int N, int M>
  23. struct BuildTemporary0 {
  24. X f() {
  25. return X(N, M);
  26. }
  27. };
  28. template struct BuildTemporary0<5, 7>;
  29. template<int N, int M>
  30. struct Temporaries0 {
  31. void f() {
  32. (void)X(N, M);
  33. }
  34. };
  35. template struct Temporaries0<5, 7>;
  36. // Ensure that both the constructor and the destructor are instantiated by
  37. // checking for parse errors from each.
  38. template<int N> struct BadX {
  39. BadX() { int a[-N]; } // expected-error {{array with a negative size}}
  40. ~BadX() { int a[-N]; } // expected-error {{array with a negative size}}
  41. };
  42. template<int N>
  43. struct PR6671 {
  44. void f() { (void)BadX<1>(); } // expected-note 2 {{instantiation}}
  45. };
  46. template struct PR6671<1>;
  47. // ---------------------------------------------------------------------
  48. // new/delete expressions
  49. // ---------------------------------------------------------------------
  50. struct Y { };
  51. template<typename T>
  52. struct New0 {
  53. T* f(bool x) {
  54. if (x)
  55. return new T; // expected-error{{no matching}}
  56. else
  57. return new T();
  58. }
  59. };
  60. template struct New0<int>;
  61. template struct New0<Y>;
  62. template struct New0<X>; // expected-note{{instantiation}}
  63. template<typename T, typename Arg1>
  64. struct New1 {
  65. T* f(bool x, Arg1 a1) {
  66. return new T(a1); // expected-error{{no matching}}
  67. }
  68. };
  69. template struct New1<int, float>;
  70. template struct New1<Y, Y>;
  71. template struct New1<X, Y>; // expected-note{{instantiation}}
  72. template<typename T, typename Arg1, typename Arg2>
  73. struct New2 {
  74. T* f(bool x, Arg1 a1, Arg2 a2) {
  75. return new T(a1, a2); // expected-error{{no matching}}
  76. }
  77. };
  78. template struct New2<X, int, float>;
  79. template struct New2<X, int, int*>; // expected-note{{instantiation}}
  80. // FIXME: template struct New2<int, int, float>;
  81. // PR5833
  82. struct New3 {
  83. New3();
  84. void *operator new[](__SIZE_TYPE__) __attribute__((unavailable)); // expected-note{{explicitly made unavailable}}
  85. };
  86. template<class C>
  87. void* object_creator() {
  88. return new C(); // expected-error{{call to unavailable function 'operator new[]'}}
  89. }
  90. template void *object_creator<New3[4]>(); // expected-note{{instantiation}}
  91. template<typename T>
  92. struct Delete0 {
  93. void f(T t) {
  94. delete t; // expected-error{{cannot delete}}
  95. ::delete [] t; // expected-error{{cannot delete}}
  96. }
  97. };
  98. template struct Delete0<int*>;
  99. template struct Delete0<X*>;
  100. template struct Delete0<int>; // expected-note{{instantiation}}
  101. namespace PR5755 {
  102. template <class T>
  103. void Foo() {
  104. char* p = 0;
  105. delete[] p;
  106. }
  107. void Test() {
  108. Foo<int>();
  109. }
  110. }
  111. namespace PR10480 {
  112. template<typename T>
  113. struct X {
  114. X();
  115. ~X() {
  116. T *ptr = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
  117. }
  118. };
  119. template<typename T>
  120. void f() {
  121. new X<int>[1]; // expected-note{{in instantiation of member function 'PR10480::X<int>::~X' requested here}}
  122. }
  123. template void f<int>();
  124. }
  125. // ---------------------------------------------------------------------
  126. // throw expressions
  127. // ---------------------------------------------------------------------
  128. template<typename T>
  129. struct Throw1 {
  130. void f(T t) {
  131. throw;
  132. throw t; // expected-error{{incomplete type}}
  133. }
  134. };
  135. struct Incomplete; // expected-note 2{{forward}}
  136. template struct Throw1<int>;
  137. template struct Throw1<int*>;
  138. template struct Throw1<Incomplete*>; // expected-note{{instantiation}}
  139. // ---------------------------------------------------------------------
  140. // typeid expressions
  141. // ---------------------------------------------------------------------
  142. namespace std {
  143. class type_info;
  144. }
  145. template<typename T>
  146. struct TypeId0 {
  147. const std::type_info &f(T* ptr) {
  148. if (ptr)
  149. return typeid(ptr);
  150. else
  151. return typeid(T); // expected-error{{'typeid' of incomplete type 'Incomplete'}}
  152. }
  153. };
  154. struct Abstract {
  155. virtual void f() = 0;
  156. };
  157. template struct TypeId0<int>;
  158. template struct TypeId0<Incomplete>; // expected-note{{instantiation of member function}}
  159. template struct TypeId0<Abstract>;
  160. // ---------------------------------------------------------------------
  161. // type traits
  162. // ---------------------------------------------------------------------
  163. template<typename T>
  164. struct is_pod {
  165. static const bool value = __is_pod(T);
  166. };
  167. static int is_pod0[is_pod<X>::value? -1 : 1];
  168. static int is_pod1[is_pod<Y>::value? 1 : -1];
  169. // ---------------------------------------------------------------------
  170. // initializer lists
  171. // ---------------------------------------------------------------------
  172. template<typename T, typename Val1>
  173. struct InitList1 {
  174. void f(Val1 val1) {
  175. T x = { val1 };
  176. }
  177. };
  178. struct APair {
  179. int *x;
  180. const float *y;
  181. };
  182. template struct InitList1<int[1], float>;
  183. template struct InitList1<APair, int*>;
  184. template<typename T, typename Val1, typename Val2>
  185. struct InitList2 {
  186. void f(Val1 val1, Val2 val2) {
  187. T x = { val1, val2 }; // expected-error{{cannot initialize}}
  188. }
  189. };
  190. template struct InitList2<APair, int*, float*>;
  191. template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}}
  192. // ---------------------------------------------------------------------
  193. // member references
  194. // ---------------------------------------------------------------------
  195. template<typename T, typename Result>
  196. struct DotMemRef0 {
  197. void f(T t) {
  198. Result result = t.m; // expected-error{{non-const lvalue reference to type}}
  199. }
  200. };
  201. struct MemInt {
  202. int m;
  203. };
  204. struct InheritsMemInt : MemInt { };
  205. struct MemIntFunc {
  206. static int m(int);
  207. };
  208. template struct DotMemRef0<MemInt, int&>;
  209. template struct DotMemRef0<InheritsMemInt, int&>;
  210. template struct DotMemRef0<MemIntFunc, int (*)(int)>;
  211. template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}}
  212. template<typename T, typename Result>
  213. struct ArrowMemRef0 {
  214. void f(T t) {
  215. Result result = t->m; // expected-error 2{{non-const lvalue reference}}
  216. }
  217. };
  218. template<typename T>
  219. struct ArrowWrapper {
  220. T operator->();
  221. };
  222. template struct ArrowMemRef0<MemInt*, int&>;
  223. template struct ArrowMemRef0<InheritsMemInt*, int&>;
  224. template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>;
  225. template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}}
  226. template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>;
  227. template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>;
  228. template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>;
  229. template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}}
  230. template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>;
  231. struct UnresolvedMemRefArray {
  232. int f(int);
  233. int f(char);
  234. };
  235. UnresolvedMemRefArray Arr[10];
  236. template<typename U> int UnresolvedMemRefArrayT(U u) {
  237. return Arr->f(u);
  238. }
  239. template int UnresolvedMemRefArrayT<int>(int);
  240. // FIXME: we should be able to return a MemInt without the reference!
  241. MemInt &createMemInt(int);
  242. template<int N>
  243. struct NonDepMemberExpr0 {
  244. void f() {
  245. createMemInt(N).m = N;
  246. }
  247. };
  248. template struct NonDepMemberExpr0<0>;
  249. template<typename T, typename Result>
  250. struct MemberFuncCall0 {
  251. void f(T t) {
  252. Result result = t.f();
  253. }
  254. };
  255. template<typename T>
  256. struct HasMemFunc0 {
  257. T f();
  258. };
  259. template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>;
  260. template<typename Result>
  261. struct ThisMemberFuncCall0 {
  262. Result g();
  263. void f() {
  264. Result r1 = g();
  265. Result r2 = this->g();
  266. }
  267. };
  268. template struct ThisMemberFuncCall0<int&>;
  269. template<typename T>
  270. struct NonDepMemberCall0 {
  271. void foo(HasMemFunc0<int&> x) {
  272. T result = x.f(); // expected-error{{non-const lvalue reference}}
  273. }
  274. };
  275. template struct NonDepMemberCall0<int&>;
  276. template struct NonDepMemberCall0<const int&>;
  277. template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}}
  278. template<typename T>
  279. struct QualifiedDeclRef0 {
  280. T f() {
  281. return is_pod<X>::value; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'const bool'}}
  282. }
  283. };
  284. template struct QualifiedDeclRef0<bool>;
  285. template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}}