Anders Carlsson | 6774b1f | 2011-02-28 00:40:07 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++98 %s |
| 3 | // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 %s |
Douglas Gregor | e70f108 | 2009-05-20 21:38:11 +0000 | [diff] [blame] | 4 | |
| 5 | // --------------------------------------------------------------------- |
| 6 | // C++ Functional Casts |
| 7 | // --------------------------------------------------------------------- |
| 8 | template<int N> |
| 9 | struct ValueInit0 { |
| 10 | int f() { |
| 11 | return int(); |
| 12 | } |
| 13 | }; |
| 14 | |
| 15 | template struct ValueInit0<5>; |
| 16 | |
| 17 | template<int N> |
| 18 | struct FunctionalCast0 { |
| 19 | int f() { |
| 20 | return int(N); |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | template struct FunctionalCast0<5>; |
| 25 | |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 26 | struct X { // expected-note 3 {{candidate constructor (the implicit copy constructor)}} |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 27 | #if __cplusplus >= 201103L |
| 28 | // expected-note@-2 3 {{candidate constructor (the implicit move constructor) not viable}} |
| 29 | #endif |
John McCall | fd0b2f8 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 30 | X(int, int); // expected-note 3 {{candidate constructor}} |
Douglas Gregor | e70f108 | 2009-05-20 21:38:11 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | template<int N, int M> |
| 34 | struct BuildTemporary0 { |
| 35 | X f() { |
| 36 | return X(N, M); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | template struct BuildTemporary0<5, 7>; |
Douglas Gregor | 06555f1 | 2009-05-20 21:51:01 +0000 | [diff] [blame] | 41 | |
| 42 | template<int N, int M> |
| 43 | struct Temporaries0 { |
| 44 | void f() { |
| 45 | (void)X(N, M); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | template struct Temporaries0<5, 7>; |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 50 | |
Chandler Carruth | b32b344 | 2010-03-31 18:34:58 +0000 | [diff] [blame] | 51 | // Ensure that both the constructor and the destructor are instantiated by |
| 52 | // checking for parse errors from each. |
| 53 | template<int N> struct BadX { |
Chandler Carruth | a92409c | 2011-01-04 04:44:35 +0000 | [diff] [blame] | 54 | BadX() { int a[-N]; } // expected-error {{array with a negative size}} |
| 55 | ~BadX() { int a[-N]; } // expected-error {{array with a negative size}} |
Chandler Carruth | b32b344 | 2010-03-31 18:34:58 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | template<int N> |
| 59 | struct PR6671 { |
| 60 | void f() { (void)BadX<1>(); } // expected-note 2 {{instantiation}} |
| 61 | }; |
| 62 | template struct PR6671<1>; |
| 63 | |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 64 | // --------------------------------------------------------------------- |
Douglas Gregor | 29fe6ae | 2009-05-21 17:21:12 +0000 | [diff] [blame] | 65 | // new/delete expressions |
Douglas Gregor | d0fefba | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 66 | // --------------------------------------------------------------------- |
| 67 | struct Y { }; |
| 68 | |
| 69 | template<typename T> |
| 70 | struct New0 { |
| 71 | T* f(bool x) { |
| 72 | if (x) |
| 73 | return new T; // expected-error{{no matching}} |
| 74 | else |
| 75 | return new T(); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | template struct New0<int>; |
| 80 | template struct New0<Y>; |
| 81 | template struct New0<X>; // expected-note{{instantiation}} |
| 82 | |
| 83 | template<typename T, typename Arg1> |
| 84 | struct New1 { |
| 85 | T* f(bool x, Arg1 a1) { |
| 86 | return new T(a1); // expected-error{{no matching}} |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | template struct New1<int, float>; |
| 91 | template struct New1<Y, Y>; |
| 92 | template struct New1<X, Y>; // expected-note{{instantiation}} |
| 93 | |
| 94 | template<typename T, typename Arg1, typename Arg2> |
| 95 | struct New2 { |
| 96 | T* f(bool x, Arg1 a1, Arg2 a2) { |
| 97 | return new T(a1, a2); // expected-error{{no matching}} |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | template struct New2<X, int, float>; |
| 102 | template struct New2<X, int, int*>; // expected-note{{instantiation}} |
| 103 | // FIXME: template struct New2<int, int, float>; |
Douglas Gregor | 29fe6ae | 2009-05-21 17:21:12 +0000 | [diff] [blame] | 104 | |
Douglas Gregor | 2e9c795 | 2009-12-22 17:13:37 +0000 | [diff] [blame] | 105 | // PR5833 |
| 106 | struct New3 { |
| 107 | New3(); |
| 108 | |
| 109 | void *operator new[](__SIZE_TYPE__) __attribute__((unavailable)); // expected-note{{explicitly made unavailable}} |
| 110 | }; |
| 111 | |
| 112 | template<class C> |
| 113 | void* object_creator() { |
| 114 | return new C(); // expected-error{{call to unavailable function 'operator new[]'}} |
| 115 | } |
| 116 | |
| 117 | template void *object_creator<New3[4]>(); // expected-note{{instantiation}} |
| 118 | |
Douglas Gregor | 29fe6ae | 2009-05-21 17:21:12 +0000 | [diff] [blame] | 119 | template<typename T> |
| 120 | struct Delete0 { |
| 121 | void f(T t) { |
| 122 | delete t; // expected-error{{cannot delete}} |
John McCall | 1ababa6 | 2010-08-27 19:56:05 +0000 | [diff] [blame] | 123 | ::delete [] t; // expected-error{{cannot delete}} |
Douglas Gregor | 29fe6ae | 2009-05-21 17:21:12 +0000 | [diff] [blame] | 124 | } |
| 125 | }; |
| 126 | |
| 127 | template struct Delete0<int*>; |
| 128 | template struct Delete0<X*>; |
| 129 | template struct Delete0<int>; // expected-note{{instantiation}} |
Douglas Gregor | 728d41b | 2009-05-21 17:37:52 +0000 | [diff] [blame] | 130 | |
Douglas Gregor | 6131b44 | 2009-12-12 18:16:41 +0000 | [diff] [blame] | 131 | namespace PR5755 { |
| 132 | template <class T> |
| 133 | void Foo() { |
| 134 | char* p = 0; |
| 135 | delete[] p; |
| 136 | } |
| 137 | |
| 138 | void Test() { |
| 139 | Foo<int>(); |
| 140 | } |
| 141 | } |
| 142 | |
Douglas Gregor | 72912fb | 2011-07-26 15:11:03 +0000 | [diff] [blame] | 143 | namespace PR10480 { |
| 144 | template<typename T> |
| 145 | struct X { |
| 146 | X(); |
| 147 | ~X() { |
| 148 | T *ptr = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}} |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | template<typename T> |
| 153 | void f() { |
| 154 | new X<int>[1]; // expected-note{{in instantiation of member function 'PR10480::X<int>::~X' requested here}} |
| 155 | } |
| 156 | |
| 157 | template void f<int>(); |
| 158 | } |
| 159 | |
Douglas Gregor | 728d41b | 2009-05-21 17:37:52 +0000 | [diff] [blame] | 160 | // --------------------------------------------------------------------- |
| 161 | // throw expressions |
| 162 | // --------------------------------------------------------------------- |
| 163 | template<typename T> |
| 164 | struct Throw1 { |
| 165 | void f(T t) { |
| 166 | throw; |
| 167 | throw t; // expected-error{{incomplete type}} |
| 168 | } |
| 169 | }; |
| 170 | |
Douglas Gregor | 721fb2b | 2009-12-23 21:06:06 +0000 | [diff] [blame] | 171 | struct Incomplete; // expected-note 2{{forward}} |
Douglas Gregor | 728d41b | 2009-05-21 17:37:52 +0000 | [diff] [blame] | 172 | |
| 173 | template struct Throw1<int>; |
| 174 | template struct Throw1<int*>; |
| 175 | template struct Throw1<Incomplete*>; // expected-note{{instantiation}} |
| 176 | |
Douglas Gregor | aa85d82 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 177 | // --------------------------------------------------------------------- |
| 178 | // typeid expressions |
| 179 | // --------------------------------------------------------------------- |
| 180 | |
Douglas Gregor | aa85d82 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 181 | namespace std { |
| 182 | class type_info; |
| 183 | } |
| 184 | |
| 185 | template<typename T> |
| 186 | struct TypeId0 { |
| 187 | const std::type_info &f(T* ptr) { |
| 188 | if (ptr) |
| 189 | return typeid(ptr); |
| 190 | else |
John McCall | 85f9055 | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 191 | return typeid(T); // expected-error{{'typeid' of incomplete type 'Incomplete'}} |
Douglas Gregor | aa85d82 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 192 | } |
| 193 | }; |
| 194 | |
| 195 | struct Abstract { |
| 196 | virtual void f() = 0; |
| 197 | }; |
| 198 | |
| 199 | template struct TypeId0<int>; |
Douglas Gregor | 721fb2b | 2009-12-23 21:06:06 +0000 | [diff] [blame] | 200 | template struct TypeId0<Incomplete>; // expected-note{{instantiation of member function}} |
Douglas Gregor | aa85d82 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 201 | template struct TypeId0<Abstract>; |
Douglas Gregor | 31c7e99 | 2009-05-21 18:55:48 +0000 | [diff] [blame] | 202 | |
| 203 | // --------------------------------------------------------------------- |
| 204 | // type traits |
| 205 | // --------------------------------------------------------------------- |
| 206 | template<typename T> |
| 207 | struct is_pod { |
| 208 | static const bool value = __is_pod(T); |
| 209 | }; |
| 210 | |
Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 211 | static int is_pod0[is_pod<X>::value? -1 : 1]; |
| 212 | static int is_pod1[is_pod<Y>::value? 1 : -1]; |
Douglas Gregor | c2607b9 | 2009-05-21 21:38:12 +0000 | [diff] [blame] | 213 | |
| 214 | // --------------------------------------------------------------------- |
| 215 | // initializer lists |
| 216 | // --------------------------------------------------------------------- |
| 217 | template<typename T, typename Val1> |
| 218 | struct InitList1 { |
| 219 | void f(Val1 val1) { |
| 220 | T x = { val1 }; |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 221 | #if __cplusplus >= 201103L |
| 222 | // expected-error@-2 {{type 'float' cannot be narrowed to 'int' in initializer list}} |
| 223 | // expected-note@-3 {{insert an explicit cast to silence this issue}} |
| 224 | #endif |
Douglas Gregor | c2607b9 | 2009-05-21 21:38:12 +0000 | [diff] [blame] | 225 | } |
| 226 | }; |
| 227 | |
| 228 | struct APair { |
| 229 | int *x; |
| 230 | const float *y; |
| 231 | }; |
| 232 | |
| 233 | template struct InitList1<int[1], float>; |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 234 | #if __cplusplus >= 201103L |
| 235 | // expected-note@-2 {{instantiation of member function}} |
| 236 | #endif |
Douglas Gregor | c2607b9 | 2009-05-21 21:38:12 +0000 | [diff] [blame] | 237 | template struct InitList1<APair, int*>; |
| 238 | |
| 239 | template<typename T, typename Val1, typename Val2> |
| 240 | struct InitList2 { |
| 241 | void f(Val1 val1, Val2 val2) { |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 242 | T x = { val1, val2 }; // expected-error{{cannot initialize}} |
Douglas Gregor | c2607b9 | 2009-05-21 21:38:12 +0000 | [diff] [blame] | 243 | } |
| 244 | }; |
| 245 | |
| 246 | template struct InitList2<APair, int*, float*>; |
| 247 | template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}} |
Douglas Gregor | a8db954 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 248 | |
| 249 | // --------------------------------------------------------------------- |
| 250 | // member references |
| 251 | // --------------------------------------------------------------------- |
| 252 | template<typename T, typename Result> |
| 253 | struct DotMemRef0 { |
| 254 | void f(T t) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 255 | Result result = t.m; // expected-error{{non-const lvalue reference to type}} |
Douglas Gregor | a8db954 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 256 | } |
| 257 | }; |
| 258 | |
| 259 | struct MemInt { |
| 260 | int m; |
| 261 | }; |
| 262 | |
| 263 | struct InheritsMemInt : MemInt { }; |
| 264 | |
| 265 | struct MemIntFunc { |
| 266 | static int m(int); |
| 267 | }; |
| 268 | |
| 269 | template struct DotMemRef0<MemInt, int&>; |
| 270 | template struct DotMemRef0<InheritsMemInt, int&>; |
| 271 | template struct DotMemRef0<MemIntFunc, int (*)(int)>; |
| 272 | template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}} |
| 273 | |
| 274 | template<typename T, typename Result> |
| 275 | struct ArrowMemRef0 { |
| 276 | void f(T t) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 277 | Result result = t->m; // expected-error 2{{non-const lvalue reference}} |
Douglas Gregor | a8db954 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 278 | } |
| 279 | }; |
| 280 | |
| 281 | template<typename T> |
| 282 | struct ArrowWrapper { |
| 283 | T operator->(); |
| 284 | }; |
| 285 | |
| 286 | template struct ArrowMemRef0<MemInt*, int&>; |
| 287 | template struct ArrowMemRef0<InheritsMemInt*, int&>; |
| 288 | template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>; |
| 289 | template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}} |
| 290 | |
| 291 | template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>; |
| 292 | template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>; |
| 293 | template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>; |
| 294 | template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}} |
| 295 | template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>; |
| 296 | |
Richard Smith | ae1bab5 | 2011-10-26 06:49:26 +0000 | [diff] [blame] | 297 | struct UnresolvedMemRefArray { |
| 298 | int f(int); |
| 299 | int f(char); |
| 300 | }; |
| 301 | UnresolvedMemRefArray Arr[10]; |
| 302 | template<typename U> int UnresolvedMemRefArrayT(U u) { |
| 303 | return Arr->f(u); |
| 304 | } |
| 305 | template int UnresolvedMemRefArrayT<int>(int); |
| 306 | |
Douglas Gregor | a8db954 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 307 | // FIXME: we should be able to return a MemInt without the reference! |
| 308 | MemInt &createMemInt(int); |
| 309 | |
| 310 | template<int N> |
| 311 | struct NonDepMemberExpr0 { |
| 312 | void f() { |
| 313 | createMemInt(N).m = N; |
| 314 | } |
| 315 | }; |
| 316 | |
| 317 | template struct NonDepMemberExpr0<0>; |
Douglas Gregor | efe7c39 | 2009-05-22 21:26:58 +0000 | [diff] [blame] | 318 | |
| 319 | template<typename T, typename Result> |
| 320 | struct MemberFuncCall0 { |
| 321 | void f(T t) { |
| 322 | Result result = t.f(); |
| 323 | } |
| 324 | }; |
| 325 | |
| 326 | template<typename T> |
| 327 | struct HasMemFunc0 { |
| 328 | T f(); |
| 329 | }; |
| 330 | |
| 331 | |
| 332 | template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>; |
| 333 | |
| 334 | template<typename Result> |
| 335 | struct ThisMemberFuncCall0 { |
| 336 | Result g(); |
| 337 | |
| 338 | void f() { |
| 339 | Result r1 = g(); |
| 340 | Result r2 = this->g(); |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | template struct ThisMemberFuncCall0<int&>; |
| 345 | |
| 346 | template<typename T> |
| 347 | struct NonDepMemberCall0 { |
| 348 | void foo(HasMemFunc0<int&> x) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 349 | T result = x.f(); // expected-error{{non-const lvalue reference}} |
Douglas Gregor | efe7c39 | 2009-05-22 21:26:58 +0000 | [diff] [blame] | 350 | } |
| 351 | }; |
| 352 | |
| 353 | template struct NonDepMemberCall0<int&>; |
| 354 | template struct NonDepMemberCall0<const int&>; |
| 355 | template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}} |
Douglas Gregor | 4a9f481 | 2009-05-22 23:47:06 +0000 | [diff] [blame] | 356 | |
| 357 | |
| 358 | template<typename T> |
| 359 | struct QualifiedDeclRef0 { |
| 360 | T f() { |
Chris Lattner | 53fa049 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 361 | return is_pod<X>::value; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'const bool'}} |
Douglas Gregor | 4a9f481 | 2009-05-22 23:47:06 +0000 | [diff] [blame] | 362 | } |
| 363 | }; |
| 364 | |
| 365 | template struct QualifiedDeclRef0<bool>; |
| 366 | template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}} |