Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -pedantic -verify %s
      2 int* f(int) { return 0; }
      3 float* f(float) { return 0; }
      4 void f();
      5 
      6 void test_f(int iv, float fv) {
      7   float* fp = f(fv);
      8   int* ip = f(iv);
      9 }
     10 
     11 int* g(int, float, int); // expected-note {{candidate function}}
     12 float* g(int, int, int); // expected-note {{candidate function}}
     13 double* g(int, float, float); // expected-note {{candidate function}}
     14 char* g(int, float, ...); // expected-note {{candidate function}}
     15 void g();
     16 
     17 void test_g(int iv, float fv) {
     18   int* ip1 = g(iv, fv, 0);
     19   float* fp1 = g(iv, iv, 0);
     20   double* dp1 = g(iv, fv, fv);
     21   char* cp1 = g(0, 0);
     22   char* cp2 = g(0, 0, 0, iv, fv);
     23 
     24   double* dp2 = g(0, fv, 1.5); // expected-error {{call to 'g' is ambiguous}}
     25 }
     26 
     27 double* h(double f);
     28 int* h(int);
     29 
     30 void test_h(float fv, unsigned char cv) {
     31   double* dp = h(fv);
     32   int* ip = h(cv);
     33 }
     34 
     35 int* i(int);
     36 double* i(long);
     37 
     38 void test_i(short sv, int iv, long lv, unsigned char ucv) {
     39   int* ip1 = i(sv);
     40   int* ip2 = i(iv);
     41   int* ip3 = i(ucv);
     42   double* dp1 = i(lv);
     43 }
     44 
     45 int* j(void*);
     46 double* j(bool);
     47 
     48 void test_j(int* ip) {
     49   int* ip1 = j(ip);
     50 }
     51 
     52 int* k(char*);
     53 double* k(bool);
     54 
     55 void test_k() {
     56   int* ip1 = k("foo"); // expected-warning{{conversion from string literal to 'char *' is deprecated}}
     57   int* ip2 = k(("foo")); // expected-warning{{conversion from string literal to 'char *' is deprecated}}
     58   double* dp1 = k(L"foo");
     59 }
     60 
     61 int* l(wchar_t*);
     62 double* l(bool);
     63 
     64 void test_l() {
     65   int* ip1 = l(L"foo"); // expected-warning{{conversion from string literal to 'wchar_t *' is deprecated}}
     66   double* dp1 = l("foo");
     67 }
     68 
     69 int* m(const char*);
     70 double* m(char*);
     71 
     72 void test_m() {
     73   int* ip = m("foo");
     74 }
     75 
     76 int* n(char*);
     77 double* n(void*);
     78 class E;
     79 
     80 void test_n(E* e) {
     81   char ca[7];
     82   int* ip1 = n(ca);
     83   int* ip2 = n("foo"); // expected-warning{{conversion from string literal to 'char *' is deprecated}}
     84 
     85   float fa[7];
     86   double* dp1 = n(fa);
     87 
     88   double* dp2 = n(e);
     89 }
     90 
     91 enum PromotesToInt {
     92   PromotesToIntValue = -1
     93 };
     94 
     95 enum PromotesToUnsignedInt {
     96   PromotesToUnsignedIntValue = __INT_MAX__ * 2U
     97 };
     98 
     99 int* o(int);
    100 double* o(unsigned int);
    101 float* o(long);
    102 
    103 void test_o() {
    104   int* ip1 = o(PromotesToIntValue);
    105   double* dp1 = o(PromotesToUnsignedIntValue);
    106 }
    107 
    108 int* p(int);
    109 double* p(double);
    110 
    111 void test_p() {
    112   int* ip = p((short)1);
    113   double* dp = p(1.0f);
    114 }
    115 
    116 struct Bits {
    117   signed short int_bitfield : 5;
    118   unsigned int uint_bitfield : 8;
    119 };
    120 
    121 int* bitfields(int, int);
    122 float* bitfields(unsigned int, int);
    123 
    124 void test_bitfield(Bits bits, int x) {
    125   int* ip = bitfields(bits.int_bitfield, 0);
    126   float* fp = bitfields(bits.uint_bitfield, 0u);
    127 }
    128 
    129 int* multiparm(long, int, long); // expected-note {{candidate function}}
    130 float* multiparm(int, int, int); // expected-note {{candidate function}}
    131 double* multiparm(int, int, short); // expected-note {{candidate function}}
    132 
    133 void test_multiparm(long lv, short sv, int iv) {
    134   int* ip1 = multiparm(lv, iv, lv);
    135   int* ip2 = multiparm(lv, sv, lv);
    136   float* fp1 = multiparm(iv, iv, iv);
    137   float* fp2 = multiparm(sv, iv, iv);
    138   double* dp1 = multiparm(sv, sv, sv);
    139   double* dp2 = multiparm(iv, sv, sv);
    140   multiparm(sv, sv, lv); // expected-error {{call to 'multiparm' is ambiguous}}
    141 }
    142 
    143 // Test overloading based on qualification vs. no qualification
    144 // conversion.
    145 int* quals1(int const * p);
    146 char* quals1(int * p);
    147 
    148 int* quals2(int const * const * pp);
    149 char* quals2(int * * pp);
    150 
    151 int* quals3(int const * * const * ppp);
    152 char* quals3(int *** ppp);
    153 
    154 void test_quals(int * p, int * * pp, int * * * ppp) {
    155   char* q1 = quals1(p);
    156   char* q2 = quals2(pp);
    157   char* q3 = quals3(ppp);
    158 }
    159 
    160 // Test overloading based on qualification ranking (C++ 13.3.2)p3.
    161 int* quals_rank1(int const * p);
    162 float* quals_rank1(int const volatile *p);
    163 char* quals_rank1(char*);
    164 double* quals_rank1(const char*);
    165 
    166 int* quals_rank2(int const * const * pp);
    167 float* quals_rank2(int * const * pp);
    168 
    169 void quals_rank3(int const * const * const volatile * p); // expected-note{{candidate function}}
    170 void quals_rank3(int const * const volatile * const * p); // expected-note{{candidate function}}
    171 
    172 void quals_rank3(int const *); // expected-note{{candidate function}}
    173 void quals_rank3(int volatile *); // expected-note{{candidate function}}
    174 
    175 void test_quals_ranking(int * p, int volatile *pq, int * * pp, int * * * ppp) {
    176   int* q1 = quals_rank1(p);
    177   float* q2 = quals_rank1(pq);
    178   double* q3 = quals_rank1("string literal");
    179   char a[17];
    180   const char* ap = a;
    181   char* q4 = quals_rank1(a);
    182   double* q5 = quals_rank1(ap);
    183 
    184   float* q6 = quals_rank2(pp);
    185 
    186   quals_rank3(ppp); // expected-error {{call to 'quals_rank3' is ambiguous}}
    187 
    188   quals_rank3(p); // expected-error {{call to 'quals_rank3' is ambiguous}}
    189   quals_rank3(pq);
    190 }
    191 
    192 // Test overloading based on derived-to-base conversions
    193 class A { };
    194 class B : public A { };
    195 class C : public B { };
    196 class D : public C { };
    197 
    198 int* derived1(A*);
    199 char* derived1(const A*);
    200 float* derived1(void*);
    201 
    202 int* derived2(A*);
    203 float* derived2(B*);
    204 
    205 int* derived3(A*);
    206 float* derived3(const B*);
    207 char* derived3(C*);
    208 
    209 void test_derived(B* b, B const* bc, C* c, const C* cc, void* v, D* d) {
    210   int* d1 = derived1(b);
    211   char* d2 = derived1(bc);
    212   int* d3 = derived1(c);
    213   char* d4 = derived1(cc);
    214   float* d5 = derived1(v);
    215 
    216   float* d6 = derived2(b);
    217   float* d7 = derived2(c);
    218 
    219   char* d8 = derived3(d);
    220 }
    221 
    222 void derived4(C*); // expected-note{{candidate function not viable: cannot convert from base class pointer 'A *' to derived class pointer 'C *' for 1st argument}}
    223 
    224 void test_base(A* a) {
    225   derived4(a); // expected-error{{no matching function for call to 'derived4}}
    226 }
    227 
    228 // Test overloading of references.
    229 // (FIXME: tests binding to determine candidate sets, not overload
    230 //  resolution per se).
    231 int* intref(int&);
    232 float* intref(const int&);
    233 
    234 void intref_test() {
    235   float* ir1 = intref(5);
    236   float* ir2 = intref(5.5); // expected-warning{{implicit conversion from 'double' to 'int' changes value from 5.5 to 5}}
    237 }
    238 
    239 void derived5(C&); // expected-note{{candidate function not viable: cannot bind base class object of type 'A' to derived class reference 'C &' for 1st argument}}
    240 
    241 void test_base(A& a) {
    242   derived5(a); // expected-error{{no matching function for call to 'derived5}}
    243 }
    244 
    245 // Test reference binding vs. standard conversions.
    246 int& bind_vs_conv(const double&);
    247 float& bind_vs_conv(int);
    248 
    249 void bind_vs_conv_test()
    250 {
    251   int& i1 = bind_vs_conv(1.0f);
    252   float& f1 = bind_vs_conv((short)1);
    253 }
    254 
    255 // Test that cv-qualifiers get subsumed in the reference binding.
    256 struct X { };
    257 struct Y { };
    258 struct Z : X, Y { };
    259 
    260 int& cvqual_subsume(X&); // expected-note{{candidate function}}
    261 float& cvqual_subsume(const Y&); // expected-note{{candidate function}}
    262 
    263 int& cvqual_subsume2(X&); // expected-note{{candidate function}}
    264 float& cvqual_subsume2(volatile Y&); // expected-note{{candidate function}}
    265 
    266 void cvqual_subsume_test(Z z) {
    267   cvqual_subsume(z); // expected-error{{call to 'cvqual_subsume' is ambiguous}}
    268   cvqual_subsume2(z); // expected-error{{call to 'cvqual_subsume2' is ambiguous}}
    269 }
    270 
    271 // Test overloading with cv-qualification differences in reference
    272 // binding.
    273 int& cvqual_diff(X&);
    274 float& cvqual_diff(const X&);
    275 
    276 void cvqual_diff_test(X x, Z z) {
    277   int& i1 = cvqual_diff(x);
    278   int& i2 = cvqual_diff(z);
    279 }
    280 
    281 // Test overloading with derived-to-base differences in reference
    282 // binding.
    283 struct Z2 : Z { };
    284 
    285 int& db_rebind(X&);
    286 long& db_rebind(Y&);
    287 float& db_rebind(Z&);
    288 
    289 void db_rebind_test(Z2 z2) {
    290   float& f1 = db_rebind(z2);
    291 }
    292 
    293 class string { };
    294 class opt : public string { };
    295 
    296 struct SR {
    297   SR(const string&);
    298 };
    299 
    300 void f(SR) { }
    301 
    302 void g(opt o) {
    303   f(o);
    304 }
    305 
    306 
    307 namespace PR5756 {
    308   int &a(void*, int);
    309   float &a(void*, float);
    310   void b() {
    311     int &ir = a(0,0);
    312     (void)ir;
    313   }
    314 }
    315 
    316 // Tests the exact text used to note the candidates
    317 namespace test1 {
    318   template <class T> void foo(T t, unsigned N); // expected-note {{candidate function [with T = int] not viable: no known conversion from 'const char [6]' to 'unsigned int' for 2nd argument}}
    319   void foo(int n, char N); // expected-note {{candidate function not viable: no known conversion from 'const char [6]' to 'char' for 2nd argument}}
    320   void foo(int n, const char *s, int t); // expected-note {{candidate function not viable: requires 3 arguments, but 2 were provided}}
    321   void foo(int n, const char *s, int t, ...); // expected-note {{candidate function not viable: requires at least 3 arguments, but 2 were provided}}
    322   void foo(int n, const char *s, int t, int u = 0); // expected-note {{candidate function not viable: requires at least 3 arguments, but 2 were provided}}
    323 
    324   // PR 11857
    325   void foo(int n); // expected-note {{candidate function not viable: requires single argument 'n', but 2 arguments were provided}}
    326   void foo(unsigned n = 10); // expected-note {{candidate function not viable: allows at most single argument 'n', but 2 arguments were provided}}
    327   void bar(int n, int u = 0); // expected-note {{candidate function not viable: requires at least argument 'n', but no arguments were provided}}
    328   void baz(int n = 0, int u = 0); // expected-note {{candidate function not viable: requires at most 2 arguments, but 3 were provided}}
    329 
    330   void test() {
    331     foo(4, "hello"); //expected-error {{no matching function for call to 'foo'}}
    332     bar(); //expected-error {{no matching function for call to 'bar'}}
    333     baz(3, 4, 5); // expected-error {{no matching function for call to 'baz'}}
    334   }
    335 }
    336 
    337 // PR 6014
    338 namespace test2 {
    339   struct QFixed {
    340     QFixed(int i);
    341     QFixed(long i);
    342   };
    343 
    344   bool operator==(const QFixed &f, int i);
    345 
    346   class qrgb666 {
    347     inline operator unsigned int () const;
    348 
    349     inline bool operator==(const qrgb666 &v) const;
    350     inline bool operator!=(const qrgb666 &v) const { return !(*this == v); }
    351   };
    352 }
    353 
    354 // PR 6117
    355 namespace test3 {
    356   struct Base {};
    357   struct Incomplete;
    358 
    359   void foo(Base *); // expected-note 2 {{cannot convert argument of incomplete type}}
    360   void foo(Base &); // expected-note 2 {{cannot convert argument of incomplete type}}
    361 
    362   void test(Incomplete *P) {
    363     foo(P); // expected-error {{no matching function for call to 'foo'}}
    364     foo(*P); // expected-error {{no matching function for call to 'foo'}}
    365   }
    366 }
    367 
    368 namespace DerivedToBaseVsVoid {
    369   struct A { };
    370   struct B : A { };
    371 
    372   float &f(void *);
    373   int &f(const A*);
    374 
    375   void g(B *b) {
    376     int &ir = f(b);
    377   }
    378 }
    379 
    380 // PR 6398 + PR 6421
    381 namespace test4 {
    382   class A;
    383   class B {
    384     static void foo(); // expected-note {{not viable}}
    385     static void foo(int*); // expected-note {{not viable}}
    386     static void foo(long*); // expected-note {{not viable}}
    387 
    388     void bar(A *a) {
    389       foo(a); // expected-error {{no matching function for call}}
    390     }
    391   };
    392 }
    393 
    394 namespace DerivedToBase {
    395   struct A { };
    396   struct B : A { };
    397   struct C : B { };
    398 
    399   int &f0(const A&);
    400   float &f0(B);
    401 
    402   void g() {
    403     float &fr = f0(C());
    404   }
    405 }
    406 
    407 namespace PR6483 {
    408   struct X0 {
    409     operator const unsigned int & () const;
    410   };
    411 
    412   struct X1 {
    413     operator unsigned int & () const;
    414   };
    415 
    416   void f0(const bool &);
    417   void f1(bool &); // expected-note 2{{not viable}}
    418 
    419   void g(X0 x0, X1 x1) {
    420     f0(x0);
    421     f1(x0); // expected-error{{no matching function for call}}
    422     f0(x1);
    423     f1(x1); // expected-error{{no matching function for call}}
    424   }
    425 }
    426 
    427 namespace PR6078 {
    428   struct A {
    429     A(short); // expected-note{{candidate constructor}}
    430     A(long); // expected-note{{candidate constructor}}
    431   };
    432   struct S {
    433     typedef void ft(A);
    434     operator ft*();
    435   };
    436 
    437   void f() {
    438     S()(0); // expected-error{{conversion from 'int' to 'PR6078::A' is ambiguous}}
    439   }
    440 }
    441 
    442 namespace PR6177 {
    443   struct String { String(char const*); };
    444 
    445   void f(bool const volatile&);
    446   int &f(String);
    447 
    448   void g() { int &r = f(""); }
    449 }
    450 
    451 namespace PR7095 {
    452   struct X { };
    453 
    454   struct Y {
    455     operator const X*();
    456 
    457   private:
    458     operator X*();
    459   };
    460 
    461   void f(const X *);
    462   void g(Y y) { f(y); }
    463 }
    464 
    465 namespace PR7224 {
    466   class A {};
    467   class B : public A {};
    468 
    469   int &foo(A *const d);
    470   float &foo(const A *const d);
    471 
    472   void bar()
    473   {
    474     B *const d = 0;
    475     B const *const d2 = 0;
    476     int &ir = foo(d);
    477     float &fr = foo(d2);
    478   }
    479 }
    480 
    481 namespace NontrivialSubsequence {
    482   struct X0;
    483 
    484   class A {
    485     operator X0 *();
    486   public:
    487     operator const X0 *();
    488   };
    489 
    490   A a;
    491   void foo( void const * );
    492 
    493   void g() {
    494     foo(a);
    495   }
    496 }
    497 
    498 // rdar://rdar8499524
    499 namespace rdar8499524 {
    500   struct W {};
    501   struct S {
    502       S(...);
    503   };
    504 
    505   void g(const S&);
    506   void f() {
    507     g(W());
    508   }
    509 }
    510 
    511 namespace rdar9173984 {
    512   template <typename T, unsigned long N> int &f(const T (&)[N]);
    513   template <typename T> float &f(const T *);
    514 
    515   void test() {
    516     int arr[2] = {0, 0};
    517     int *arrp = arr;
    518     int &ir = f(arr);
    519     float &fr = f(arrp);
    520   }
    521 }
    522 
    523 namespace PR9507 {
    524   void f(int * const&); // expected-note{{candidate function}}
    525   void f(int const(&)[1]); // expected-note{{candidate function}}
    526 
    527   int main() {
    528     int n[1];
    529     f(n); // expected-error{{call to 'f' is ambiguous}}
    530   }
    531 }
    532 
    533 namespace rdar9803316 {
    534   void foo(float);
    535   int &foo(int);
    536 
    537   void bar() {
    538     int &ir = (&foo)(0);
    539   }
    540 }
    541 
    542 namespace IncompleteArg {
    543   // Ensure that overload resolution attempts to complete argument types when
    544   // performing ADL.
    545   template<typename T> struct S {
    546     friend int f(const S&);
    547   };
    548   extern S<int> s;
    549   int k = f(s);
    550 
    551   template<typename T> struct Op {
    552     friend bool operator==(const Op &, const Op &);
    553   };
    554   extern Op<char> op;
    555   bool b = op == op;
    556 
    557   // ... and not in other cases! Nothing here requires U<int()> to be complete.
    558   // (Note that instantiating U<int()> will fail.)
    559   template<typename T> struct U {
    560     T t;
    561   };
    562   struct Consumer {
    563     template<typename T>
    564     int operator()(const U<T> &);
    565   };
    566   template<typename T> U<T> &make();
    567   Consumer c;
    568   int n = sizeof(c(make<int()>()));
    569 }
    570 
    571 namespace PR12142 {
    572   void fun(int (*x)[10]); // expected-note{{candidate function not viable: 1st argument ('const int (*)[10]') would lose const qualifier}}
    573   void g() { fun((const int(*)[10])0); } // expected-error{{no matching function for call to 'fun'}}
    574 }
    575 
    576 // DR1152: Take 'volatile' into account when handling reference bindings in
    577 //         overload resolution.
    578 namespace PR12931 {
    579   void f(const int &, ...);
    580   void f(const volatile int &, int);
    581   void g() { f(0, 0); }
    582 }
    583 
    584 void test5() {
    585   struct {
    586     typedef void F1(int);
    587     typedef void F2(double);
    588     operator F1*();  // expected-note{{conversion candidate}}
    589     operator F2*();  // expected-note{{conversion candidate}}
    590   } callable;
    591   callable();  // expected-error{{no matching function for call}}
    592 }
    593