Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 2 | class X { }; |
| 3 | |
| 4 | X operator+(X, X); |
| 5 | |
| 6 | void f(X x) { |
| 7 | x = x + x; |
| 8 | } |
| 9 | |
| 10 | struct Y; |
| 11 | struct Z; |
| 12 | |
| 13 | struct Y { |
| 14 | Y(const Z&); |
| 15 | }; |
| 16 | |
| 17 | struct Z { |
| 18 | Z(const Y&); |
| 19 | }; |
| 20 | |
| 21 | Y operator+(Y, Y); |
| 22 | bool operator-(Y, Y); // expected-note{{candidate function}} |
| 23 | bool operator-(Z, Z); // expected-note{{candidate function}} |
| 24 | |
| 25 | void g(Y y, Z z) { |
| 26 | y = y + z; |
| 27 | bool b = y - z; // expected-error{{use of overloaded operator '-' is ambiguous; candidates are:}} |
| 28 | } |
| 29 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 30 | struct A { |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 31 | bool operator==(Z&); // expected-note 2{{candidate function}} |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 32 | }; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 34 | A make_A(); |
| 35 | |
Douglas Gregor | 3307475 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 36 | bool operator==(A&, Z&); // expected-note 2{{candidate function}} |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 37 | |
| 38 | void h(A a, const A ac, Z z) { |
| 39 | make_A() == z; |
| 40 | a == z; // expected-error{{use of overloaded operator '==' is ambiguous; candidates are:}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 41 | ac == z; // expected-error{{invalid operands to binary expression ('A const' and 'Z')}} |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | struct B { |
| 45 | bool operator==(const B&) const; |
| 46 | |
| 47 | void test(Z z) { |
| 48 | make_A() == z; |
| 49 | } |
| 50 | }; |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 51 | |
| 52 | enum Enum1 { }; |
| 53 | enum Enum2 { }; |
| 54 | |
| 55 | struct E1 { |
| 56 | E1(Enum1) { } |
| 57 | }; |
| 58 | |
| 59 | struct E2 { |
| 60 | E2(Enum2); |
| 61 | }; |
| 62 | |
| 63 | // C++ [over.match.oper]p3 - enum restriction. |
| 64 | float& operator==(E1, E2); |
| 65 | |
| 66 | void enum_test(Enum1 enum1, Enum2 enum2, E1 e1, E2 e2) { |
| 67 | float &f1 = (e1 == e2); |
| 68 | float &f2 = (enum1 == e2); |
| 69 | float &f3 = (e1 == enum2); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 70 | float &f4 = (enum1 == enum2); // expected-error{{non-const lvalue reference to type 'float' cannot bind to a temporary of type 'bool'}} |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 71 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 72 | |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 73 | // PR5244 - Argument-dependent lookup would include the two operators below, |
| 74 | // which would break later assumptions and lead to a crash. |
| 75 | class pr5244_foo |
| 76 | { |
| 77 | pr5244_foo(int); |
| 78 | pr5244_foo(char); |
| 79 | }; |
| 80 | |
| 81 | bool operator==(const pr5244_foo& s1, const pr5244_foo& s2); |
| 82 | bool operator==(char c, const pr5244_foo& s); |
| 83 | |
| 84 | enum pr5244_bar |
| 85 | { |
| 86 | pr5244_BAR |
| 87 | }; |
| 88 | |
| 89 | class pr5244_baz |
| 90 | { |
John McCall | 7002f4c | 2010-04-09 19:03:51 +0000 | [diff] [blame] | 91 | public: |
Sebastian Redl | 644be85 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 92 | pr5244_bar quux; |
| 93 | }; |
| 94 | |
| 95 | void pr5244_barbaz() |
| 96 | { |
| 97 | pr5244_baz quuux; |
| 98 | (void)(pr5244_BAR == quuux.quux); |
| 99 | } |
| 100 | |
| 101 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 102 | |
| 103 | struct PostInc { |
| 104 | PostInc operator++(int); |
| 105 | PostInc& operator++(); |
| 106 | }; |
| 107 | |
| 108 | struct PostDec { |
| 109 | PostDec operator--(int); |
| 110 | PostDec& operator--(); |
| 111 | }; |
| 112 | |
| 113 | void incdec_test(PostInc pi, PostDec pd) { |
| 114 | const PostInc& pi1 = pi++; |
| 115 | const PostDec& pd1 = pd--; |
| 116 | PostInc &pi2 = ++pi; |
| 117 | PostDec &pd2 = --pd; |
| 118 | } |
| 119 | |
| 120 | struct SmartPtr { |
| 121 | int& operator*(); |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 122 | long& operator*() const volatile; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 125 | void test_smartptr(SmartPtr ptr, const SmartPtr cptr, |
| 126 | const volatile SmartPtr cvptr) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 127 | int &ir = *ptr; |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 128 | long &lr = *cptr; |
| 129 | long &lr2 = *cvptr; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 130 | } |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 131 | |
| 132 | |
| 133 | struct ArrayLike { |
| 134 | int& operator[](int); |
| 135 | }; |
| 136 | |
| 137 | void test_arraylike(ArrayLike a) { |
| 138 | int& ir = a[17]; |
| 139 | } |
| 140 | |
| 141 | struct SmartRef { |
| 142 | int* operator&(); |
| 143 | }; |
| 144 | |
| 145 | void test_smartref(SmartRef r) { |
| 146 | int* ip = &r; |
| 147 | } |
| 148 | |
| 149 | bool& operator,(X, Y); |
| 150 | |
| 151 | void test_comma(X x, Y y) { |
| 152 | bool& b1 = (x, y); |
| 153 | X& xr = (x, x); |
| 154 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 155 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 156 | struct Callable { |
| 157 | int& operator()(int, double = 2.71828); // expected-note{{candidate function}} |
| 158 | float& operator()(int, double, long, ...); // expected-note{{candidate function}} |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 159 | |
| 160 | double& operator()(float); // expected-note{{candidate function}} |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 163 | struct Callable2 { |
| 164 | int& operator()(int i = 0); |
| 165 | double& operator()(...) const; |
| 166 | }; |
| 167 | |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 168 | struct DerivesCallable : public Callable { |
| 169 | }; |
| 170 | |
| 171 | void test_callable(Callable c, Callable2 c2, const Callable2& c2c, |
| 172 | DerivesCallable dc) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 173 | int &ir = c(1); |
| 174 | float &fr = c(1, 3.14159, 17, 42); |
| 175 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 176 | c(); // expected-error{{no matching function for call to object of type 'Callable'}} |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 177 | |
| 178 | double &dr = c(1.0f); |
| 179 | |
| 180 | int &ir2 = c2(); |
| 181 | int &ir3 = c2(1); |
| 182 | double &fr2 = c2c(); |
Douglas Gregor | 593564b | 2009-11-15 07:48:03 +0000 | [diff] [blame] | 183 | |
| 184 | int &ir4 = dc(17); |
| 185 | double &fr3 = dc(3.14159f); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 186 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 187 | |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 188 | typedef float FLOAT; |
| 189 | typedef int& INTREF; |
| 190 | typedef INTREF Func1(FLOAT, double); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 191 | typedef float& Func2(int, double); |
| 192 | |
| 193 | struct ConvertToFunc { |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 194 | operator Func1*(); // expected-note 2{{conversion candidate of type 'INTREF (*)(FLOAT, double)'}} |
| 195 | operator Func2&(); // expected-note 2{{conversion candidate of type 'float &(&)(int, double)'}} |
Douglas Gregor | 9ebae31 | 2008-11-19 22:59:19 +0000 | [diff] [blame] | 196 | void operator()(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 199 | struct ConvertToFuncDerived : ConvertToFunc { }; |
| 200 | |
| 201 | void test_funcptr_call(ConvertToFunc ctf, ConvertToFuncDerived ctfd) { |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 202 | int &i1 = ctf(1.0f, 2.0); |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 203 | float &f1 = ctf((short int)1, 1.0f); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 204 | ctf((long int)17, 2.0); // expected-error{{call to object of type 'ConvertToFunc' is ambiguous}} |
Douglas Gregor | 9ebae31 | 2008-11-19 22:59:19 +0000 | [diff] [blame] | 205 | ctf(); |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 206 | |
| 207 | int &i2 = ctfd(1.0f, 2.0); |
| 208 | float &f2 = ctfd((short int)1, 1.0f); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 209 | ctfd((long int)17, 2.0); // expected-error{{call to object of type 'ConvertToFuncDerived' is ambiguous}} |
Douglas Gregor | 9007328 | 2010-01-11 19:36:35 +0000 | [diff] [blame] | 210 | ctfd(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 211 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 212 | |
| 213 | struct HasMember { |
| 214 | int m; |
| 215 | }; |
| 216 | |
| 217 | struct Arrow1 { |
| 218 | HasMember* operator->(); |
| 219 | }; |
| 220 | |
| 221 | struct Arrow2 { |
| 222 | Arrow1 operator->(); // expected-note{{candidate function}} |
| 223 | }; |
| 224 | |
| 225 | void test_arrow(Arrow1 a1, Arrow2 a2, const Arrow2 a3) { |
| 226 | int &i1 = a1->m; |
| 227 | int &i2 = a2->m; |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 228 | a3->m; // expected-error{{no viable overloaded 'operator->'; candidate is}} |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 229 | } |
Douglas Gregor | e63ef48 | 2009-01-13 00:11:19 +0000 | [diff] [blame] | 230 | |
| 231 | struct CopyConBase { |
| 232 | }; |
| 233 | |
| 234 | struct CopyCon : public CopyConBase { |
| 235 | CopyCon(const CopyConBase &Base); |
| 236 | |
| 237 | CopyCon(const CopyConBase *Base) { |
| 238 | *this = *Base; |
| 239 | } |
| 240 | }; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 241 | |
| 242 | namespace N { |
| 243 | struct X { }; |
| 244 | } |
| 245 | |
| 246 | namespace M { |
| 247 | N::X operator+(N::X, N::X); |
| 248 | } |
| 249 | |
| 250 | namespace M { |
| 251 | void test_X(N::X x) { |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 252 | (void)(x + x); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 255 | |
| 256 | struct AA { bool operator!=(AA&); }; |
| 257 | struct BB : AA {}; |
| 258 | bool x(BB y, BB z) { return y != z; } |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 259 | |
| 260 | |
| 261 | struct AX { |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 262 | AX& operator ->(); // expected-note {{declared at}} |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 263 | int b; |
| 264 | }; |
| 265 | |
| 266 | void m() { |
| 267 | AX a; |
| 268 | a->b = 0; // expected-error {{circular pointer delegation detected}} |
| 269 | } |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 270 | |
| 271 | struct CircA { |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 272 | struct CircB& operator->(); // expected-note {{declared at}} |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 273 | int val; |
| 274 | }; |
| 275 | struct CircB { |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 276 | struct CircC& operator->(); // expected-note {{declared at}} |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 277 | }; |
| 278 | struct CircC { |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 279 | struct CircA& operator->(); // expected-note {{declared at}} |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | void circ() { |
| 283 | CircA a; |
| 284 | a->val = 0; // expected-error {{circular pointer delegation detected}} |
| 285 | } |
Sebastian Redl | a65b551 | 2009-11-05 16:36:20 +0000 | [diff] [blame] | 286 | |
| 287 | // PR5360: Arrays should lead to built-in candidates for subscript. |
| 288 | typedef enum { |
| 289 | LastReg = 23, |
| 290 | } Register; |
| 291 | class RegAlloc { |
| 292 | int getPriority(Register r) { |
| 293 | return usepri[r]; |
| 294 | } |
| 295 | int usepri[LastReg + 1]; |
| 296 | }; |
Sebastian Redl | a9efada | 2009-11-18 20:39:26 +0000 | [diff] [blame] | 297 | |
| 298 | // PR5546: Don't generate incorrect and ambiguous overloads for multi-level |
| 299 | // arrays. |
| 300 | namespace pr5546 |
| 301 | { |
| 302 | enum { X }; |
| 303 | extern const char *const sMoveCommands[][2][2]; |
| 304 | const char* a() { return sMoveCommands[X][0][0]; } |
| 305 | const char* b() { return (*(sMoveCommands+X))[0][0]; } |
| 306 | } |
Sebastian Redl | 275c2b4 | 2009-11-18 23:10:33 +0000 | [diff] [blame] | 307 | |
| 308 | // PR5512 and its discussion |
| 309 | namespace pr5512 { |
| 310 | struct Y { |
| 311 | operator short(); |
| 312 | operator float(); |
| 313 | }; |
| 314 | void g_test(Y y) { |
| 315 | short s = 0; |
| 316 | // DR507, this should be ambiguous, but we special-case assignment |
| 317 | s = y; |
| 318 | // Note: DR507, this is ambiguous as specified |
| 319 | //s += y; |
| 320 | } |
| 321 | |
| 322 | struct S {}; |
| 323 | void operator +=(int&, S); |
| 324 | void f(S s) { |
| 325 | int i = 0; |
| 326 | i += s; |
| 327 | } |
| 328 | |
| 329 | struct A {operator int();}; |
| 330 | int a; |
| 331 | void b(A x) { |
| 332 | a += x; |
| 333 | } |
| 334 | } |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 335 | |
| 336 | // PR5900 |
| 337 | namespace pr5900 { |
| 338 | struct NotAnArray {}; |
| 339 | void test0() { |
| 340 | NotAnArray x; |
| 341 | x[0] = 0; // expected-error {{does not provide a subscript operator}} |
| 342 | } |
| 343 | |
| 344 | struct NonConstArray { |
| 345 | int operator[](unsigned); // expected-note {{candidate}} |
| 346 | }; |
| 347 | int test1() { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 348 | const NonConstArray x = NonConstArray(); |
John McCall | 1eb3e10 | 2010-01-07 02:04:15 +0000 | [diff] [blame] | 349 | return x[0]; // expected-error {{no viable overloaded operator[] for type}} |
| 350 | } |
| 351 | |
| 352 | // Not really part of this PR, but implemented at the same time. |
| 353 | struct NotAFunction {}; |
| 354 | void test2() { |
| 355 | NotAFunction x; |
| 356 | x(); // expected-error {{does not provide a call operator}} |
| 357 | } |
| 358 | } |