Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 1 | // RUN: clang-cc -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 { |
| 31 | bool operator==(Z&); // expected-note{{candidate function}} |
| 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 | |
| 36 | bool operator==(A&, Z&); // expected-note{{candidate function}} |
| 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:}} |
| 41 | ac == z; // expected-error{{invalid operands to binary expression ('struct A const' and 'struct Z')}} |
| 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 | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 70 | float &f4 = (enum1 == enum2); // expected-error{{non-const lvalue reference to type 'float' cannot be initialized with 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 | |
| 73 | |
| 74 | struct PostInc { |
| 75 | PostInc operator++(int); |
| 76 | PostInc& operator++(); |
| 77 | }; |
| 78 | |
| 79 | struct PostDec { |
| 80 | PostDec operator--(int); |
| 81 | PostDec& operator--(); |
| 82 | }; |
| 83 | |
| 84 | void incdec_test(PostInc pi, PostDec pd) { |
| 85 | const PostInc& pi1 = pi++; |
| 86 | const PostDec& pd1 = pd--; |
| 87 | PostInc &pi2 = ++pi; |
| 88 | PostDec &pd2 = --pd; |
| 89 | } |
| 90 | |
| 91 | struct SmartPtr { |
| 92 | int& operator*(); |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 93 | long& operator*() const volatile; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 96 | void test_smartptr(SmartPtr ptr, const SmartPtr cptr, |
| 97 | const volatile SmartPtr cvptr) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 98 | int &ir = *ptr; |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 99 | long &lr = *cptr; |
| 100 | long &lr2 = *cvptr; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 101 | } |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | struct ArrayLike { |
| 105 | int& operator[](int); |
| 106 | }; |
| 107 | |
| 108 | void test_arraylike(ArrayLike a) { |
| 109 | int& ir = a[17]; |
| 110 | } |
| 111 | |
| 112 | struct SmartRef { |
| 113 | int* operator&(); |
| 114 | }; |
| 115 | |
| 116 | void test_smartref(SmartRef r) { |
| 117 | int* ip = &r; |
| 118 | } |
| 119 | |
| 120 | bool& operator,(X, Y); |
| 121 | |
| 122 | void test_comma(X x, Y y) { |
| 123 | bool& b1 = (x, y); |
| 124 | X& xr = (x, x); |
| 125 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 126 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 127 | struct Callable { |
| 128 | int& operator()(int, double = 2.71828); // expected-note{{candidate function}} |
| 129 | float& operator()(int, double, long, ...); // expected-note{{candidate function}} |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 130 | |
| 131 | double& operator()(float); // expected-note{{candidate function}} |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 134 | struct Callable2 { |
| 135 | int& operator()(int i = 0); |
| 136 | double& operator()(...) const; |
| 137 | }; |
| 138 | |
| 139 | void test_callable(Callable c, Callable2 c2, const Callable2& c2c) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 140 | int &ir = c(1); |
| 141 | float &fr = c(1, 3.14159, 17, 42); |
| 142 | |
| 143 | c(); // expected-error{{no matching function for call to object of type 'struct Callable'; candidates are:}} |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 144 | |
| 145 | double &dr = c(1.0f); |
| 146 | |
| 147 | int &ir2 = c2(); |
| 148 | int &ir3 = c2(1); |
| 149 | double &fr2 = c2c(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 150 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 151 | |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 152 | typedef float FLOAT; |
| 153 | typedef int& INTREF; |
| 154 | typedef INTREF Func1(FLOAT, double); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 155 | typedef float& Func2(int, double); |
| 156 | |
| 157 | struct ConvertToFunc { |
Fariborz Jahanian | 7605618 | 2009-09-29 20:28:06 +0000 | [diff] [blame] | 158 | operator Func1*(); // expected-note{{conversion candidate of type 'INTREF (*)(float, double)'}} |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 159 | operator Func2&(); // expected-note{{conversion candidate of type 'float &(&)(int, double)'}} |
Douglas Gregor | 9ebae31 | 2008-11-19 22:59:19 +0000 | [diff] [blame] | 160 | void operator()(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | void test_funcptr_call(ConvertToFunc ctf) { |
| 164 | int &i1 = ctf(1.0f, 2.0); |
| 165 | float &f2 = ctf((short int)1, 1.0f); |
| 166 | ctf((long int)17, 2.0); // expected-error{{error: call to object of type 'struct ConvertToFunc' is ambiguous; candidates are:}} |
Douglas Gregor | 9ebae31 | 2008-11-19 22:59:19 +0000 | [diff] [blame] | 167 | ctf(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 168 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 169 | |
| 170 | struct HasMember { |
| 171 | int m; |
| 172 | }; |
| 173 | |
| 174 | struct Arrow1 { |
| 175 | HasMember* operator->(); |
| 176 | }; |
| 177 | |
| 178 | struct Arrow2 { |
| 179 | Arrow1 operator->(); // expected-note{{candidate function}} |
| 180 | }; |
| 181 | |
| 182 | void test_arrow(Arrow1 a1, Arrow2 a2, const Arrow2 a3) { |
| 183 | int &i1 = a1->m; |
| 184 | int &i2 = a2->m; |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 185 | a3->m; // expected-error{{no viable overloaded 'operator->'; candidate is}} |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 186 | } |
Douglas Gregor | e63ef48 | 2009-01-13 00:11:19 +0000 | [diff] [blame] | 187 | |
| 188 | struct CopyConBase { |
| 189 | }; |
| 190 | |
| 191 | struct CopyCon : public CopyConBase { |
| 192 | CopyCon(const CopyConBase &Base); |
| 193 | |
| 194 | CopyCon(const CopyConBase *Base) { |
| 195 | *this = *Base; |
| 196 | } |
| 197 | }; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 198 | |
| 199 | namespace N { |
| 200 | struct X { }; |
| 201 | } |
| 202 | |
| 203 | namespace M { |
| 204 | N::X operator+(N::X, N::X); |
| 205 | } |
| 206 | |
| 207 | namespace M { |
| 208 | void test_X(N::X x) { |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 209 | (void)(x + x); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 212 | |
| 213 | struct AA { bool operator!=(AA&); }; |
| 214 | struct BB : AA {}; |
| 215 | bool x(BB y, BB z) { return y != z; } |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 216 | |
| 217 | |
| 218 | struct AX { |
| 219 | AX& operator ->(); |
| 220 | int b; |
| 221 | }; |
| 222 | |
| 223 | void m() { |
| 224 | AX a; |
| 225 | a->b = 0; // expected-error {{circular pointer delegation detected}} |
| 226 | } |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame^] | 227 | |
| 228 | struct CircA { |
| 229 | struct CircB& operator->(); |
| 230 | int val; |
| 231 | }; |
| 232 | struct CircB { |
| 233 | struct CircC& operator->(); |
| 234 | }; |
| 235 | struct CircC { |
| 236 | struct CircA& operator->(); |
| 237 | }; |
| 238 | |
| 239 | void circ() { |
| 240 | CircA a; |
| 241 | a->val = 0; // expected-error {{circular pointer delegation detected}} |
| 242 | } |