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