Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 2 | int f(double); // expected-note{{candidate function}} |
| 3 | int f(int); // expected-note{{candidate function}} |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4 | |
| 5 | int (*pfd)(double) = f; // selects f(double) |
| 6 | int (*pfd2)(double) = &f; // selects f(double) |
| 7 | int (*pfd3)(double) = ((&((f)))); // selects f(double) |
| 8 | int (*pfi)(int) = &f; // selects f(int) |
| 9 | // FIXME: This error message is not very good. We need to keep better |
| 10 | // track of what went wrong when the implicit conversion failed to |
| 11 | // give a better error message here. |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 12 | int (*pfe)(...) = &f; // expected-error{{address of overloaded function 'f' does not match required type 'int (...)'}} |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 13 | int (&rfi)(int) = f; // selects f(int) |
| 14 | int (&rfd)(double) = f; // selects f(double) |
| 15 | |
Richard Trieu | 553b2b2 | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 16 | void g(int (*fp)(int)); // expected-note{{candidate function}} |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 17 | void g(int (*fp)(float)); |
Richard Trieu | 553b2b2 | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 18 | void g(int (*fp)(double)); // expected-note{{candidate function}} |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 19 | |
| 20 | int g1(int); |
| 21 | int g1(char); |
| 22 | |
| 23 | int g2(int); |
| 24 | int g2(double); |
| 25 | |
Douglas Gregor | 48bc374 | 2009-09-14 22:02:01 +0000 | [diff] [blame] | 26 | template<typename T> T g3(T); |
| 27 | int g3(int); |
| 28 | int g3(char); |
| 29 | |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 30 | void g_test() { |
| 31 | g(g1); |
Richard Trieu | 553b2b2 | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 32 | g(g2); // expected-error{{call to 'g' is ambiguous}} |
Douglas Gregor | 48bc374 | 2009-09-14 22:02:01 +0000 | [diff] [blame] | 33 | g(g3); |
| 34 | } |
| 35 | |
| 36 | template<typename T> T h1(T); |
| 37 | template<typename R, typename A1> R h1(A1); |
Douglas Gregor | 2e0807c | 2009-09-14 22:31:20 +0000 | [diff] [blame] | 38 | int h1(char); |
Douglas Gregor | 48bc374 | 2009-09-14 22:02:01 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | 2e0807c | 2009-09-14 22:31:20 +0000 | [diff] [blame] | 40 | void ha(int (*fp)(int)); |
| 41 | void hb(int (*fp)(double)); |
Douglas Gregor | 48bc374 | 2009-09-14 22:02:01 +0000 | [diff] [blame] | 42 | |
| 43 | void h_test() { |
Douglas Gregor | 2e0807c | 2009-09-14 22:31:20 +0000 | [diff] [blame] | 44 | ha(h1); |
| 45 | hb(h1); |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 46 | } |
Anders Carlsson | 6c966c4 | 2009-10-07 22:26:29 +0000 | [diff] [blame] | 47 | |
| 48 | struct A { }; |
| 49 | void f(void (*)(A *)); |
| 50 | |
| 51 | struct B |
| 52 | { |
| 53 | void g() { f(d); } |
| 54 | void d(void *); |
| 55 | static void d(A *); |
| 56 | }; |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 57 | |
| 58 | struct C { |
| 59 | C &getC() { |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 60 | return makeAC; // expected-error{{reference to non-static member function must be called; did you mean to call it with no arguments?}} |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 61 | } |
| 62 | |
David Blaikie | 6df859d8 | 2013-06-04 00:28:46 +0000 | [diff] [blame] | 63 | // FIXME: filter by const so we can unambiguously suggest '()' & point to just the one candidate, probably |
| 64 | C &makeAC(); // expected-note{{possible target for call}} |
| 65 | const C &makeAC() const; // expected-note{{possible target for call}} |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 66 | |
| 67 | static void f(); // expected-note{{candidate function}} |
| 68 | static void f(int); // expected-note{{candidate function}} |
| 69 | |
| 70 | void g() { |
| 71 | int (&fp)() = f; // expected-error{{address of overloaded function 'f' does not match required type 'int ()'}} |
| 72 | } |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 73 | |
| 74 | template<typename T> |
| 75 | void q1(int); // expected-note{{possible target for call}} |
| 76 | template<typename T> |
| 77 | void q2(T t = T()); // expected-note{{possible target for call}} |
| 78 | template<typename T> |
| 79 | void q3(); // expected-note{{possible target for call}} |
| 80 | template<typename T1, typename T2> |
| 81 | void q4(); // expected-note{{possible target for call}} |
| 82 | template<typename T1 = int> // expected-warning{{default template arguments for a function template are a C++11 extension}} |
| 83 | void q5(); // expected-note{{possible target for call}} |
| 84 | |
| 85 | void h() { |
| 86 | // Do not suggest '()' since an int argument is required |
Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 87 | q1<int>; // expected-error-re{{reference to non-static member function must be called{{$}}}} |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 88 | // Suggest '()' since there's a default value for the only argument & the |
| 89 | // type argument is already provided |
| 90 | q2<int>; // expected-error{{reference to non-static member function must be called; did you mean to call it with no arguments?}} |
| 91 | // Suggest '()' since no arguments are required & the type argument is |
| 92 | // already provided |
| 93 | q3<int>; // expected-error{{reference to non-static member function must be called; did you mean to call it with no arguments?}} |
| 94 | // Do not suggest '()' since another type argument is required |
Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 95 | q4<int>; // expected-error-re{{reference to non-static member function must be called{{$}}}} |
David Blaikie | e5323aa | 2013-06-21 23:54:45 +0000 | [diff] [blame] | 96 | // Suggest '()' since the type parameter has a default value |
| 97 | q5; // expected-error{{reference to non-static member function must be called; did you mean to call it with no arguments?}} |
| 98 | } |
Douglas Gregor | 064fdb2 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 99 | }; |
John McCall | 8c12dc4 | 2010-04-22 18:44:12 +0000 | [diff] [blame] | 100 | |
| 101 | // PR6886 |
| 102 | namespace test0 { |
| 103 | void myFunction(void (*)(void *)); |
| 104 | |
| 105 | class Foo { |
| 106 | void foo(); |
| 107 | |
| 108 | static void bar(void*); |
| 109 | static void bar(); |
| 110 | }; |
| 111 | |
| 112 | void Foo::foo() { |
| 113 | myFunction(bar); |
| 114 | } |
| 115 | } |
Eli Friedman | 7fd5544 | 2010-08-24 05:23:20 +0000 | [diff] [blame] | 116 | |
| 117 | namespace PR7971 { |
| 118 | struct S { |
| 119 | void g() { |
| 120 | f(&g); |
| 121 | } |
| 122 | void f(bool (*)(int, char)); |
| 123 | static bool g(int, char); |
| 124 | }; |
| 125 | } |
Douglas Gregor | bdd7b23 | 2010-09-12 08:16:09 +0000 | [diff] [blame] | 126 | |
| 127 | namespace PR8033 { |
Richard Trieu | 553b2b2 | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 128 | template <typename T1, typename T2> int f(T1 *, const T2 *); // expected-note {{candidate function [with T1 = const int, T2 = int]}} \ |
| 129 | // expected-note{{candidate function}} |
| 130 | template <typename T1, typename T2> int f(const T1 *, T2 *); // expected-note {{candidate function [with T1 = int, T2 = const int]}} \ |
| 131 | // expected-note{{candidate function}} |
Douglas Gregor | bdd7b23 | 2010-09-12 08:16:09 +0000 | [diff] [blame] | 132 | int (*p)(const int *, const int *) = f; // expected-error{{address of overloaded function 'f' is ambiguous}} \ |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 133 | // expected-error{{address of overloaded function 'f' is ambiguous}} |
Douglas Gregor | bdd7b23 | 2010-09-12 08:16:09 +0000 | [diff] [blame] | 134 | |
| 135 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 136 | |
| 137 | namespace PR8196 { |
| 138 | template <typename T> struct mcdata { |
| 139 | typedef int result_type; |
| 140 | }; |
| 141 | template <class T> |
| 142 | typename mcdata<T>::result_type wrap_mean(mcdata<T> const&); |
| 143 | void add_property(double(*)(mcdata<double> const &)); // expected-note{{candidate function not viable: no overload of 'wrap_mean' matching}} |
| 144 | void f() { |
| 145 | add_property(&wrap_mean); // expected-error{{no matching function for call to 'add_property'}} |
| 146 | } |
| 147 | } |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 148 | |
| 149 | namespace PR7425 { |
| 150 | template<typename T> |
| 151 | void foo() |
| 152 | { |
| 153 | } |
| 154 | |
| 155 | struct B |
| 156 | { |
| 157 | template<typename T> |
| 158 | B(const T&) |
| 159 | { |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | void bar(const B& b) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | void bar2(const B& b = foo<int>) |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | void test(int argc, char** argv) |
| 172 | { |
| 173 | bar(foo<int>); |
| 174 | bar2(); |
| 175 | } |
| 176 | } |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 177 | |
| 178 | namespace test1 { |
| 179 | void fun(int x) {} |
| 180 | |
| 181 | void parameter_number() { |
| 182 | void (*ptr1)(int, int) = &fun; // expected-error {{cannot initialize a variable of type 'void (*)(int, int)' with an rvalue of type 'void (*)(int)': different number of parameters (2 vs 1)}} |
| 183 | void (*ptr2)(int, int); |
| 184 | ptr2 = &fun; // expected-error {{assigning to 'void (*)(int, int)' from incompatible type 'void (*)(int)': different number of parameters (2 vs 1)}} |
| 185 | } |
| 186 | |
| 187 | void parameter_mismatch() { |
Richard Trieu | 96ed5b6 | 2011-12-13 23:19:45 +0000 | [diff] [blame] | 188 | void (*ptr1)(double) = &fun; // expected-error {{cannot initialize a variable of type 'void (*)(double)' with an rvalue of type 'void (*)(int)': type mismatch at 1st parameter ('double' vs 'int')}} |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 189 | void (*ptr2)(double); |
| 190 | ptr2 = &fun; // expected-error {{assigning to 'void (*)(double)' from incompatible type 'void (*)(int)': type mismatch at 1st parameter ('double' vs 'int')}} |
| 191 | } |
| 192 | |
| 193 | void return_type_test() { |
| 194 | int (*ptr1)(int) = &fun; // expected-error {{cannot initialize a variable of type 'int (*)(int)' with an rvalue of type 'void (*)(int)': different return type ('int' vs 'void')}} |
| 195 | int (*ptr2)(int); |
| 196 | ptr2 = &fun; // expected-error {{assigning to 'int (*)(int)' from incompatible type 'void (*)(int)': different return type ('int' vs 'void')}} |
| 197 | } |
| 198 | |
| 199 | int foo(double x, double y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}} |
| 200 | int foo(int x, int y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}} |
| 201 | int foo(double x) {return 0;} // expected-note {{candidate function has type mismatch at 1st parameter (expected 'int' but has 'double')}} |
| 202 | double foo(float x, float y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}} |
| 203 | double foo(int x, float y) {return 0;} // expected-note {{candidate function has different number of parameters (expected 1 but has 2)}} |
| 204 | double foo(float x) {return 0;} // expected-note {{candidate function has type mismatch at 1st parameter (expected 'int' but has 'float')}} |
| 205 | double foo(int x) {return 0;} // expected-note {{candidate function has different return type ('int' expected but has 'double')}} |
| 206 | |
| 207 | int (*ptr)(int) = &foo; // expected-error {{address of overloaded function 'foo' does not match required type 'int (int)'}} |
| 208 | |
| 209 | struct Qualifiers { |
| 210 | void N() {}; |
| 211 | void C() const {}; |
| 212 | void V() volatile {}; |
| 213 | void R() __restrict {}; |
| 214 | void CV() const volatile {}; |
| 215 | void CR() const __restrict {}; |
| 216 | void VR() volatile __restrict {}; |
| 217 | void CVR() const volatile __restrict {}; |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | void QualifierTest() { |
| 222 | void (Qualifiers::*X)(); |
Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 223 | X = &Qualifiers::C; // expected-error-re {{assigning to 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}}' from incompatible type 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}} const': different qualifiers (none vs const)}} |
| 224 | X = &Qualifiers::V; // expected-error-re{{assigning to 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}}' from incompatible type 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}} volatile': different qualifiers (none vs volatile)}} |
Jacques Pienaar | b92e7a9 | 2015-03-03 23:58:09 +0000 | [diff] [blame^] | 225 | X = &Qualifiers::R; // expected-error-re{{assigning to 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}}' from incompatible type 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}} __restrict': different qualifiers (none vs restrict)}} |
Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 226 | X = &Qualifiers::CV; // expected-error-re{{assigning to 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}}' from incompatible type 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}} const volatile': different qualifiers (none vs const and volatile)}} |
Jacques Pienaar | b92e7a9 | 2015-03-03 23:58:09 +0000 | [diff] [blame^] | 227 | X = &Qualifiers::CR; // expected-error-re{{assigning to 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}}' from incompatible type 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}} const __restrict': different qualifiers (none vs const and restrict)}} |
| 228 | X = &Qualifiers::VR; // expected-error-re{{assigning to 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}}' from incompatible type 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}} volatile __restrict': different qualifiers (none vs volatile and restrict)}} |
| 229 | X = &Qualifiers::CVR; // expected-error-re{{assigning to 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}}' from incompatible type 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}} const volatile __restrict': different qualifiers (none vs const, volatile, and restrict)}} |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | struct Dummy { |
| 233 | void N() {}; |
| 234 | }; |
| 235 | |
Alp Toker | 6ed7251 | 2013-12-14 01:07:05 +0000 | [diff] [blame] | 236 | void (Qualifiers::*X)() = &Dummy::N; // expected-error-re{{cannot initialize a variable of type 'void (test1::Qualifiers::*)(){{( __attribute__\(\(thiscall\)\))?}}' with an rvalue of type 'void (test1::Dummy::*)(){{( __attribute__\(\(thiscall\)\))?}}': different classes ('test1::Qualifiers' vs 'test1::Dummy')}} |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 237 | } |
Eli Friedman | 544c956 | 2013-07-08 23:35:04 +0000 | [diff] [blame] | 238 | |
| 239 | template <typename T> class PR16561 { |
| 240 | virtual bool f() { if (f) {} return false; } // expected-error {{reference to non-static member function must be called}} |
| 241 | }; |