blob: 40ac33b8eb0df8c991c273a48f9d2d2e7d66890f [file] [log] [blame]
Larisse Voufo39a1e502013-08-06 01:03:05 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002class X {
3public:
4 operator bool();
5 operator int() const;
Douglas Gregorae2fbad2008-11-17 20:34:05 +00006
7 bool f() {
8 return operator bool();
9 }
10
11 float g() {
John McCalld681c392009-12-16 08:11:27 +000012 return operator float(); // expected-error{{use of undeclared 'operator float'}}
Douglas Gregorae2fbad2008-11-17 20:34:05 +000013 }
Eli Friedman600bc242013-06-20 20:58:02 +000014
15 static operator short(); // expected-error{{conversion function must be a non-static member function}}
Douglas Gregordbc5daf2008-11-07 20:08:42 +000016};
17
18operator int(); // expected-error{{conversion function must be a non-static member function}}
19
Douglas Gregor92751d42008-11-17 22:58:34 +000020operator int; // expected-error{{'operator int' cannot be the name of a variable or data member}}
21
Douglas Gregordbc5daf2008-11-07 20:08:42 +000022typedef int func_type(int);
23typedef int array_type[10];
24
25class Y {
26public:
27 void operator bool(int, ...) const; // expected-error{{conversion function cannot have a return type}} \
Chris Lattnerb41df4f2009-04-25 08:35:12 +000028 // expected-error{{conversion function cannot have any parameters}}
Nico Webercf596d82014-05-10 19:15:24 +000029
Nico Weber7aeab602014-05-11 00:28:16 +000030 operator bool(int a = 4, int b = 6) const; // expected-error{{conversion function cannot have any parameters}}
Nico Webercf596d82014-05-10 19:15:24 +000031
Chris Lattnerb41df4f2009-04-25 08:35:12 +000032
33 operator float(...) const; // expected-error{{conversion function cannot be variadic}}
34
35
Douglas Gregordbc5daf2008-11-07 20:08:42 +000036 operator func_type(); // expected-error{{conversion function cannot convert to a function type}}
37 operator array_type(); // expected-error{{conversion function cannot convert to an array type}}
38};
39
40
41typedef int INT;
42typedef INT* INT_PTR;
43
44class Z {
Chris Lattner0369c572008-11-23 23:12:31 +000045 operator int(); // expected-note {{previous declaration is here}}
46 operator int**(); // expected-note {{previous declaration is here}}
Douglas Gregordbc5daf2008-11-07 20:08:42 +000047
48 operator INT(); // expected-error{{conversion function cannot be redeclared}}
49 operator INT_PTR*(); // expected-error{{conversion function cannot be redeclared}}
50};
51
52
53class A { };
54
55class B : public A {
56public:
John McCall85f90552010-03-10 11:27:22 +000057 operator A&() const; // expected-warning{{conversion function converting 'B' to its base class 'A' will never be used}}
Chris Lattner53fa0492010-09-05 00:04:01 +000058 operator const void() const; // expected-warning{{conversion function converting 'B' to 'const void' will never be used}}
John McCall85f90552010-03-10 11:27:22 +000059 operator const B(); // expected-warning{{conversion function converting 'B' to itself will never be used}}
Douglas Gregordbc5daf2008-11-07 20:08:42 +000060};
Sebastian Redl1a99f442009-04-16 17:51:27 +000061
62// This used to crash Clang.
63struct Flip;
Douglas Gregorc779e992010-04-24 20:54:38 +000064struct Flop {
Sebastian Redl1a99f442009-04-16 17:51:27 +000065 Flop();
John McCallfd0b2f82010-01-06 09:43:14 +000066 Flop(const Flip&); // expected-note{{candidate constructor}}
Sebastian Redl1a99f442009-04-16 17:51:27 +000067};
68struct Flip {
Douglas Gregora4b592a2009-12-19 03:01:41 +000069 operator Flop() const; // expected-note{{candidate function}}
Sebastian Redl1a99f442009-04-16 17:51:27 +000070};
John McCall85f90552010-03-10 11:27:22 +000071Flop flop = Flip(); // expected-error {{conversion from 'Flip' to 'Flop' is ambiguous}}
Anders Carlsson1b12ed42009-09-13 21:33:06 +000072
73// This tests that we don't add the second conversion declaration to the list of user conversions
74struct C {
75 operator const char *() const;
76};
77
78C::operator const char*() const { return 0; }
79
80void f(const C& c) {
81 const char* v = c;
82}
Fariborz Jahanianf4061e32009-09-14 20:41:01 +000083
84// Test. Conversion in base class is visible in derived class.
85class XB {
86public:
Fariborz Jahanian19c73282009-09-15 00:10:11 +000087 operator int(); // expected-note {{candidate function}}
Fariborz Jahanianf4061e32009-09-14 20:41:01 +000088};
89
90class Yb : public XB {
91public:
Fariborz Jahanian19c73282009-09-15 00:10:11 +000092 operator char(); // expected-note {{candidate function}}
Fariborz Jahanianf4061e32009-09-14 20:41:01 +000093};
94
95void f(Yb& a) {
John McCall85f90552010-03-10 11:27:22 +000096 if (a) { } // expected-error {{conversion from 'Yb' to 'bool' is ambiguous}}
Fariborz Jahanianf4061e32009-09-14 20:41:01 +000097 int i = a; // OK. calls XB::operator int();
98 char ch = a; // OK. calls Yb::operator char();
99}
100
Douglas Gregor379d84b2009-11-13 18:44:21 +0000101// Test conversion + copy construction.
102class AutoPtrRef { };
103
104class AutoPtr {
John McCall7b5f0fe2010-07-22 22:44:38 +0000105 AutoPtr(AutoPtr &); // expected-note{{declared private here}}
Douglas Gregor379d84b2009-11-13 18:44:21 +0000106
107public:
108 AutoPtr();
109 AutoPtr(AutoPtrRef);
110
111 operator AutoPtrRef();
112};
113
114AutoPtr make_auto_ptr();
115
116AutoPtr test_auto_ptr(bool Cond) {
117 AutoPtr p1( make_auto_ptr() );
118
119 AutoPtr p;
120 if (Cond)
John McCall7b5f0fe2010-07-22 22:44:38 +0000121 return p; // expected-error{{calling a private constructor}}
Douglas Gregor379d84b2009-11-13 18:44:21 +0000122
123 return AutoPtr();
124}
125
Douglas Gregore1314a62009-12-18 05:02:21 +0000126struct A1 {
127 A1(const char *);
128 ~A1();
Douglas Gregor379d84b2009-11-13 18:44:21 +0000129
Douglas Gregore1314a62009-12-18 05:02:21 +0000130private:
John McCall7b5f0fe2010-07-22 22:44:38 +0000131 A1(const A1&); // expected-note 2 {{declared private here}}
Douglas Gregore1314a62009-12-18 05:02:21 +0000132};
133
134A1 f() {
John McCall7b5f0fe2010-07-22 22:44:38 +0000135 // FIXME: redundant diagnostics!
136 return "Hello"; // expected-error {{calling a private constructor}} expected-warning {{an accessible copy constructor}}
Douglas Gregore1314a62009-12-18 05:02:21 +0000137}
Douglas Gregor5c0066f2010-04-12 23:19:01 +0000138
139namespace source_locations {
140 template<typename T>
141 struct sneaky_int {
142 typedef int type;
143 };
144
145 template<typename T, typename U>
146 struct A { };
147
148 template<typename T>
149 struct A<T, T> : A<T, int> { };
150
151 struct E {
152 template<typename T>
153 operator A<T, typename sneaky_int<T>::type>&() const; // expected-note{{candidate function}}
154 };
155
156 void f() {
157 A<float, float> &af = E(); // expected-error{{no viable conversion}}
158 A<float, int> &af2 = E();
159 const A<float, int> &caf2 = E();
160 }
161
162 // Check
163 template<typename T>
164 struct E2 {
165 operator T
166 * // expected-error{{pointer to a reference}}
167 () const;
168 };
169
170 E2<int&> e2i; // expected-note{{in instantiation}}
171}
John McCall212fa2e2010-04-13 00:04:31 +0000172
173namespace crazy_declarators {
174 struct A {
175 (&operator bool())(); // expected-error {{must use a typedef to declare a conversion to 'bool (&)()'}}
176
177 // FIXME: This diagnostic is misleading (the correct spelling
178 // would be 'operator int*'), but it's a corner case of a
179 // rarely-used syntax extension.
180 *operator int(); // expected-error {{must use a typedef to declare a conversion to 'int *'}}
181 };
182}
Douglas Gregor5ab11652010-04-17 22:01:05 +0000183
184namespace smart_ptr {
185 class Y {
186 class YRef { };
187
188 Y(Y&);
189
190 public:
191 Y();
192 Y(YRef);
193
194 operator YRef(); // expected-note{{candidate function}}
195 };
196
197 struct X { // expected-note{{candidate constructor (the implicit copy constructor) not}}
198 explicit X(Y);
199 };
200
201 Y make_Y();
202
203 X f() {
204 X x = make_Y(); // expected-error{{no viable conversion from 'smart_ptr::Y' to 'smart_ptr::X'}}
205 X x2(make_Y());
206 return X(Y());
207 }
208}
Douglas Gregorc779e992010-04-24 20:54:38 +0000209
210struct Any {
211 Any(...);
212};
213
214struct Other {
215 Other(const Other &);
216 Other();
217};
218
219void test_any() {
220 Any any = Other(); // expected-error{{cannot pass object of non-POD type 'Other' through variadic constructor; call will abort at runtime}}
221}
Douglas Gregor836a7e82010-08-11 02:15:33 +0000222
223namespace PR7055 {
224 // Make sure that we don't allow too many conversions in an
225 // auto_ptr-like template. In particular, we can't create multiple
226 // temporary objects when binding to a reference.
227 struct auto_ptr {
228 struct auto_ptr_ref { };
229
230 auto_ptr(auto_ptr&);
231 auto_ptr(auto_ptr_ref);
232 explicit auto_ptr(int *);
233
234 operator auto_ptr_ref();
235 };
236
237 struct X {
238 X(auto_ptr);
239 };
240
241 X f() {
242 X x(auto_ptr(new int));
243 return X(auto_ptr(new int));
244 }
245
246 auto_ptr foo();
247
248 X e(foo());
249
250 struct Y {
251 Y(X);
252 };
253
254 Y f2(foo());
255}
Douglas Gregorfb640862010-08-18 21:25:30 +0000256
257namespace PR7934 {
258 typedef unsigned char uint8;
259
260 struct MutablePtr {
261 MutablePtr() : ptr(0) {}
262 void *ptr;
263
264 operator void*() { return ptr; }
265
266 private:
267 operator uint8*() { return reinterpret_cast<uint8*>(ptr); }
268 operator const char*() const { return reinterpret_cast<const char*>(ptr); }
269 };
270
271 void fake_memcpy(const void *);
272
273 void use() {
274 MutablePtr ptr;
275 fake_memcpy(ptr);
276 }
277}
Douglas Gregorc9ed4682010-08-19 15:57:50 +0000278
279namespace rdar8018274 {
280 struct X { };
281 struct Y {
282 operator const struct X *() const;
283 };
284
285 struct Z : Y {
286 operator struct X * ();
287 };
288
289 void test() {
290 Z x;
291 (void) (x != __null);
292 }
293
294
295 struct Base {
296 operator int();
297 };
298
299 struct Derived1 : Base { };
300
301 struct Derived2 : Base { };
302
303 struct SuperDerived : Derived1, Derived2 {
304 using Derived1::operator int;
305 };
306
307 struct UeberDerived : SuperDerived {
308 operator long();
309 };
310
311 void test2(UeberDerived ud) {
312 int i = ud; // expected-error{{ambiguous conversion from derived class 'rdar8018274::SuperDerived' to base class 'rdar8018274::Base'}}
313 }
Douglas Gregorc0afc672010-08-19 17:02:01 +0000314
315 struct Base2 {
316 operator int();
317 };
318
319 struct Base3 {
320 operator int();
321 };
322
323 struct Derived23 : Base2, Base3 {
324 using Base2::operator int;
325 };
326
327 struct ExtraDerived23 : Derived23 { };
328
329 void test3(ExtraDerived23 ed) {
330 int i = ed;
331 }
Douglas Gregorc9ed4682010-08-19 15:57:50 +0000332}
Douglas Gregor6309e3d2010-09-12 07:22:28 +0000333
334namespace PR8065 {
335 template <typename T> struct Iterator;
336 template <typename T> struct Container;
337
338 template<>
339 struct Iterator<int> {
340 typedef Container<int> container_type;
341 };
342
343 template <typename T>
344 struct Container {
345 typedef typename Iterator<T>::container_type X;
346 operator X(void) { return X(); }
347 };
348
349 Container<int> test;
350}
Douglas Gregord5b730c92010-09-12 08:07:23 +0000351
352namespace PR8034 {
353 struct C {
354 operator int();
355
356 private:
357 template <typename T> operator T();
358 };
359 int x = C().operator int();
360}
Douglas Gregord99609a2011-03-06 09:03:20 +0000361
362namespace PR9336 {
363 template<class T>
364 struct generic_list
365 {
366 template<class Container>
367 operator Container()
368 {
369 Container ar;
370 T* i;
371 ar[0]=*i;
372 return ar;
373 }
374 };
375
376 template<class T>
377 struct array
378 {
379 T& operator[](int);
380 const T& operator[](int)const;
381 };
382
383 generic_list<generic_list<int> > l;
384 array<array<int> > a = l;
385}
Richard Smith48d24642011-07-13 22:53:21 +0000386
387namespace PR8800 {
388 struct A;
389 struct C {
390 operator A&();
391 };
392 void f() {
393 C c;
394 A& a1(c);
395 A& a2 = c;
396 A& a3 = static_cast<A&>(c);
397 A& a4 = (A&)c;
398 }
399}
Richard Smith99fdf8d2012-05-06 00:04:32 +0000400
401namespace PR12712 {
402 struct A {};
403 struct B {
404 operator A();
405 operator A() const;
406 };
407 struct C : B {};
408
409 A f(const C c) { return c; }
410}
Richard Smith649c7b062014-01-08 00:56:48 +0000411
412namespace PR18234 {
413 struct A {
Ismail Pazarbasi025f4282014-03-07 22:36:23 +0000414 operator enum E { e } (); // expected-error {{'PR18234::A::E' cannot be defined in a type specifier}}
415 operator struct S { int n; } (); // expected-error {{'PR18234::A::S' cannot be defined in a type specifier}}
Richard Smith649c7b062014-01-08 00:56:48 +0000416 } a;
417 A::S s = a;
418 A::E e = a; // expected-note {{here}}
419 bool k1 = e == A::e; // expected-error {{no member named 'e'}}
420 bool k2 = e.n == 0;
421}