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}} |
| 53 | operator const void() const; // expected-warning{{conversion function converting 'B' to 'void const' will never be used}} |
| 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 { |
| 100 | // FIXME: Using 'unavailable' since we do not have access control yet. |
| 101 | // FIXME: The error message isn't so good. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 102 | AutoPtr(AutoPtr &) __attribute__((unavailable)); // expected-note{{explicitly marked}} |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 103 | |
| 104 | public: |
| 105 | AutoPtr(); |
| 106 | AutoPtr(AutoPtrRef); |
| 107 | |
| 108 | operator AutoPtrRef(); |
| 109 | }; |
| 110 | |
| 111 | AutoPtr make_auto_ptr(); |
| 112 | |
| 113 | AutoPtr test_auto_ptr(bool Cond) { |
| 114 | AutoPtr p1( make_auto_ptr() ); |
| 115 | |
| 116 | AutoPtr p; |
| 117 | if (Cond) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 118 | return p; // expected-error{{call to deleted constructor}} |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 119 | |
| 120 | return AutoPtr(); |
| 121 | } |
| 122 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 123 | struct A1 { |
| 124 | A1(const char *); |
| 125 | ~A1(); |
Douglas Gregor | 79b680e | 2009-11-13 18:44:21 +0000 | [diff] [blame] | 126 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 127 | private: |
| 128 | A1(const A1&) __attribute__((unavailable)); // expected-note{{here}} |
| 129 | }; |
| 130 | |
| 131 | A1 f() { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 132 | return "Hello"; // expected-error{{invokes deleted constructor}} |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 133 | } |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 134 | |
| 135 | namespace source_locations { |
| 136 | template<typename T> |
| 137 | struct sneaky_int { |
| 138 | typedef int type; |
| 139 | }; |
| 140 | |
| 141 | template<typename T, typename U> |
| 142 | struct A { }; |
| 143 | |
| 144 | template<typename T> |
| 145 | struct A<T, T> : A<T, int> { }; |
| 146 | |
| 147 | struct E { |
| 148 | template<typename T> |
| 149 | operator A<T, typename sneaky_int<T>::type>&() const; // expected-note{{candidate function}} |
| 150 | }; |
| 151 | |
| 152 | void f() { |
| 153 | A<float, float> &af = E(); // expected-error{{no viable conversion}} |
| 154 | A<float, int> &af2 = E(); |
| 155 | const A<float, int> &caf2 = E(); |
| 156 | } |
| 157 | |
| 158 | // Check |
| 159 | template<typename T> |
| 160 | struct E2 { |
| 161 | operator T |
| 162 | * // expected-error{{pointer to a reference}} |
| 163 | () const; |
| 164 | }; |
| 165 | |
| 166 | E2<int&> e2i; // expected-note{{in instantiation}} |
| 167 | } |
John McCall | a3f8137 | 2010-04-13 00:04:31 +0000 | [diff] [blame] | 168 | |
| 169 | namespace crazy_declarators { |
| 170 | struct A { |
| 171 | (&operator bool())(); // expected-error {{must use a typedef to declare a conversion to 'bool (&)()'}} |
| 172 | |
| 173 | // FIXME: This diagnostic is misleading (the correct spelling |
| 174 | // would be 'operator int*'), but it's a corner case of a |
| 175 | // rarely-used syntax extension. |
| 176 | *operator int(); // expected-error {{must use a typedef to declare a conversion to 'int *'}} |
| 177 | }; |
| 178 | } |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 179 | |
| 180 | namespace smart_ptr { |
| 181 | class Y { |
| 182 | class YRef { }; |
| 183 | |
| 184 | Y(Y&); |
| 185 | |
| 186 | public: |
| 187 | Y(); |
| 188 | Y(YRef); |
| 189 | |
| 190 | operator YRef(); // expected-note{{candidate function}} |
| 191 | }; |
| 192 | |
| 193 | struct X { // expected-note{{candidate constructor (the implicit copy constructor) not}} |
| 194 | explicit X(Y); |
| 195 | }; |
| 196 | |
| 197 | Y make_Y(); |
| 198 | |
| 199 | X f() { |
| 200 | X x = make_Y(); // expected-error{{no viable conversion from 'smart_ptr::Y' to 'smart_ptr::X'}} |
| 201 | X x2(make_Y()); |
| 202 | return X(Y()); |
| 203 | } |
| 204 | } |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 205 | |
| 206 | struct Any { |
| 207 | Any(...); |
| 208 | }; |
| 209 | |
| 210 | struct Other { |
| 211 | Other(const Other &); |
| 212 | Other(); |
| 213 | }; |
| 214 | |
| 215 | void test_any() { |
| 216 | Any any = Other(); // expected-error{{cannot pass object of non-POD type 'Other' through variadic constructor; call will abort at runtime}} |
| 217 | } |