overloaded-operator.cpp revision 9007328043c9c17029e07c99d89d0488fb3da3a7
1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -verify %s 2eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorclass X { }; 3eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor 4eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas GregorX operator+(X, X); 5eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor 6eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorvoid f(X x) { 7eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor x = x + x; 8eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor} 9eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor 10eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorstruct Y; 11eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorstruct Z; 12eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor 13eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorstruct Y { 14eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor Y(const Z&); 15eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor}; 16eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor 17eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorstruct Z { 18eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor Z(const Y&); 19eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor}; 20eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor 21eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas GregorY operator+(Y, Y); 22eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorbool operator-(Y, Y); // expected-note{{candidate function}} 23eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorbool operator-(Z, Z); // expected-note{{candidate function}} 24eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor 25eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorvoid g(Y y, Z z) { 26eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor y = y + z; 27eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor bool b = y - z; // expected-error{{use of overloaded operator '-' is ambiguous; candidates are:}} 28eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor} 29eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor 3096176b3575823ea996c6140380dd17d9240c9766Douglas Gregorstruct A { 313307475eb0dd6e5d88be9392ea8247d0b6b812dfDouglas Gregor bool operator==(Z&); // expected-note 2{{candidate function}} 3296176b3575823ea996c6140380dd17d9240c9766Douglas Gregor}; 3396176b3575823ea996c6140380dd17d9240c9766Douglas Gregor 3496176b3575823ea996c6140380dd17d9240c9766Douglas GregorA make_A(); 35eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor 363307475eb0dd6e5d88be9392ea8247d0b6b812dfDouglas Gregorbool operator==(A&, Z&); // expected-note 2{{candidate function}} 3796176b3575823ea996c6140380dd17d9240c9766Douglas Gregor 3896176b3575823ea996c6140380dd17d9240c9766Douglas Gregorvoid h(A a, const A ac, Z z) { 3996176b3575823ea996c6140380dd17d9240c9766Douglas Gregor make_A() == z; 4096176b3575823ea996c6140380dd17d9240c9766Douglas Gregor a == z; // expected-error{{use of overloaded operator '==' is ambiguous; candidates are:}} 4196176b3575823ea996c6140380dd17d9240c9766Douglas Gregor ac == z; // expected-error{{invalid operands to binary expression ('struct A const' and 'struct Z')}} 4296176b3575823ea996c6140380dd17d9240c9766Douglas Gregor} 4396176b3575823ea996c6140380dd17d9240c9766Douglas Gregor 4496176b3575823ea996c6140380dd17d9240c9766Douglas Gregorstruct B { 4596176b3575823ea996c6140380dd17d9240c9766Douglas Gregor bool operator==(const B&) const; 4696176b3575823ea996c6140380dd17d9240c9766Douglas Gregor 4796176b3575823ea996c6140380dd17d9240c9766Douglas Gregor void test(Z z) { 4896176b3575823ea996c6140380dd17d9240c9766Douglas Gregor make_A() == z; 4996176b3575823ea996c6140380dd17d9240c9766Douglas Gregor } 5096176b3575823ea996c6140380dd17d9240c9766Douglas Gregor}; 51447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor 52447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregorenum Enum1 { }; 53447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregorenum Enum2 { }; 54447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor 55447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregorstruct E1 { 56447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor E1(Enum1) { } 57447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor}; 58447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor 59447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregorstruct E2 { 60447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor E2(Enum2); 61447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor}; 62447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor 63447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor// C++ [over.match.oper]p3 - enum restriction. 64447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregorfloat& operator==(E1, E2); 65447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor 66447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregorvoid enum_test(Enum1 enum1, Enum2 enum2, E1 e1, E2 e2) { 67447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor float &f1 = (e1 == e2); 68447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor float &f2 = (enum1 == e2); 69447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor float &f3 = (e1 == enum2); 7020093b4bf698f292c664676987541d5103b65b15Douglas Gregor float &f4 = (enum1 == enum2); // expected-error{{non-const lvalue reference to type 'float' cannot bind to a temporary of type 'bool'}} 71447b69e55e1098d8df46dd99f171bfaace9ff8a0Douglas Gregor} 7274253736184c0717a0649922551bf9d8b6815651Douglas Gregor 73644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl// PR5244 - Argument-dependent lookup would include the two operators below, 74644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl// which would break later assumptions and lead to a crash. 75644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redlclass pr5244_foo 76644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl{ 77644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl pr5244_foo(int); 78644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl pr5244_foo(char); 79644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl}; 80644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl 81644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redlbool operator==(const pr5244_foo& s1, const pr5244_foo& s2); 82644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redlbool operator==(char c, const pr5244_foo& s); 83644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl 84644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redlenum pr5244_bar 85644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl{ 86644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl pr5244_BAR 87644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl}; 88644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl 89644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redlclass pr5244_baz 90644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl{ 91644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl pr5244_bar quux; 92644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl}; 93644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl 94644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redlvoid pr5244_barbaz() 95644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl{ 96644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl pr5244_baz quuux; 97644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl (void)(pr5244_BAR == quuux.quux); 98644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl} 99644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl 100644be853b87cae94fcabaf309a5e482a8c291fb9Sebastian Redl 10174253736184c0717a0649922551bf9d8b6815651Douglas Gregor 10274253736184c0717a0649922551bf9d8b6815651Douglas Gregorstruct PostInc { 10374253736184c0717a0649922551bf9d8b6815651Douglas Gregor PostInc operator++(int); 10474253736184c0717a0649922551bf9d8b6815651Douglas Gregor PostInc& operator++(); 10574253736184c0717a0649922551bf9d8b6815651Douglas Gregor}; 10674253736184c0717a0649922551bf9d8b6815651Douglas Gregor 10774253736184c0717a0649922551bf9d8b6815651Douglas Gregorstruct PostDec { 10874253736184c0717a0649922551bf9d8b6815651Douglas Gregor PostDec operator--(int); 10974253736184c0717a0649922551bf9d8b6815651Douglas Gregor PostDec& operator--(); 11074253736184c0717a0649922551bf9d8b6815651Douglas Gregor}; 11174253736184c0717a0649922551bf9d8b6815651Douglas Gregor 11274253736184c0717a0649922551bf9d8b6815651Douglas Gregorvoid incdec_test(PostInc pi, PostDec pd) { 11374253736184c0717a0649922551bf9d8b6815651Douglas Gregor const PostInc& pi1 = pi++; 11474253736184c0717a0649922551bf9d8b6815651Douglas Gregor const PostDec& pd1 = pd--; 11574253736184c0717a0649922551bf9d8b6815651Douglas Gregor PostInc &pi2 = ++pi; 11674253736184c0717a0649922551bf9d8b6815651Douglas Gregor PostDec &pd2 = --pd; 11774253736184c0717a0649922551bf9d8b6815651Douglas Gregor} 11874253736184c0717a0649922551bf9d8b6815651Douglas Gregor 11974253736184c0717a0649922551bf9d8b6815651Douglas Gregorstruct SmartPtr { 12074253736184c0717a0649922551bf9d8b6815651Douglas Gregor int& operator*(); 1211ca50c3f541dd637063b9d186a7ea193e3440a48Douglas Gregor long& operator*() const volatile; 12274253736184c0717a0649922551bf9d8b6815651Douglas Gregor}; 12374253736184c0717a0649922551bf9d8b6815651Douglas Gregor 1241ca50c3f541dd637063b9d186a7ea193e3440a48Douglas Gregorvoid test_smartptr(SmartPtr ptr, const SmartPtr cptr, 1251ca50c3f541dd637063b9d186a7ea193e3440a48Douglas Gregor const volatile SmartPtr cvptr) { 12674253736184c0717a0649922551bf9d8b6815651Douglas Gregor int &ir = *ptr; 1271ca50c3f541dd637063b9d186a7ea193e3440a48Douglas Gregor long &lr = *cptr; 1281ca50c3f541dd637063b9d186a7ea193e3440a48Douglas Gregor long &lr2 = *cvptr; 12974253736184c0717a0649922551bf9d8b6815651Douglas Gregor} 130337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 131337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 132337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregorstruct ArrayLike { 133337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor int& operator[](int); 134337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor}; 135337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 136337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregorvoid test_arraylike(ArrayLike a) { 137337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor int& ir = a[17]; 138337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor} 139337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 140337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregorstruct SmartRef { 141337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor int* operator&(); 142337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor}; 143337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 144337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregorvoid test_smartref(SmartRef r) { 145337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor int* ip = &r; 146337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor} 147337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 148337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregorbool& operator,(X, Y); 149337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor 150337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregorvoid test_comma(X x, Y y) { 151337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor bool& b1 = (x, y); 152337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor X& xr = (x, x); 153337c6b9f5d502dc1c5acea628bf7bf9e828efc0eDouglas Gregor} 154f9eb905197e44ed5634205512074993f6f40470dDouglas Gregor 155f9eb905197e44ed5634205512074993f6f40470dDouglas Gregorstruct Callable { 156f9eb905197e44ed5634205512074993f6f40470dDouglas Gregor int& operator()(int, double = 2.71828); // expected-note{{candidate function}} 157f9eb905197e44ed5634205512074993f6f40470dDouglas Gregor float& operator()(int, double, long, ...); // expected-note{{candidate function}} 158518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor 159518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor double& operator()(float); // expected-note{{candidate function}} 160518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor}; 161518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor 162518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregorstruct Callable2 { 163518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor int& operator()(int i = 0); 164518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor double& operator()(...) const; 165f9eb905197e44ed5634205512074993f6f40470dDouglas Gregor}; 166f9eb905197e44ed5634205512074993f6f40470dDouglas Gregor 167593564ba94ff854b7a410a4ca17ad34a90c5b761Douglas Gregorstruct DerivesCallable : public Callable { 168593564ba94ff854b7a410a4ca17ad34a90c5b761Douglas Gregor}; 169593564ba94ff854b7a410a4ca17ad34a90c5b761Douglas Gregor 170593564ba94ff854b7a410a4ca17ad34a90c5b761Douglas Gregorvoid test_callable(Callable c, Callable2 c2, const Callable2& c2c, 171593564ba94ff854b7a410a4ca17ad34a90c5b761Douglas Gregor DerivesCallable dc) { 172f9eb905197e44ed5634205512074993f6f40470dDouglas Gregor int &ir = c(1); 173f9eb905197e44ed5634205512074993f6f40470dDouglas Gregor float &fr = c(1, 3.14159, 17, 42); 174f9eb905197e44ed5634205512074993f6f40470dDouglas Gregor 175f9eb905197e44ed5634205512074993f6f40470dDouglas Gregor c(); // expected-error{{no matching function for call to object of type 'struct Callable'; candidates are:}} 176518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor 177518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor double &dr = c(1.0f); 178518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor 179518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor int &ir2 = c2(); 180518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor int &ir3 = c2(1); 181518fda1d121dcba3ad7276f5e9a94f733f6e5ecdDouglas Gregor double &fr2 = c2c(); 182593564ba94ff854b7a410a4ca17ad34a90c5b761Douglas Gregor 183593564ba94ff854b7a410a4ca17ad34a90c5b761Douglas Gregor int &ir4 = dc(17); 184593564ba94ff854b7a410a4ca17ad34a90c5b761Douglas Gregor double &fr3 = dc(3.14159f); 185f9eb905197e44ed5634205512074993f6f40470dDouglas Gregor} 186106c6eb3f1380bfe243a62d8f0f8bce6a7c8d5c6Douglas Gregor 187621b3933e570792810386b891264c78e3bd7b169Douglas Gregortypedef float FLOAT; 188621b3933e570792810386b891264c78e3bd7b169Douglas Gregortypedef int& INTREF; 189621b3933e570792810386b891264c78e3bd7b169Douglas Gregortypedef INTREF Func1(FLOAT, double); 190106c6eb3f1380bfe243a62d8f0f8bce6a7c8d5c6Douglas Gregortypedef float& Func2(int, double); 191106c6eb3f1380bfe243a62d8f0f8bce6a7c8d5c6Douglas Gregor 192106c6eb3f1380bfe243a62d8f0f8bce6a7c8d5c6Douglas Gregorstruct ConvertToFunc { 1939007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregor operator Func1*(); // expected-note 2{{conversion candidate of type 'INTREF (*)(FLOAT, double)'}} 1949007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregor operator Func2&(); // expected-note 2{{conversion candidate of type 'float &(&)(int, double)'}} 1959ebae31a97f862771dc5f6bd4923dd97131fed71Douglas Gregor void operator()(); 196106c6eb3f1380bfe243a62d8f0f8bce6a7c8d5c6Douglas Gregor}; 197106c6eb3f1380bfe243a62d8f0f8bce6a7c8d5c6Douglas Gregor 1989007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregorstruct ConvertToFuncDerived : ConvertToFunc { }; 1999007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregor 2009007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregorvoid test_funcptr_call(ConvertToFunc ctf, ConvertToFuncDerived ctfd) { 201106c6eb3f1380bfe243a62d8f0f8bce6a7c8d5c6Douglas Gregor int &i1 = ctf(1.0f, 2.0); 2029007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregor float &f1 = ctf((short int)1, 1.0f); 203106c6eb3f1380bfe243a62d8f0f8bce6a7c8d5c6Douglas Gregor ctf((long int)17, 2.0); // expected-error{{error: call to object of type 'struct ConvertToFunc' is ambiguous; candidates are:}} 2049ebae31a97f862771dc5f6bd4923dd97131fed71Douglas Gregor ctf(); 2059007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregor 2069007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregor int &i2 = ctfd(1.0f, 2.0); 2079007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregor float &f2 = ctfd((short int)1, 1.0f); 2089007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregor ctfd((long int)17, 2.0); // expected-error{{error: call to object of type 'struct ConvertToFuncDerived' is ambiguous; candidates are:}} 2099007328043c9c17029e07c99d89d0488fb3da3a7Douglas Gregor ctfd(); 210106c6eb3f1380bfe243a62d8f0f8bce6a7c8d5c6Douglas Gregor} 2118ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor 2128ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregorstruct HasMember { 2138ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor int m; 2148ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor}; 2158ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor 2168ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregorstruct Arrow1 { 2178ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor HasMember* operator->(); 2188ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor}; 2198ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor 2208ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregorstruct Arrow2 { 2218ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor Arrow1 operator->(); // expected-note{{candidate function}} 2228ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor}; 2238ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor 2248ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregorvoid test_arrow(Arrow1 a1, Arrow2 a2, const Arrow2 a3) { 2258ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor int &i1 = a1->m; 2268ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor int &i2 = a2->m; 227e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl a3->m; // expected-error{{no viable overloaded 'operator->'; candidate is}} 2288ba10745f525467e91bbaec21044bf4d9017a988Douglas Gregor} 229e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregor 230e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregorstruct CopyConBase { 231e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregor}; 232e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregor 233e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregorstruct CopyCon : public CopyConBase { 234e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregor CopyCon(const CopyConBase &Base); 235e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregor 236e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregor CopyCon(const CopyConBase *Base) { 237e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregor *this = *Base; 238e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregor } 239e63ef48b76b84e7ffb0a38fbf475ddea5c48adb4Douglas Gregor}; 240fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 241fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregornamespace N { 242fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor struct X { }; 243fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor} 244fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 245fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregornamespace M { 246fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor N::X operator+(N::X, N::X); 247fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor} 248fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor 249fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregornamespace M { 250fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor void test_X(N::X x) { 251f680a0fe2dcab32b59fe6fdf71145b5313c40950Douglas Gregor (void)(x + x); 252fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor } 253fa047648b2a5502d7eef117adb4777eb9a63baa6Douglas Gregor} 2548a5ae2401645788144c0ae769a2fb899866801f5Douglas Gregor 2558a5ae2401645788144c0ae769a2fb899866801f5Douglas Gregorstruct AA { bool operator!=(AA&); }; 2568a5ae2401645788144c0ae769a2fb899866801f5Douglas Gregorstruct BB : AA {}; 2578a5ae2401645788144c0ae769a2fb899866801f5Douglas Gregorbool x(BB y, BB z) { return y != z; } 2584a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian 2594a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian 2604a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanianstruct AX { 2617a8233a69103a6eeb6602bd4a17fb5d9b7bacaadFariborz Jahanian AX& operator ->(); // expected-note {{declared at}} 2624a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian int b; 2634a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian}; 2644a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian 2654a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanianvoid m() { 2664a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian AX a; 2674a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian a->b = 0; // expected-error {{circular pointer delegation detected}} 2684a4e345a8bf749b5462a127ecfb1f90d4a9d6ba7Fariborz Jahanian} 269c4e8321deb2bd83f734a09749460050f40ec51d1John McCall 270c4e8321deb2bd83f734a09749460050f40ec51d1John McCallstruct CircA { 2717a8233a69103a6eeb6602bd4a17fb5d9b7bacaadFariborz Jahanian struct CircB& operator->(); // expected-note {{declared at}} 272c4e8321deb2bd83f734a09749460050f40ec51d1John McCall int val; 273c4e8321deb2bd83f734a09749460050f40ec51d1John McCall}; 274c4e8321deb2bd83f734a09749460050f40ec51d1John McCallstruct CircB { 2757a8233a69103a6eeb6602bd4a17fb5d9b7bacaadFariborz Jahanian struct CircC& operator->(); // expected-note {{declared at}} 276c4e8321deb2bd83f734a09749460050f40ec51d1John McCall}; 277c4e8321deb2bd83f734a09749460050f40ec51d1John McCallstruct CircC { 2787a8233a69103a6eeb6602bd4a17fb5d9b7bacaadFariborz Jahanian struct CircA& operator->(); // expected-note {{declared at}} 279c4e8321deb2bd83f734a09749460050f40ec51d1John McCall}; 280c4e8321deb2bd83f734a09749460050f40ec51d1John McCall 281c4e8321deb2bd83f734a09749460050f40ec51d1John McCallvoid circ() { 282c4e8321deb2bd83f734a09749460050f40ec51d1John McCall CircA a; 283c4e8321deb2bd83f734a09749460050f40ec51d1John McCall a->val = 0; // expected-error {{circular pointer delegation detected}} 284c4e8321deb2bd83f734a09749460050f40ec51d1John McCall} 285a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redl 286a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redl// PR5360: Arrays should lead to built-in candidates for subscript. 287a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redltypedef enum { 288a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redl LastReg = 23, 289a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redl} Register; 290a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redlclass RegAlloc { 291a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redl int getPriority(Register r) { 292a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redl return usepri[r]; 293a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redl } 294a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redl int usepri[LastReg + 1]; 295a65b55142bd8e8c94f371bb056d8f58f11de220fSebastian Redl}; 296a9efadada0430493a4d68b34b2e76580d0fd8f10Sebastian Redl 297a9efadada0430493a4d68b34b2e76580d0fd8f10Sebastian Redl// PR5546: Don't generate incorrect and ambiguous overloads for multi-level 298a9efadada0430493a4d68b34b2e76580d0fd8f10Sebastian Redl// arrays. 299a9efadada0430493a4d68b34b2e76580d0fd8f10Sebastian Redlnamespace pr5546 300a9efadada0430493a4d68b34b2e76580d0fd8f10Sebastian Redl{ 301a9efadada0430493a4d68b34b2e76580d0fd8f10Sebastian Redl enum { X }; 302a9efadada0430493a4d68b34b2e76580d0fd8f10Sebastian Redl extern const char *const sMoveCommands[][2][2]; 303a9efadada0430493a4d68b34b2e76580d0fd8f10Sebastian Redl const char* a() { return sMoveCommands[X][0][0]; } 304a9efadada0430493a4d68b34b2e76580d0fd8f10Sebastian Redl const char* b() { return (*(sMoveCommands+X))[0][0]; } 305a9efadada0430493a4d68b34b2e76580d0fd8f10Sebastian Redl} 306275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl 307275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl// PR5512 and its discussion 308275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redlnamespace pr5512 { 309275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl struct Y { 310275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl operator short(); 311275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl operator float(); 312275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl }; 313275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl void g_test(Y y) { 314275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl short s = 0; 315275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl // DR507, this should be ambiguous, but we special-case assignment 316275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl s = y; 317275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl // Note: DR507, this is ambiguous as specified 318275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl //s += y; 319275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl } 320275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl 321275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl struct S {}; 322275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl void operator +=(int&, S); 323275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl void f(S s) { 324275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl int i = 0; 325275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl i += s; 326275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl } 327275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl 328275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl struct A {operator int();}; 329275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl int a; 330275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl void b(A x) { 331275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl a += x; 332275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl } 333275c2b47a1dc200d6b55dd867bdfeb7a91b8d876Sebastian Redl} 3341eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall 3351eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall// PR5900 3361eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCallnamespace pr5900 { 3371eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall struct NotAnArray {}; 3381eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall void test0() { 3391eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall NotAnArray x; 3401eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall x[0] = 0; // expected-error {{does not provide a subscript operator}} 3411eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall } 3421eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall 3431eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall struct NonConstArray { 3441eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall int operator[](unsigned); // expected-note {{candidate}} 3451eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall }; 3461eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall int test1() { 3471eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall const NonConstArray x; 3481eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall return x[0]; // expected-error {{no viable overloaded operator[] for type}} 3491eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall } 3501eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall 3511eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall // Not really part of this PR, but implemented at the same time. 3521eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall struct NotAFunction {}; 3531eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall void test2() { 3541eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall NotAFunction x; 3551eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall x(); // expected-error {{does not provide a call operator}} 3561eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall } 3571eb3e1003d5cda4d47f54321d81d678c26981e7aJohn McCall} 358