Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2 | class X { |
| 3 | public: |
| 4 | operator bool(); |
| 5 | operator int() const; |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 6 | |
| 7 | bool f() { |
| 8 | return operator bool(); |
| 9 | } |
| 10 | |
| 11 | float g() { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 12 | return operator float(); // expected-error{{use of undeclared 'operator float'}} |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 13 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 14 | }; |
| 15 | |
| 16 | operator int(); // expected-error{{conversion function must be a non-static member function}} |
| 17 | |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 18 | operator int; // expected-error{{'operator int' cannot be the name of a variable or data member}} |
| 19 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 20 | typedef int func_type(int); |
| 21 | typedef int array_type[10]; |
| 22 | |
| 23 | class Y { |
| 24 | public: |
| 25 | void operator bool(int, ...) const; // expected-error{{conversion function cannot have a return type}} \ |
Chris Lattner | 6e47501 | 2009-04-25 08:35:12 +0000 | [diff] [blame] | 26 | // expected-error{{conversion function cannot have any parameters}} |
| 27 | |
| 28 | operator float(...) const; // expected-error{{conversion function cannot be variadic}} |
| 29 | |
| 30 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 31 | operator func_type(); // expected-error{{conversion function cannot convert to a function type}} |
| 32 | operator array_type(); // expected-error{{conversion function cannot convert to an array type}} |
| 33 | }; |
| 34 | |
| 35 | |
| 36 | typedef int INT; |
| 37 | typedef INT* INT_PTR; |
| 38 | |
| 39 | class Z { |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 40 | operator int(); // expected-note {{previous declaration is here}} |
| 41 | operator int**(); // expected-note {{previous declaration is here}} |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 42 | |
| 43 | operator INT(); // expected-error{{conversion function cannot be redeclared}} |
| 44 | operator INT_PTR*(); // expected-error{{conversion function cannot be redeclared}} |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | class A { }; |
| 49 | |
| 50 | class B : public A { |
| 51 | public: |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 52 | operator A&() const; // expected-warning{{conversion function converting 'B' to its base class 'A' will never be used}} |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 53 | operator const void() const; // expected-warning{{conversion function converting 'B' to 'const void' will never be used}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 54 | operator const B(); // expected-warning{{conversion function converting 'B' to itself will never be used}} |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 55 | }; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 56 | |
| 57 | // This used to crash Clang. |
| 58 | struct Flip; |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 59 | struct Flop { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 60 | Flop(); |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 61 | Flop(const Flip&); // expected-note{{candidate constructor}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 62 | }; |
| 63 | struct Flip { |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 64 | operator Flop() const; // expected-note{{candidate function}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 65 | }; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 66 | Flop flop = Flip(); // expected-error {{conversion from 'Flip' to 'Flop' is ambiguous}} |
Anders Carlsson | 2c59d3c | 2009-09-13 21:33:06 +0000 | [diff] [blame] | 67 | |
| 68 | // This tests that we don't add the second conversion declaration to the list of user conversions |
| 69 | struct C { |
| 70 | operator const char *() const; |
| 71 | }; |
| 72 | |
| 73 | C::operator const char*() const { return 0; } |
| 74 | |
| 75 | void f(const C& c) { |
| 76 | const char* v = c; |
| 77 | } |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 78 | |
| 79 | // Test. Conversion in base class is visible in derived class. |
| 80 | class XB { |
| 81 | public: |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 82 | operator int(); // expected-note {{candidate function}} |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | class Yb : public XB { |
| 86 | public: |
Fariborz Jahanian | 78cf9a2 | 2009-09-15 00:10:11 +0000 | [diff] [blame] | 87 | operator char(); // expected-note {{candidate function}} |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | void f(Yb& a) { |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 91 | if (a) { } // expected-error {{conversion from 'Yb' to 'bool' is ambiguous}} |
Fariborz Jahanian | b191e2d | 2009-09-14 20:41:01 +0000 | [diff] [blame] | 92 | int i = a; // OK. calls XB::operator int(); |
| 93 | char ch = a; // OK. calls Yb::operator char(); |
| 94 | } |
| 95 | |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 96 | // Test conversion + copy construction. |
| 97 | class AutoPtrRef { }; |
| 98 | |
| 99 | class AutoPtr { |
John McCall | b92caa5 | 2010-07-22 22:44:38 +0000 | [diff] [blame] | 100 | AutoPtr(AutoPtr &); // expected-note{{declared private here}} |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 101 | |
| 102 | public: |
| 103 | AutoPtr(); |
| 104 | AutoPtr(AutoPtrRef); |
| 105 | |
| 106 | operator AutoPtrRef(); |
| 107 | }; |
| 108 | |
| 109 | AutoPtr make_auto_ptr(); |
| 110 | |
| 111 | AutoPtr test_auto_ptr(bool Cond) { |
| 112 | AutoPtr p1( make_auto_ptr() ); |
| 113 | |
| 114 | AutoPtr p; |
| 115 | if (Cond) |
John McCall | b92caa5 | 2010-07-22 22:44:38 +0000 | [diff] [blame] | 116 | return p; // expected-error{{calling a private constructor}} |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 117 | |
| 118 | return AutoPtr(); |
| 119 | } |
| 120 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 121 | struct A1 { |
| 122 | A1(const char *); |
| 123 | ~A1(); |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 124 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 125 | private: |
John McCall | b92caa5 | 2010-07-22 22:44:38 +0000 | [diff] [blame] | 126 | A1(const A1&); // expected-note 2 {{declared private here}} |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | A1 f() { |
John McCall | b92caa5 | 2010-07-22 22:44:38 +0000 | [diff] [blame] | 130 | // FIXME: redundant diagnostics! |
| 131 | return "Hello"; // expected-error {{calling a private constructor}} expected-warning {{an accessible copy constructor}} |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 132 | } |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 133 | |
| 134 | namespace source_locations { |
| 135 | template<typename T> |
| 136 | struct sneaky_int { |
| 137 | typedef int type; |
| 138 | }; |
| 139 | |
| 140 | template<typename T, typename U> |
| 141 | struct A { }; |
| 142 | |
| 143 | template<typename T> |
| 144 | struct A<T, T> : A<T, int> { }; |
| 145 | |
| 146 | struct E { |
| 147 | template<typename T> |
| 148 | operator A<T, typename sneaky_int<T>::type>&() const; // expected-note{{candidate function}} |
| 149 | }; |
| 150 | |
| 151 | void f() { |
| 152 | A<float, float> &af = E(); // expected-error{{no viable conversion}} |
| 153 | A<float, int> &af2 = E(); |
| 154 | const A<float, int> &caf2 = E(); |
| 155 | } |
| 156 | |
| 157 | // Check |
| 158 | template<typename T> |
| 159 | struct E2 { |
| 160 | operator T |
| 161 | * // expected-error{{pointer to a reference}} |
| 162 | () const; |
| 163 | }; |
| 164 | |
| 165 | E2<int&> e2i; // expected-note{{in instantiation}} |
| 166 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 167 | |
| 168 | namespace crazy_declarators { |
| 169 | struct A { |
| 170 | (&operator bool())(); // expected-error {{must use a typedef to declare a conversion to 'bool (&)()'}} |
| 171 | |
| 172 | // FIXME: This diagnostic is misleading (the correct spelling |
| 173 | // would be 'operator int*'), but it's a corner case of a |
| 174 | // rarely-used syntax extension. |
| 175 | *operator int(); // expected-error {{must use a typedef to declare a conversion to 'int *'}} |
| 176 | }; |
| 177 | } |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 178 | |
| 179 | namespace smart_ptr { |
| 180 | class Y { |
| 181 | class YRef { }; |
| 182 | |
| 183 | Y(Y&); |
| 184 | |
| 185 | public: |
| 186 | Y(); |
| 187 | Y(YRef); |
| 188 | |
| 189 | operator YRef(); // expected-note{{candidate function}} |
| 190 | }; |
| 191 | |
| 192 | struct X { // expected-note{{candidate constructor (the implicit copy constructor) not}} |
| 193 | explicit X(Y); |
| 194 | }; |
| 195 | |
| 196 | Y make_Y(); |
| 197 | |
| 198 | X f() { |
| 199 | X x = make_Y(); // expected-error{{no viable conversion from 'smart_ptr::Y' to 'smart_ptr::X'}} |
| 200 | X x2(make_Y()); |
| 201 | return X(Y()); |
| 202 | } |
| 203 | } |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 204 | |
| 205 | struct Any { |
| 206 | Any(...); |
| 207 | }; |
| 208 | |
| 209 | struct Other { |
| 210 | Other(const Other &); |
| 211 | Other(); |
| 212 | }; |
| 213 | |
| 214 | void test_any() { |
| 215 | Any any = Other(); // expected-error{{cannot pass object of non-POD type 'Other' through variadic constructor; call will abort at runtime}} |
| 216 | } |
Douglas Gregor | 604eb65 | 2010-08-11 02:15:33 +0000 | [diff] [blame] | 217 | |
| 218 | namespace PR7055 { |
| 219 | // Make sure that we don't allow too many conversions in an |
| 220 | // auto_ptr-like template. In particular, we can't create multiple |
| 221 | // temporary objects when binding to a reference. |
| 222 | struct auto_ptr { |
| 223 | struct auto_ptr_ref { }; |
| 224 | |
| 225 | auto_ptr(auto_ptr&); |
| 226 | auto_ptr(auto_ptr_ref); |
| 227 | explicit auto_ptr(int *); |
| 228 | |
| 229 | operator auto_ptr_ref(); |
| 230 | }; |
| 231 | |
| 232 | struct X { |
| 233 | X(auto_ptr); |
| 234 | }; |
| 235 | |
| 236 | X f() { |
| 237 | X x(auto_ptr(new int)); |
| 238 | return X(auto_ptr(new int)); |
| 239 | } |
| 240 | |
| 241 | auto_ptr foo(); |
| 242 | |
| 243 | X e(foo()); |
| 244 | |
| 245 | struct Y { |
| 246 | Y(X); |
| 247 | }; |
| 248 | |
| 249 | Y f2(foo()); |
| 250 | } |
Douglas Gregor | 4e938f57b | 2010-08-18 21:25:30 +0000 | [diff] [blame] | 251 | |
| 252 | namespace PR7934 { |
| 253 | typedef unsigned char uint8; |
| 254 | |
| 255 | struct MutablePtr { |
| 256 | MutablePtr() : ptr(0) {} |
| 257 | void *ptr; |
| 258 | |
| 259 | operator void*() { return ptr; } |
| 260 | |
| 261 | private: |
| 262 | operator uint8*() { return reinterpret_cast<uint8*>(ptr); } |
| 263 | operator const char*() const { return reinterpret_cast<const char*>(ptr); } |
| 264 | }; |
| 265 | |
| 266 | void fake_memcpy(const void *); |
| 267 | |
| 268 | void use() { |
| 269 | MutablePtr ptr; |
| 270 | fake_memcpy(ptr); |
| 271 | } |
| 272 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 273 | |
| 274 | namespace rdar8018274 { |
| 275 | struct X { }; |
| 276 | struct Y { |
| 277 | operator const struct X *() const; |
| 278 | }; |
| 279 | |
| 280 | struct Z : Y { |
| 281 | operator struct X * (); |
| 282 | }; |
| 283 | |
| 284 | void test() { |
| 285 | Z x; |
| 286 | (void) (x != __null); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | struct Base { |
| 291 | operator int(); |
| 292 | }; |
| 293 | |
| 294 | struct Derived1 : Base { }; |
| 295 | |
| 296 | struct Derived2 : Base { }; |
| 297 | |
| 298 | struct SuperDerived : Derived1, Derived2 { |
| 299 | using Derived1::operator int; |
| 300 | }; |
| 301 | |
| 302 | struct UeberDerived : SuperDerived { |
| 303 | operator long(); |
| 304 | }; |
| 305 | |
| 306 | void test2(UeberDerived ud) { |
| 307 | int i = ud; // expected-error{{ambiguous conversion from derived class 'rdar8018274::SuperDerived' to base class 'rdar8018274::Base'}} |
| 308 | } |
Douglas Gregor | 06ebc98 | 2010-08-19 17:02:01 +0000 | [diff] [blame] | 309 | |
| 310 | struct Base2 { |
| 311 | operator int(); |
| 312 | }; |
| 313 | |
| 314 | struct Base3 { |
| 315 | operator int(); |
| 316 | }; |
| 317 | |
| 318 | struct Derived23 : Base2, Base3 { |
| 319 | using Base2::operator int; |
| 320 | }; |
| 321 | |
| 322 | struct ExtraDerived23 : Derived23 { }; |
| 323 | |
| 324 | void test3(ExtraDerived23 ed) { |
| 325 | int i = ed; |
| 326 | } |
Douglas Gregor | c774b2f | 2010-08-19 15:57:50 +0000 | [diff] [blame] | 327 | } |
Douglas Gregor | da0fd9a | 2010-09-12 07:22:28 +0000 | [diff] [blame] | 328 | |
| 329 | namespace PR8065 { |
| 330 | template <typename T> struct Iterator; |
| 331 | template <typename T> struct Container; |
| 332 | |
| 333 | template<> |
| 334 | struct Iterator<int> { |
| 335 | typedef Container<int> container_type; |
| 336 | }; |
| 337 | |
| 338 | template <typename T> |
| 339 | struct Container { |
| 340 | typedef typename Iterator<T>::container_type X; |
| 341 | operator X(void) { return X(); } |
| 342 | }; |
| 343 | |
| 344 | Container<int> test; |
| 345 | } |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 346 | |
| 347 | namespace PR8034 { |
| 348 | struct C { |
| 349 | operator int(); |
| 350 | |
| 351 | private: |
| 352 | template <typename T> operator T(); |
| 353 | }; |
| 354 | int x = C().operator int(); |
| 355 | } |