1 // RUN: %clang_cc1 -std=c++11 -verify %s
3 typedef int (*fp)(int);
5 struct Incomplete; // expected-note{{forward declaration of 'Incomplete'}} \
6 // expected-note {{forward declaration of 'Incomplete'}}
9 X() = default; // expected-note{{candidate constructor not viable: requires 0 arguments, but 1 was provided}}
10 X(const X&) = default; // expected-note{{candidate constructor not viable: no known conversion from 'bool' to 'const X' for 1st argument}}
11 X(bool b) __attribute__((enable_if(b, "chosen when 'b' is true"))); // expected-note{{candidate disabled: chosen when 'b' is true}}
13 void f(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));
14 void f(int n) __attribute__((enable_if(n == 1, "chosen when 'n' is one"))); // expected-note{{member declaration nearly matches}} expected-note 2{{candidate disabled: chosen when 'n' is one}}
16 void g(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero"))); // expected-note{{candidate disabled: chosen when 'n' is zero}}
18 void h(int n, int m = 0) __attribute__((enable_if(m == 0, "chosen when 'm' is zero"))); // expected-note{{candidate disabled: chosen when 'm' is zero}}
20 static void s(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero"))); // expected-note2{{candidate disabled: chosen when 'n' is zero}}
22 void conflict(int n) __attribute__((enable_if(n+n == 10, "chosen when 'n' is five"))); // expected-note{{candidate function}}
23 void conflict(int n) __attribute__((enable_if(n*2 == 10, "chosen when 'n' is five"))); // expected-note{{candidate function}}
25 void hidden_by_argument_conversion(Incomplete n, int m = 0) __attribute__((enable_if(m == 10, "chosen when 'm' is ten")));
26 Incomplete hidden_by_incomplete_return_value(int n = 0) __attribute__((enable_if(n == 10, "chosen when 'n' is ten"))); // expected-note{{'hidden_by_incomplete_return_value' declared here}}
28 operator long() __attribute__((enable_if(true, "chosen on your platform")));
29 operator int() __attribute__((enable_if(false, "chosen on other platform")));
31 operator fp() __attribute__((enable_if(false, "never enabled"))) { return surrogate; } // expected-note{{conversion candidate of type 'int (*)(int)'}} // FIXME: the message is not displayed
34 void X::f(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero"))) // expected-note{{member declaration nearly matches}} expected-note 2{{candidate disabled: chosen when 'n' is zero}}
38 void X::f(int n) __attribute__((enable_if(n == 2, "chosen when 'n' is two"))) // expected-error{{out-of-line definition of 'f' does not match any declaration in 'X'}}
43 X x2(false); // expected-error{{no matching constructor for initialization of 'X'}}
45 __attribute__((deprecated)) constexpr int old() { return 0; } // expected-note2{{'old' has been explicitly marked deprecated here}}
46 void deprec1(int i) __attribute__((enable_if(old() == 0, "chosen when old() is zero"))); // expected-warning{{'old' is deprecated}}
47 void deprec2(int i) __attribute__((enable_if(old() == 0, "chosen when old() is zero"))); // expected-warning{{'old' is deprecated}}
50 void overloaded(long);
53 constexpr Int(int i) : i(i) { }
54 constexpr operator int() const { return i; }
58 void default_argument(int n, int m = 0) __attribute__((enable_if(m == 0, "chosen when 'm' is zero"))); // expected-note{{candidate disabled: chosen when 'm' is zero}}
59 void default_argument_promotion(int n, int m = Int(0)) __attribute__((enable_if(m == 0, "chosen when 'm' is zero"))); // expected-note{{candidate disabled: chosen when 'm' is zero}}
62 template<typename T> void typedep(T t) __attribute__((enable_if(t, ""))); // expected-note{{candidate disabled:}} expected-error{{value of type 'Nothing' is not contextually convertible to 'bool'}}
63 template<int N> void valuedep() __attribute__((enable_if(N == 1, "")));
65 // FIXME: we skip potential constant expression evaluation on value dependent
66 // enable-if expressions
68 template<int N> void valuedep() __attribute__((enable_if(N == not_constexpr(), "")));
70 template <typename T> void instantiationdep() __attribute__((enable_if(sizeof(sizeof(T)) != 0, "")));
76 x.f(2); // expected-error{{no matching member function for call to 'f'}}
77 x.f(3); // expected-error{{no matching member function for call to 'f'}}
80 x.g(1); // expected-error{{no matching member function for call to 'g'}}
83 x.h(1, 2); // expected-error{{no matching member function for call to 'h'}}
86 x.s(1); // expected-error{{no matching member function for call to 's'}}
89 X::s(1); // expected-error{{no matching member function for call to 's'}}
91 x.conflict(5); // expected-error{{call to member function 'conflict' is ambiguous}}
93 x.hidden_by_argument_conversion(10); // expected-error{{argument type 'Incomplete' is incomplete}}
94 x.hidden_by_incomplete_return_value(10); // expected-error{{calling 'hidden_by_incomplete_return_value' with incomplete return type 'Incomplete'}}
101 default_argument(1, 2); // expected-error{{no matching function for call to 'default_argument'}}
103 default_argument_promotion(0);
104 default_argument_promotion(1, 2); // expected-error{{no matching function for call to 'default_argument_promotion'}}
106 int i = x(1); // expected-error{{no matching function for call to object of type 'X'}}
109 typedep(0); // expected-error{{no matching function for call to 'typedep'}}
111 typedep(n); // expected-note{{in instantiation of function template specialization 'typedep<Nothing>' requested here}}
114 template <typename T> class C {
115 void f() __attribute__((enable_if(T::expr == 0, ""))) {}
119 int fn3(bool b) __attribute__((enable_if(b, ""))); // FIXME: This test should net 0 error messages.
120 template <class T> void test3() {
121 fn3(sizeof(T) == 1); // expected-error{{no matching function for call to 'fn3'}} expected-note@-2{{candidate disabled}}
124 template <typename T>
126 T h(int n, int m = 0) __attribute__((enable_if(m == 0, "chosen when 'm' is zero"))); // expected-note{{candidate disabled: chosen when 'm' is zero}}
133 int t1 = y.h(1, 2); // expected-error{{no matching member function for call to 'h'}}
136 // FIXME: issue an error (without instantiation) because ::h(T()) is not
137 // convertible to bool, because return types aren't overloadable.
139 template <typename T> void outer() {
140 void local_function() __attribute__((enable_if(::h(T()), "")));
141 local_function(); // expected-error{{no matching function for call to 'local_function'}} expected-note@-1{{candidate disabled}}
149 int fn1(const Integer &) __attribute__((enable_if(true, "")));
150 template <class T> void test1() {
151 int &expr = T::expr();
155 int fn2(const Integer &) __attribute__((enable_if(false, ""))); // expected-note{{candidate disabled}}
156 template <class T> void test2() {
157 int &expr = T::expr();
158 fn2(expr); // expected-error{{no matching function for call to 'fn2'}}
161 int fn3(bool b) __attribute__((enable_if(b, ""))); // FIXME: This test should net 0 error messages.
162 template <class T> void test3() {
163 fn3(sizeof(T) == 1); // expected-error{{no matching function for call to 'fn3'}} expected-note@-2{{candidate disabled}}
168 int ovlFoo(int m) __attribute__((enable_if(m > 0, "")));
172 // Assignment gives us a different code path than declarations, and `&foo`
173 // gives us a different code path than `foo`
174 int (*p)(int) = ovlFoo;
175 int (*p2)(int) = &ovlFoo;
181 int ovlBar(int) __attribute__((enable_if(true, "")));
182 int ovlBar(int m) __attribute__((enable_if(false, "")));
184 int (*p)(int) = ovlBar;
185 int (*p2)(int) = &ovlBar;
191 int ovlConflict(int m) __attribute__((enable_if(true, "")));
192 int ovlConflict(int m) __attribute__((enable_if(1, "")));
194 int (*p)(int) = ovlConflict; // expected-error{{address of overloaded function 'ovlConflict' is ambiguous}} expected-note@191{{candidate function}} expected-note@192{{candidate function}}
195 int (*p2)(int) = &ovlConflict; // expected-error{{address of overloaded function 'ovlConflict' is ambiguous}} expected-note@191{{candidate function}} expected-note@192{{candidate function}}
197 a = ovlConflict; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@191{{candidate function}} expected-note@192{{candidate function}}
198 a = &ovlConflict; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@191{{candidate function}} expected-note@192{{candidate function}}
201 template <typename T>
202 T templated(T m) __attribute__((enable_if(true, ""))) { return T(); }
203 template <typename T>
204 T templated(T m) __attribute__((enable_if(false, ""))) { return T(); }
206 int (*p)(int) = templated<int>;
207 int (*p2)(int) = &templated<int>;
213 template <typename T>
214 T templatedBar(T m) __attribute__((enable_if(m > 0, ""))) { return T(); }
216 int (*p)(int) = templatedBar<int>; // expected-error{{address of overloaded function 'templatedBar' does not match required type 'int (int)'}} expected-note@214{{candidate function made ineligible by enable_if}}
217 int (*p2)(int) = &templatedBar<int>; // expected-error{{address of overloaded function 'templatedBar' does not match required type 'int (int)'}} expected-note@214{{candidate function made ineligible by enable_if}}
219 a = templatedBar<int>; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@214{{candidate function made ineligible by enable_if}}
220 a = &templatedBar<int>; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@214{{candidate function made ineligible by enable_if}}
223 template <typename T>
224 T templatedConflict(T m) __attribute__((enable_if(false, ""))) { return T(); }
225 template <typename T>
226 T templatedConflict(T m) __attribute__((enable_if(true, ""))) { return T(); }
227 template <typename T>
228 T templatedConflict(T m) __attribute__((enable_if(1, ""))) { return T(); }
230 int (*p)(int) = templatedConflict<int>; // expected-error{{address of overloaded function 'templatedConflict' is ambiguous}} expected-note@224{{candidate function made ineligible by enable_if}} expected-note@226{{candidate function}} expected-note@228{{candidate function}}
231 int (*p0)(int) = &templatedConflict<int>; // expected-error{{address of overloaded function 'templatedConflict' is ambiguous}} expected-note@224{{candidate function made ineligible by enable_if}} expected-note@226{{candidate function}} expected-note@228{{candidate function}}
233 a = templatedConflict<int>; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@226{{candidate function}} expected-note@228{{candidate function}}
234 a = &templatedConflict<int>; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@226{{candidate function}} expected-note@228{{candidate function}}
237 int ovlNoCandidate(int m) __attribute__((enable_if(false, "")));
238 int ovlNoCandidate(int m) __attribute__((enable_if(0, "")));
240 int (*p)(int) = ovlNoCandidate; // expected-error{{address of overloaded function 'ovlNoCandidate' does not match required type}} expected-note@237{{made ineligible by enable_if}} expected-note@238{{made ineligible by enable_if}}
241 int (*p2)(int) = &ovlNoCandidate; // expected-error{{address of overloaded function 'ovlNoCandidate' does not match required type}} expected-note@237{{made ineligible by enable_if}} expected-note@238{{made ineligible by enable_if}}
243 a = ovlNoCandidate; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@237{{made ineligible by enable_if}} expected-note@238{{made ineligible by enable_if}}
244 a = &ovlNoCandidate; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@237{{made ineligible by enable_if}} expected-note@238{{made ineligible by enable_if}}
247 int noOvlNoCandidate(int m) __attribute__((enable_if(false, "")));
249 int (*p)(int) = noOvlNoCandidate; // expected-error{{cannot take address of function 'noOvlNoCandidate' becuase it has one or more non-tautological enable_if conditions}}
250 int (*p2)(int) = &noOvlNoCandidate; // expected-error{{cannot take address of function 'noOvlNoCandidate' becuase it has one or more non-tautological enable_if conditions}}
252 a = noOvlNoCandidate; // expected-error{{cannot take address of function 'noOvlNoCandidate' becuase it has one or more non-tautological enable_if conditions}}
253 a = &noOvlNoCandidate; // expected-error{{cannot take address of function 'noOvlNoCandidate' becuase it has one or more non-tautological enable_if conditions}}
258 using VoidFnTy = void (*)();
260 void foo(void *c) __attribute__((enable_if(0, "")));
261 void foo(int *c) __attribute__((enable_if(c, "")));
262 void foo(char *c) __attribute__((enable_if(1, "")));
265 auto A = reinterpret_cast<VoidFnTy>(foo);
266 auto AAmp = reinterpret_cast<VoidFnTy>(&foo);
268 using VoidFooTy = void (*)(void *);
269 auto B = reinterpret_cast<VoidFooTy>(foo);
270 auto BAmp = reinterpret_cast<VoidFooTy>(&foo);
272 using IntFooTy = void (*)(int *);
273 auto C = reinterpret_cast<IntFooTy>(foo);
274 auto CAmp = reinterpret_cast<IntFooTy>(&foo);
276 using CharFooTy = void (*)(void *);
277 auto D = reinterpret_cast<CharFooTy>(foo);
278 auto DAmp = reinterpret_cast<CharFooTy>(&foo);
281 void testItCStyle() {
282 auto A = (VoidFnTy)foo;
283 auto AAmp = (VoidFnTy)&foo;
285 using VoidFooTy = void (*)(void *);
286 auto B = (VoidFooTy)foo;
287 auto BAmp = (VoidFooTy)&foo;
289 using IntFooTy = void (*)(int *);
290 auto C = (IntFooTy)foo;
291 auto CAmp = (IntFooTy)&foo;
293 using CharFooTy = void (*)(void *);
294 auto D = (CharFooTy)foo;
295 auto DAmp = (CharFooTy)&foo;
299 namespace casting_templates {
300 template <typename T> void foo(T) {} // expected-note 4 {{candidate function}}
302 void foo(int *c) __attribute__((enable_if(c, ""))); //expected-note 4 {{candidate function}}
303 void foo(char *c) __attribute__((enable_if(c, ""))); //expected-note 4 {{candidate function}}
306 using IntFooTy = void (*)(int *);
307 auto A = reinterpret_cast<IntFooTy>(foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
308 auto ARef = reinterpret_cast<IntFooTy>(&foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
309 auto AExplicit = reinterpret_cast<IntFooTy>(foo<int*>);
311 using CharFooTy = void (*)(char *);
312 auto B = reinterpret_cast<CharFooTy>(foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
313 auto BRef = reinterpret_cast<CharFooTy>(&foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
314 auto BExplicit = reinterpret_cast<CharFooTy>(foo<char*>);
317 void testItCStyle() {
318 // constexpr is usable here because all of these should become static_casts.
319 using IntFooTy = void (*)(int *);
320 constexpr auto A = (IntFooTy)foo;
321 constexpr auto ARef = (IntFooTy)&foo;
322 constexpr auto AExplicit = (IntFooTy)foo<int*>;
324 using CharFooTy = void (*)(char *);
325 constexpr auto B = (CharFooTy)foo;
326 constexpr auto BRef = (CharFooTy)&foo;
327 constexpr auto BExplicit = (CharFooTy)foo<char*>;
329 static_assert(A == ARef && ARef == AExplicit, "");
330 static_assert(B == BRef && BRef == BExplicit, "");
334 namespace multiple_matches {
335 using NoMatchTy = void (*)();
337 void foo(float *c); //expected-note 4 {{candidate function}}
338 void foo(int *c) __attribute__((enable_if(1, ""))); //expected-note 4 {{candidate function}}
339 void foo(char *c) __attribute__((enable_if(1, ""))); //expected-note 4 {{candidate function}}
342 auto A = reinterpret_cast<NoMatchTy>(foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
343 auto ARef = reinterpret_cast<NoMatchTy>(&foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
345 auto C = (NoMatchTy)foo; // expected-error{{address of overloaded function 'foo' does not match required type 'void ()'}}
346 auto CRef = (NoMatchTy)&foo; // expected-error{{address of overloaded function 'foo' does not match required type 'void ()'}}
351 // (slightly reduced) code that motivated the bug...
353 void Function(int num)
354 __attribute__((enable_if(num != 0, "")));
355 void Function(int num, int a0)
356 __attribute__((enable_if(num != 1, "")));
359 using ns::Function; // expected-note 3{{declared here}}
361 Functioon(0); // expected-error{{use of undeclared identifier}} expected-error{{too few arguments}}
362 Functioon(0, 1); // expected-error{{use of undeclared identifier}}
363 Functioon(0, 1, 2); // expected-error{{use of undeclared identifier}}
367 void regularEnableIf(int a) __attribute__((enable_if(a, ""))); // expected-note 3{{declared here}} expected-note 3{{candidate function not viable}}
368 void runRegularEnableIf() {
369 regularEnableIf(0, 2); // expected-error{{no matching function}}
370 regularEnableIf(1, 2); // expected-error{{no matching function}}
371 regularEnableIf(); // expected-error{{no matching function}}
373 // Test without getting overload resolution involved
374 ::PR27122::regularEnableIf(0, 2); // expected-error{{too many arguments}}
375 ::PR27122::regularEnableIf(1, 2); // expected-error{{too many arguments}}
376 ::PR27122::regularEnableIf(); // expected-error{{too few arguments}}
380 void bar(int i) __attribute__((enable_if(i, ""))); // expected-note 2{{declared here}}
385 f.bar(); // expected-error{{too few arguments}}
386 f.bar(1, 2); // expected-error{{too many arguments}}
390 // Ideally, we should be able to handle value-dependent expressions sanely.
391 // Sadly, that isn't the case at the moment.
392 namespace dependent {
393 int error(int N) __attribute__((enable_if(N, ""))); // expected-note{{candidate disabled}}
394 int error(int N) __attribute__((enable_if(!N, ""))); // expected-note{{candidate disabled}}
395 template <int N> int callUnavailable() {
396 return error(N); // expected-error{{no matching function for call to 'error'}}
399 constexpr int noError(int N) __attribute__((enable_if(N, ""))) { return -1; }
400 constexpr int noError(int N) __attribute__((enable_if(!N, ""))) { return -1; }
401 constexpr int noError(int N) { return 0; }
404 constexpr int callNoError() { return noError(N); }
405 static_assert(callNoError<0>() == 0, "");
406 static_assert(callNoError<1>() == 0, "");
408 template <int N> constexpr int templated() __attribute__((enable_if(N, ""))) {
412 constexpr int A = templated<0>(); // expected-error{{no matching function for call to 'templated'}} expected-note@-4{{candidate disabled}}
413 static_assert(templated<1>() == 1, "");
415 template <int N> constexpr int callTemplated() { return templated<N>(); }
417 constexpr int B = callTemplated<0>(); // expected-error{{initialized by a constant expression}} expected-error@-2{{no matching function for call to 'templated'}} expected-note{{in instantiation of function template}} expected-note@-9{{candidate disabled}}
418 static_assert(callTemplated<1>() == 1, "");
422 void foo(int a, int b = 0, ...) __attribute__((enable_if(a && b, ""))); // expected-note 6{{disabled}}
429 foo(1, 0); // expected-error{{no matching}}
430 foo(1, 0, 2); // expected-error{{no matching}}
431 foo(1, 0, 2, 3); // expected-error{{no matching}}
438 foo(1, 0); // expected-error{{no matching}}
439 foo(1, 0, m); // expected-error{{no matching}}
440 foo(1, 0, m, 3); // expected-error{{no matching}}