Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | d94546a | 2009-05-20 21:38:11 +0000 | [diff] [blame] | 2 | |
| 3 | // --------------------------------------------------------------------- |
| 4 | // C++ Functional Casts |
| 5 | // --------------------------------------------------------------------- |
| 6 | template<int N> |
| 7 | struct ValueInit0 { |
| 8 | int f() { |
| 9 | return int(); |
| 10 | } |
| 11 | }; |
| 12 | |
| 13 | template struct ValueInit0<5>; |
| 14 | |
| 15 | template<int N> |
| 16 | struct FunctionalCast0 { |
| 17 | int f() { |
| 18 | return int(N); |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | template struct FunctionalCast0<5>; |
| 23 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 24 | struct X { // expected-note 3 {{candidate function}} |
| 25 | X(int, int); // expected-note 3 {{candidate function}} |
Douglas Gregor | d94546a | 2009-05-20 21:38:11 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | template<int N, int M> |
| 29 | struct BuildTemporary0 { |
| 30 | X f() { |
| 31 | return X(N, M); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | template struct BuildTemporary0<5, 7>; |
Douglas Gregor | e06274d | 2009-05-20 21:51:01 +0000 | [diff] [blame] | 36 | |
| 37 | template<int N, int M> |
| 38 | struct Temporaries0 { |
| 39 | void f() { |
| 40 | (void)X(N, M); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | template struct Temporaries0<5, 7>; |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 45 | |
| 46 | // --------------------------------------------------------------------- |
Douglas Gregor | d0c0267 | 2009-05-21 17:21:12 +0000 | [diff] [blame] | 47 | // new/delete expressions |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 48 | // --------------------------------------------------------------------- |
| 49 | struct Y { }; |
| 50 | |
| 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 | |
| 61 | template struct New0<int>; |
| 62 | template struct New0<Y>; |
| 63 | template struct New0<X>; // expected-note{{instantiation}} |
| 64 | |
| 65 | template<typename T, typename Arg1> |
| 66 | struct New1 { |
| 67 | T* f(bool x, Arg1 a1) { |
| 68 | return new T(a1); // expected-error{{no matching}} |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | template struct New1<int, float>; |
| 73 | template struct New1<Y, Y>; |
| 74 | template struct New1<X, Y>; // expected-note{{instantiation}} |
| 75 | |
| 76 | template<typename T, typename Arg1, typename Arg2> |
| 77 | struct New2 { |
| 78 | T* f(bool x, Arg1 a1, Arg2 a2) { |
| 79 | return new T(a1, a2); // expected-error{{no matching}} |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | template struct New2<X, int, float>; |
| 84 | template struct New2<X, int, int*>; // expected-note{{instantiation}} |
| 85 | // FIXME: template struct New2<int, int, float>; |
Douglas Gregor | d0c0267 | 2009-05-21 17:21:12 +0000 | [diff] [blame] | 86 | |
Douglas Gregor | 5b5ad84 | 2009-12-22 17:13:37 +0000 | [diff] [blame] | 87 | // PR5833 |
| 88 | struct New3 { |
| 89 | New3(); |
| 90 | |
| 91 | void *operator new[](__SIZE_TYPE__) __attribute__((unavailable)); // expected-note{{explicitly made unavailable}} |
| 92 | }; |
| 93 | |
| 94 | template<class C> |
| 95 | void* object_creator() { |
| 96 | return new C(); // expected-error{{call to unavailable function 'operator new[]'}} |
| 97 | } |
| 98 | |
| 99 | template void *object_creator<New3[4]>(); // expected-note{{instantiation}} |
| 100 | |
Douglas Gregor | d0c0267 | 2009-05-21 17:21:12 +0000 | [diff] [blame] | 101 | template<typename T> |
| 102 | struct Delete0 { |
| 103 | void f(T t) { |
| 104 | delete t; // expected-error{{cannot delete}} |
| 105 | ::delete [] t; |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | template struct Delete0<int*>; |
| 110 | template struct Delete0<X*>; |
| 111 | template struct Delete0<int>; // expected-note{{instantiation}} |
Douglas Gregor | 42e5b50 | 2009-05-21 17:37:52 +0000 | [diff] [blame] | 112 | |
Douglas Gregor | a88cfbf | 2009-12-12 18:16:41 +0000 | [diff] [blame] | 113 | namespace PR5755 { |
| 114 | template <class T> |
| 115 | void Foo() { |
| 116 | char* p = 0; |
| 117 | delete[] p; |
| 118 | } |
| 119 | |
| 120 | void Test() { |
| 121 | Foo<int>(); |
| 122 | } |
| 123 | } |
| 124 | |
Douglas Gregor | 42e5b50 | 2009-05-21 17:37:52 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | 765ccba | 2009-12-23 21:06:06 +0000 | [diff] [blame^] | 136 | struct Incomplete; // expected-note 2{{forward}} |
Douglas Gregor | 42e5b50 | 2009-05-21 17:37:52 +0000 | [diff] [blame] | 137 | |
| 138 | template struct Throw1<int>; |
| 139 | template struct Throw1<int*>; |
| 140 | template struct Throw1<Incomplete*>; // expected-note{{instantiation}} |
| 141 | |
Douglas Gregor | 12d0c30 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 142 | // --------------------------------------------------------------------- |
| 143 | // typeid expressions |
| 144 | // --------------------------------------------------------------------- |
| 145 | |
Douglas Gregor | 12d0c30 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 146 | namespace std { |
| 147 | class type_info; |
| 148 | } |
| 149 | |
| 150 | template<typename T> |
| 151 | struct TypeId0 { |
| 152 | const std::type_info &f(T* ptr) { |
| 153 | if (ptr) |
| 154 | return typeid(ptr); |
| 155 | else |
Douglas Gregor | 765ccba | 2009-12-23 21:06:06 +0000 | [diff] [blame^] | 156 | return typeid(T); // expected-error{{'typeid' of incomplete type 'struct Incomplete'}} |
Douglas Gregor | 12d0c30 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 157 | } |
| 158 | }; |
| 159 | |
| 160 | struct Abstract { |
| 161 | virtual void f() = 0; |
| 162 | }; |
| 163 | |
| 164 | template struct TypeId0<int>; |
Douglas Gregor | 765ccba | 2009-12-23 21:06:06 +0000 | [diff] [blame^] | 165 | template struct TypeId0<Incomplete>; // expected-note{{instantiation of member function}} |
Douglas Gregor | 12d0c30 | 2009-05-21 18:34:44 +0000 | [diff] [blame] | 166 | template struct TypeId0<Abstract>; |
Douglas Gregor | 36bb03b | 2009-05-21 18:55:48 +0000 | [diff] [blame] | 167 | |
| 168 | // --------------------------------------------------------------------- |
| 169 | // type traits |
| 170 | // --------------------------------------------------------------------- |
| 171 | template<typename T> |
| 172 | struct is_pod { |
| 173 | static const bool value = __is_pod(T); |
| 174 | }; |
| 175 | |
| 176 | static const int is_pod0[is_pod<X>::value? -1 : 1]; |
| 177 | static const int is_pod1[is_pod<Y>::value? 1 : -1]; |
Douglas Gregor | ccb97f5 | 2009-05-21 21:38:12 +0000 | [diff] [blame] | 178 | |
| 179 | // --------------------------------------------------------------------- |
| 180 | // initializer lists |
| 181 | // --------------------------------------------------------------------- |
| 182 | template<typename T, typename Val1> |
| 183 | struct InitList1 { |
| 184 | void f(Val1 val1) { |
| 185 | T x = { val1 }; |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | struct APair { |
| 190 | int *x; |
| 191 | const float *y; |
| 192 | }; |
| 193 | |
| 194 | template struct InitList1<int[1], float>; |
| 195 | template struct InitList1<APair, int*>; |
| 196 | |
| 197 | template<typename T, typename Val1, typename Val2> |
| 198 | struct InitList2 { |
| 199 | void f(Val1 val1, Val2 val2) { |
| 200 | T x = { val1, val2 }; // expected-error{{incompatible}} |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | template struct InitList2<APair, int*, float*>; |
| 205 | template struct InitList2<APair, int*, double*>; // expected-note{{instantiation}} |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 206 | |
| 207 | // --------------------------------------------------------------------- |
| 208 | // member references |
| 209 | // --------------------------------------------------------------------- |
| 210 | template<typename T, typename Result> |
| 211 | struct DotMemRef0 { |
| 212 | void f(T t) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 213 | Result result = t.m; // expected-error{{non-const lvalue reference to type}} |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 214 | } |
| 215 | }; |
| 216 | |
| 217 | struct MemInt { |
| 218 | int m; |
| 219 | }; |
| 220 | |
| 221 | struct InheritsMemInt : MemInt { }; |
| 222 | |
| 223 | struct MemIntFunc { |
| 224 | static int m(int); |
| 225 | }; |
| 226 | |
| 227 | template struct DotMemRef0<MemInt, int&>; |
| 228 | template struct DotMemRef0<InheritsMemInt, int&>; |
| 229 | template struct DotMemRef0<MemIntFunc, int (*)(int)>; |
| 230 | template struct DotMemRef0<MemInt, float&>; // expected-note{{instantiation}} |
| 231 | |
| 232 | template<typename T, typename Result> |
| 233 | struct ArrowMemRef0 { |
| 234 | void f(T t) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 235 | Result result = t->m; // expected-error 2{{non-const lvalue reference}} |
Douglas Gregor | 1c0ca59 | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 236 | } |
| 237 | }; |
| 238 | |
| 239 | template<typename T> |
| 240 | struct ArrowWrapper { |
| 241 | T operator->(); |
| 242 | }; |
| 243 | |
| 244 | template struct ArrowMemRef0<MemInt*, int&>; |
| 245 | template struct ArrowMemRef0<InheritsMemInt*, int&>; |
| 246 | template struct ArrowMemRef0<MemIntFunc*, int (*)(int)>; |
| 247 | template struct ArrowMemRef0<MemInt*, float&>; // expected-note{{instantiation}} |
| 248 | |
| 249 | template struct ArrowMemRef0<ArrowWrapper<MemInt*>, int&>; |
| 250 | template struct ArrowMemRef0<ArrowWrapper<InheritsMemInt*>, int&>; |
| 251 | template struct ArrowMemRef0<ArrowWrapper<MemIntFunc*>, int (*)(int)>; |
| 252 | template struct ArrowMemRef0<ArrowWrapper<MemInt*>, float&>; // expected-note{{instantiation}} |
| 253 | template struct ArrowMemRef0<ArrowWrapper<ArrowWrapper<MemInt*> >, int&>; |
| 254 | |
| 255 | // FIXME: we should be able to return a MemInt without the reference! |
| 256 | MemInt &createMemInt(int); |
| 257 | |
| 258 | template<int N> |
| 259 | struct NonDepMemberExpr0 { |
| 260 | void f() { |
| 261 | createMemInt(N).m = N; |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | template struct NonDepMemberExpr0<0>; |
Douglas Gregor | 08d3e7c | 2009-05-22 21:26:58 +0000 | [diff] [blame] | 266 | |
| 267 | template<typename T, typename Result> |
| 268 | struct MemberFuncCall0 { |
| 269 | void f(T t) { |
| 270 | Result result = t.f(); |
| 271 | } |
| 272 | }; |
| 273 | |
| 274 | template<typename T> |
| 275 | struct HasMemFunc0 { |
| 276 | T f(); |
| 277 | }; |
| 278 | |
| 279 | |
| 280 | template struct MemberFuncCall0<HasMemFunc0<int&>, const int&>; |
| 281 | |
| 282 | template<typename Result> |
| 283 | struct ThisMemberFuncCall0 { |
| 284 | Result g(); |
| 285 | |
| 286 | void f() { |
| 287 | Result r1 = g(); |
| 288 | Result r2 = this->g(); |
| 289 | } |
| 290 | }; |
| 291 | |
| 292 | template struct ThisMemberFuncCall0<int&>; |
| 293 | |
| 294 | template<typename T> |
| 295 | struct NonDepMemberCall0 { |
| 296 | void foo(HasMemFunc0<int&> x) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 297 | T result = x.f(); // expected-error{{non-const lvalue reference}} |
Douglas Gregor | 08d3e7c | 2009-05-22 21:26:58 +0000 | [diff] [blame] | 298 | } |
| 299 | }; |
| 300 | |
| 301 | template struct NonDepMemberCall0<int&>; |
| 302 | template struct NonDepMemberCall0<const int&>; |
| 303 | template struct NonDepMemberCall0<float&>; // expected-note{{instantiation}} |
Douglas Gregor | e30d0bd | 2009-05-22 23:47:06 +0000 | [diff] [blame] | 304 | |
| 305 | |
| 306 | template<typename T> |
| 307 | struct QualifiedDeclRef0 { |
| 308 | T f() { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 309 | return is_pod<X>::value; // expected-error{{non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'bool const'}} |
Douglas Gregor | e30d0bd | 2009-05-22 23:47:06 +0000 | [diff] [blame] | 310 | } |
| 311 | }; |
| 312 | |
| 313 | template struct QualifiedDeclRef0<bool>; |
| 314 | template struct QualifiedDeclRef0<int&>; // expected-note{{instantiation}} |