xref: /aosp_15_r20/external/clang/test/SemaCXX/enable_if.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li typedef int (*fp)(int);
4*67e74705SXin Li int surrogate(int);
5*67e74705SXin Li struct Incomplete;  // expected-note{{forward declaration of 'Incomplete'}} \
6*67e74705SXin Li                     // expected-note {{forward declaration of 'Incomplete'}}
7*67e74705SXin Li 
8*67e74705SXin Li struct X {
9*67e74705SXin Li   X() = default;  // expected-note{{candidate constructor not viable: requires 0 arguments, but 1 was provided}}
10*67e74705SXin Li   X(const X&) = default;  // expected-note{{candidate constructor not viable: no known conversion from 'bool' to 'const X' for 1st argument}}
11*67e74705SXin Li   X(bool b) __attribute__((enable_if(b, "chosen when 'b' is true")));  // expected-note{{candidate disabled: chosen when 'b' is true}}
12*67e74705SXin Li 
13*67e74705SXin Li   void f(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));
14*67e74705SXin Li   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}}
15*67e74705SXin Li 
16*67e74705SXin Li   void g(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));  // expected-note{{candidate disabled: chosen when 'n' is zero}}
17*67e74705SXin Li 
18*67e74705SXin Li   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}}
19*67e74705SXin Li 
20*67e74705SXin Li   static void s(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));  // expected-note2{{candidate disabled: chosen when 'n' is zero}}
21*67e74705SXin Li 
22*67e74705SXin Li   void conflict(int n) __attribute__((enable_if(n+n == 10, "chosen when 'n' is five")));  // expected-note{{candidate function}}
23*67e74705SXin Li   void conflict(int n) __attribute__((enable_if(n*2 == 10, "chosen when 'n' is five")));  // expected-note{{candidate function}}
24*67e74705SXin Li 
25*67e74705SXin Li   void hidden_by_argument_conversion(Incomplete n, int m = 0) __attribute__((enable_if(m == 10, "chosen when 'm' is ten")));
26*67e74705SXin Li   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}}
27*67e74705SXin Li 
28*67e74705SXin Li   operator long() __attribute__((enable_if(true, "chosen on your platform")));
29*67e74705SXin Li   operator int() __attribute__((enable_if(false, "chosen on other platform")));
30*67e74705SXin Li 
operator fpX31*67e74705SXin Li   operator fp() __attribute__((enable_if(false, "never enabled"))) { return surrogate; }  // expected-note{{conversion candidate of type 'int (*)(int)'}}  // FIXME: the message is not displayed
32*67e74705SXin Li };
33*67e74705SXin Li 
f(int n)34*67e74705SXin Li 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}}
35*67e74705SXin Li {
36*67e74705SXin Li }
37*67e74705SXin Li 
f(int n)38*67e74705SXin Li 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'}}
39*67e74705SXin Li {
40*67e74705SXin Li }
41*67e74705SXin Li 
42*67e74705SXin Li X x1(true);
43*67e74705SXin Li X x2(false); // expected-error{{no matching constructor for initialization of 'X'}}
44*67e74705SXin Li 
old()45*67e74705SXin Li __attribute__((deprecated)) constexpr int old() { return 0; }  // expected-note2{{'old' has been explicitly marked deprecated here}}
46*67e74705SXin Li void deprec1(int i) __attribute__((enable_if(old() == 0, "chosen when old() is zero")));  // expected-warning{{'old' is deprecated}}
47*67e74705SXin Li void deprec2(int i) __attribute__((enable_if(old() == 0, "chosen when old() is zero")));  // expected-warning{{'old' is deprecated}}
48*67e74705SXin Li 
49*67e74705SXin Li void overloaded(int);
50*67e74705SXin Li void overloaded(long);
51*67e74705SXin Li 
52*67e74705SXin Li struct Int {
IntInt53*67e74705SXin Li   constexpr Int(int i) : i(i) { }
operator intInt54*67e74705SXin Li   constexpr operator int() const { return i; }
55*67e74705SXin Li   int i;
56*67e74705SXin Li };
57*67e74705SXin Li 
58*67e74705SXin Li 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*67e74705SXin Li 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}}
60*67e74705SXin Li 
61*67e74705SXin Li struct Nothing { };
62*67e74705SXin Li 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*67e74705SXin Li template<int N> void valuedep() __attribute__((enable_if(N == 1, "")));
64*67e74705SXin Li 
65*67e74705SXin Li // FIXME: we skip potential constant expression evaluation on value dependent
66*67e74705SXin Li // enable-if expressions
67*67e74705SXin Li int not_constexpr();
68*67e74705SXin Li template<int N> void valuedep() __attribute__((enable_if(N == not_constexpr(), "")));
69*67e74705SXin Li 
70*67e74705SXin Li template <typename T> void instantiationdep() __attribute__((enable_if(sizeof(sizeof(T)) != 0, "")));
71*67e74705SXin Li 
test()72*67e74705SXin Li void test() {
73*67e74705SXin Li   X x;
74*67e74705SXin Li   x.f(0);
75*67e74705SXin Li   x.f(1);
76*67e74705SXin Li   x.f(2);  // expected-error{{no matching member function for call to 'f'}}
77*67e74705SXin Li   x.f(3);  // expected-error{{no matching member function for call to 'f'}}
78*67e74705SXin Li 
79*67e74705SXin Li   x.g(0);
80*67e74705SXin Li   x.g(1);  // expected-error{{no matching member function for call to 'g'}}
81*67e74705SXin Li 
82*67e74705SXin Li   x.h(0);
83*67e74705SXin Li   x.h(1, 2);  // expected-error{{no matching member function for call to 'h'}}
84*67e74705SXin Li 
85*67e74705SXin Li   x.s(0);
86*67e74705SXin Li   x.s(1);  // expected-error{{no matching member function for call to 's'}}
87*67e74705SXin Li 
88*67e74705SXin Li   X::s(0);
89*67e74705SXin Li   X::s(1);  // expected-error{{no matching member function for call to 's'}}
90*67e74705SXin Li 
91*67e74705SXin Li   x.conflict(5);  // expected-error{{call to member function 'conflict' is ambiguous}}
92*67e74705SXin Li 
93*67e74705SXin Li   x.hidden_by_argument_conversion(10);  // expected-error{{argument type 'Incomplete' is incomplete}}
94*67e74705SXin Li   x.hidden_by_incomplete_return_value(10);  // expected-error{{calling 'hidden_by_incomplete_return_value' with incomplete return type 'Incomplete'}}
95*67e74705SXin Li 
96*67e74705SXin Li   deprec2(0);
97*67e74705SXin Li 
98*67e74705SXin Li   overloaded(x);
99*67e74705SXin Li 
100*67e74705SXin Li   default_argument(0);
101*67e74705SXin Li   default_argument(1, 2);  // expected-error{{no matching function for call to 'default_argument'}}
102*67e74705SXin Li 
103*67e74705SXin Li   default_argument_promotion(0);
104*67e74705SXin Li   default_argument_promotion(1, 2);  // expected-error{{no matching function for call to 'default_argument_promotion'}}
105*67e74705SXin Li 
106*67e74705SXin Li   int i = x(1);  // expected-error{{no matching function for call to object of type 'X'}}
107*67e74705SXin Li 
108*67e74705SXin Li   Nothing n;
109*67e74705SXin Li   typedep(0);  // expected-error{{no matching function for call to 'typedep'}}
110*67e74705SXin Li   typedep(1);
111*67e74705SXin Li   typedep(n);  // expected-note{{in instantiation of function template specialization 'typedep<Nothing>' requested here}}
112*67e74705SXin Li }
113*67e74705SXin Li 
114*67e74705SXin Li template <typename T> class C {
f()115*67e74705SXin Li   void f() __attribute__((enable_if(T::expr == 0, ""))) {}
g()116*67e74705SXin Li   void g() { f(); }
117*67e74705SXin Li };
118*67e74705SXin Li 
119*67e74705SXin Li int fn3(bool b) __attribute__((enable_if(b, ""))); // FIXME: This test should net 0 error messages.
test3()120*67e74705SXin Li template <class T> void test3() {
121*67e74705SXin Li   fn3(sizeof(T) == 1); // expected-error{{no matching function for call to 'fn3'}} expected-note@-2{{candidate disabled}}
122*67e74705SXin Li }
123*67e74705SXin Li 
124*67e74705SXin Li template <typename T>
125*67e74705SXin Li struct Y {
126*67e74705SXin Li   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}}
127*67e74705SXin Li };
128*67e74705SXin Li 
test4()129*67e74705SXin Li void test4() {
130*67e74705SXin Li   Y<int> y;
131*67e74705SXin Li 
132*67e74705SXin Li   int t0 = y.h(0);
133*67e74705SXin Li   int t1 = y.h(1, 2);  // expected-error{{no matching member function for call to 'h'}}
134*67e74705SXin Li }
135*67e74705SXin Li 
136*67e74705SXin Li // FIXME: issue an error (without instantiation) because ::h(T()) is not
137*67e74705SXin Li // convertible to bool, because return types aren't overloadable.
138*67e74705SXin Li void h(int);
outer()139*67e74705SXin Li template <typename T> void outer() {
140*67e74705SXin Li   void local_function() __attribute__((enable_if(::h(T()), "")));
141*67e74705SXin Li   local_function(); // expected-error{{no matching function for call to 'local_function'}} expected-note@-1{{candidate disabled}}
142*67e74705SXin Li };
143*67e74705SXin Li 
144*67e74705SXin Li namespace PR20988 {
145*67e74705SXin Li   struct Integer {
146*67e74705SXin Li     Integer(int);
147*67e74705SXin Li   };
148*67e74705SXin Li 
149*67e74705SXin Li   int fn1(const Integer &) __attribute__((enable_if(true, "")));
test1()150*67e74705SXin Li   template <class T> void test1() {
151*67e74705SXin Li     int &expr = T::expr();
152*67e74705SXin Li     fn1(expr);
153*67e74705SXin Li   }
154*67e74705SXin Li 
155*67e74705SXin Li   int fn2(const Integer &) __attribute__((enable_if(false, "")));  // expected-note{{candidate disabled}}
test2()156*67e74705SXin Li   template <class T> void test2() {
157*67e74705SXin Li     int &expr = T::expr();
158*67e74705SXin Li     fn2(expr);  // expected-error{{no matching function for call to 'fn2'}}
159*67e74705SXin Li   }
160*67e74705SXin Li 
161*67e74705SXin Li   int fn3(bool b) __attribute__((enable_if(b, ""))); // FIXME: This test should net 0 error messages.
test3()162*67e74705SXin Li   template <class T> void test3() {
163*67e74705SXin Li     fn3(sizeof(T) == 1); // expected-error{{no matching function for call to 'fn3'}} expected-note@-2{{candidate disabled}}
164*67e74705SXin Li   }
165*67e74705SXin Li }
166*67e74705SXin Li 
167*67e74705SXin Li namespace FnPtrs {
168*67e74705SXin Li   int ovlFoo(int m) __attribute__((enable_if(m > 0, "")));
169*67e74705SXin Li   int ovlFoo(int m);
170*67e74705SXin Li 
test()171*67e74705SXin Li   void test() {
172*67e74705SXin Li     // Assignment gives us a different code path than declarations, and `&foo`
173*67e74705SXin Li     // gives us a different code path than `foo`
174*67e74705SXin Li     int (*p)(int) = ovlFoo;
175*67e74705SXin Li     int (*p2)(int) = &ovlFoo;
176*67e74705SXin Li     int (*a)(int);
177*67e74705SXin Li     a = ovlFoo;
178*67e74705SXin Li     a = &ovlFoo;
179*67e74705SXin Li   }
180*67e74705SXin Li 
181*67e74705SXin Li   int ovlBar(int) __attribute__((enable_if(true, "")));
182*67e74705SXin Li   int ovlBar(int m) __attribute__((enable_if(false, "")));
test2()183*67e74705SXin Li   void test2() {
184*67e74705SXin Li     int (*p)(int) = ovlBar;
185*67e74705SXin Li     int (*p2)(int) = &ovlBar;
186*67e74705SXin Li     int (*a)(int);
187*67e74705SXin Li     a = ovlBar;
188*67e74705SXin Li     a = &ovlBar;
189*67e74705SXin Li   }
190*67e74705SXin Li 
191*67e74705SXin Li   int ovlConflict(int m) __attribute__((enable_if(true, "")));
192*67e74705SXin Li   int ovlConflict(int m) __attribute__((enable_if(1, "")));
test3()193*67e74705SXin Li   void test3() {
194*67e74705SXin Li     int (*p)(int) = ovlConflict; // expected-error{{address of overloaded function 'ovlConflict' is ambiguous}} expected-note@191{{candidate function}} expected-note@192{{candidate function}}
195*67e74705SXin Li     int (*p2)(int) = &ovlConflict; // expected-error{{address of overloaded function 'ovlConflict' is ambiguous}} expected-note@191{{candidate function}} expected-note@192{{candidate function}}
196*67e74705SXin Li     int (*a)(int);
197*67e74705SXin Li     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*67e74705SXin Li     a = &ovlConflict; // expected-error{{assigning to 'int (*)(int)' from incompatible type '<overloaded function type>'}} expected-note@191{{candidate function}} expected-note@192{{candidate function}}
199*67e74705SXin Li   }
200*67e74705SXin Li 
201*67e74705SXin Li   template <typename T>
templated(T m)202*67e74705SXin Li   T templated(T m) __attribute__((enable_if(true, ""))) { return T(); }
203*67e74705SXin Li   template <typename T>
templated(T m)204*67e74705SXin Li   T templated(T m) __attribute__((enable_if(false, ""))) { return T(); }
test4()205*67e74705SXin Li   void test4() {
206*67e74705SXin Li     int (*p)(int) = templated<int>;
207*67e74705SXin Li     int (*p2)(int) = &templated<int>;
208*67e74705SXin Li     int (*a)(int);
209*67e74705SXin Li     a = templated<int>;
210*67e74705SXin Li     a = &templated<int>;
211*67e74705SXin Li   }
212*67e74705SXin Li 
213*67e74705SXin Li   template <typename T>
templatedBar(T m)214*67e74705SXin Li   T templatedBar(T m) __attribute__((enable_if(m > 0, ""))) { return T(); }
test5()215*67e74705SXin Li   void test5() {
216*67e74705SXin Li     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*67e74705SXin Li     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}}
218*67e74705SXin Li     int (*a)(int);
219*67e74705SXin Li     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*67e74705SXin Li     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}}
221*67e74705SXin Li   }
222*67e74705SXin Li 
223*67e74705SXin Li   template <typename T>
templatedConflict(T m)224*67e74705SXin Li   T templatedConflict(T m) __attribute__((enable_if(false, ""))) { return T(); }
225*67e74705SXin Li   template <typename T>
templatedConflict(T m)226*67e74705SXin Li   T templatedConflict(T m) __attribute__((enable_if(true, ""))) { return T(); }
227*67e74705SXin Li   template <typename T>
templatedConflict(T m)228*67e74705SXin Li   T templatedConflict(T m) __attribute__((enable_if(1, ""))) { return T(); }
test6()229*67e74705SXin Li   void test6() {
230*67e74705SXin Li     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*67e74705SXin Li     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}}
232*67e74705SXin Li     int (*a)(int);
233*67e74705SXin Li     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*67e74705SXin Li     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}}
235*67e74705SXin Li   }
236*67e74705SXin Li 
237*67e74705SXin Li   int ovlNoCandidate(int m) __attribute__((enable_if(false, "")));
238*67e74705SXin Li   int ovlNoCandidate(int m) __attribute__((enable_if(0, "")));
test7()239*67e74705SXin Li   void test7() {
240*67e74705SXin Li     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*67e74705SXin Li     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}}
242*67e74705SXin Li     int (*a)(int);
243*67e74705SXin Li     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*67e74705SXin Li     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}}
245*67e74705SXin Li   }
246*67e74705SXin Li 
247*67e74705SXin Li   int noOvlNoCandidate(int m) __attribute__((enable_if(false, "")));
test8()248*67e74705SXin Li   void test8() {
249*67e74705SXin Li     int (*p)(int) = noOvlNoCandidate; // expected-error{{cannot take address of function 'noOvlNoCandidate' becuase it has one or more non-tautological enable_if conditions}}
250*67e74705SXin Li     int (*p2)(int) = &noOvlNoCandidate; // expected-error{{cannot take address of function 'noOvlNoCandidate' becuase it has one or more non-tautological enable_if conditions}}
251*67e74705SXin Li     int (*a)(int);
252*67e74705SXin Li     a = noOvlNoCandidate; // expected-error{{cannot take address of function 'noOvlNoCandidate' becuase it has one or more non-tautological enable_if conditions}}
253*67e74705SXin Li     a = &noOvlNoCandidate; // expected-error{{cannot take address of function 'noOvlNoCandidate' becuase it has one or more non-tautological enable_if conditions}}
254*67e74705SXin Li   }
255*67e74705SXin Li }
256*67e74705SXin Li 
257*67e74705SXin Li namespace casting {
258*67e74705SXin Li using VoidFnTy = void (*)();
259*67e74705SXin Li 
260*67e74705SXin Li void foo(void *c) __attribute__((enable_if(0, "")));
261*67e74705SXin Li void foo(int *c) __attribute__((enable_if(c, "")));
262*67e74705SXin Li void foo(char *c) __attribute__((enable_if(1, "")));
263*67e74705SXin Li 
testIt()264*67e74705SXin Li void testIt() {
265*67e74705SXin Li   auto A = reinterpret_cast<VoidFnTy>(foo);
266*67e74705SXin Li   auto AAmp = reinterpret_cast<VoidFnTy>(&foo);
267*67e74705SXin Li 
268*67e74705SXin Li   using VoidFooTy = void (*)(void *);
269*67e74705SXin Li   auto B = reinterpret_cast<VoidFooTy>(foo);
270*67e74705SXin Li   auto BAmp = reinterpret_cast<VoidFooTy>(&foo);
271*67e74705SXin Li 
272*67e74705SXin Li   using IntFooTy = void (*)(int *);
273*67e74705SXin Li   auto C = reinterpret_cast<IntFooTy>(foo);
274*67e74705SXin Li   auto CAmp = reinterpret_cast<IntFooTy>(&foo);
275*67e74705SXin Li 
276*67e74705SXin Li   using CharFooTy = void (*)(void *);
277*67e74705SXin Li   auto D = reinterpret_cast<CharFooTy>(foo);
278*67e74705SXin Li   auto DAmp = reinterpret_cast<CharFooTy>(&foo);
279*67e74705SXin Li }
280*67e74705SXin Li 
testItCStyle()281*67e74705SXin Li void testItCStyle() {
282*67e74705SXin Li   auto A = (VoidFnTy)foo;
283*67e74705SXin Li   auto AAmp = (VoidFnTy)&foo;
284*67e74705SXin Li 
285*67e74705SXin Li   using VoidFooTy = void (*)(void *);
286*67e74705SXin Li   auto B = (VoidFooTy)foo;
287*67e74705SXin Li   auto BAmp = (VoidFooTy)&foo;
288*67e74705SXin Li 
289*67e74705SXin Li   using IntFooTy = void (*)(int *);
290*67e74705SXin Li   auto C = (IntFooTy)foo;
291*67e74705SXin Li   auto CAmp = (IntFooTy)&foo;
292*67e74705SXin Li 
293*67e74705SXin Li   using CharFooTy = void (*)(void *);
294*67e74705SXin Li   auto D = (CharFooTy)foo;
295*67e74705SXin Li   auto DAmp = (CharFooTy)&foo;
296*67e74705SXin Li }
297*67e74705SXin Li }
298*67e74705SXin Li 
299*67e74705SXin Li namespace casting_templates {
foo(T)300*67e74705SXin Li template <typename T> void foo(T) {} // expected-note 4 {{candidate function}}
301*67e74705SXin Li 
302*67e74705SXin Li void foo(int *c) __attribute__((enable_if(c, ""))); //expected-note 4 {{candidate function}}
303*67e74705SXin Li void foo(char *c) __attribute__((enable_if(c, ""))); //expected-note 4 {{candidate function}}
304*67e74705SXin Li 
testIt()305*67e74705SXin Li void testIt() {
306*67e74705SXin Li   using IntFooTy = void (*)(int *);
307*67e74705SXin Li   auto A = reinterpret_cast<IntFooTy>(foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
308*67e74705SXin Li   auto ARef = reinterpret_cast<IntFooTy>(&foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
309*67e74705SXin Li   auto AExplicit = reinterpret_cast<IntFooTy>(foo<int*>);
310*67e74705SXin Li 
311*67e74705SXin Li   using CharFooTy = void (*)(char *);
312*67e74705SXin Li   auto B = reinterpret_cast<CharFooTy>(foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
313*67e74705SXin Li   auto BRef = reinterpret_cast<CharFooTy>(&foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
314*67e74705SXin Li   auto BExplicit = reinterpret_cast<CharFooTy>(foo<char*>);
315*67e74705SXin Li }
316*67e74705SXin Li 
testItCStyle()317*67e74705SXin Li void testItCStyle() {
318*67e74705SXin Li   // constexpr is usable here because all of these should become static_casts.
319*67e74705SXin Li   using IntFooTy = void (*)(int *);
320*67e74705SXin Li   constexpr auto A = (IntFooTy)foo;
321*67e74705SXin Li   constexpr auto ARef = (IntFooTy)&foo;
322*67e74705SXin Li   constexpr auto AExplicit = (IntFooTy)foo<int*>;
323*67e74705SXin Li 
324*67e74705SXin Li   using CharFooTy = void (*)(char *);
325*67e74705SXin Li   constexpr auto B = (CharFooTy)foo;
326*67e74705SXin Li   constexpr auto BRef = (CharFooTy)&foo;
327*67e74705SXin Li   constexpr auto BExplicit = (CharFooTy)foo<char*>;
328*67e74705SXin Li 
329*67e74705SXin Li   static_assert(A == ARef && ARef == AExplicit, "");
330*67e74705SXin Li   static_assert(B == BRef && BRef == BExplicit, "");
331*67e74705SXin Li }
332*67e74705SXin Li }
333*67e74705SXin Li 
334*67e74705SXin Li namespace multiple_matches {
335*67e74705SXin Li using NoMatchTy = void (*)();
336*67e74705SXin Li 
337*67e74705SXin Li void foo(float *c); //expected-note 4 {{candidate function}}
338*67e74705SXin Li void foo(int *c) __attribute__((enable_if(1, ""))); //expected-note 4 {{candidate function}}
339*67e74705SXin Li void foo(char *c) __attribute__((enable_if(1, ""))); //expected-note 4 {{candidate function}}
340*67e74705SXin Li 
testIt()341*67e74705SXin Li void testIt() {
342*67e74705SXin Li   auto A = reinterpret_cast<NoMatchTy>(foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
343*67e74705SXin Li   auto ARef = reinterpret_cast<NoMatchTy>(&foo); // expected-error{{reinterpret_cast cannot resolve overloaded function 'foo' to type}}
344*67e74705SXin Li 
345*67e74705SXin Li   auto C = (NoMatchTy)foo; // expected-error{{address of overloaded function 'foo' does not match required type 'void ()'}}
346*67e74705SXin Li   auto CRef = (NoMatchTy)&foo; // expected-error{{address of overloaded function 'foo' does not match required type 'void ()'}}
347*67e74705SXin Li }
348*67e74705SXin Li }
349*67e74705SXin Li 
350*67e74705SXin Li namespace PR27122 {
351*67e74705SXin Li // (slightly reduced) code that motivated the bug...
352*67e74705SXin Li namespace ns {
353*67e74705SXin Li void Function(int num)
354*67e74705SXin Li   __attribute__((enable_if(num != 0, "")));
355*67e74705SXin Li void Function(int num, int a0)
356*67e74705SXin Li   __attribute__((enable_if(num != 1, "")));
357*67e74705SXin Li }  // namespace ns
358*67e74705SXin Li 
359*67e74705SXin Li using ns::Function; // expected-note 3{{declared here}}
Run()360*67e74705SXin Li void Run() {
361*67e74705SXin Li   Functioon(0); // expected-error{{use of undeclared identifier}} expected-error{{too few arguments}}
362*67e74705SXin Li   Functioon(0, 1); // expected-error{{use of undeclared identifier}}
363*67e74705SXin Li   Functioon(0, 1, 2); // expected-error{{use of undeclared identifier}}
364*67e74705SXin Li }
365*67e74705SXin Li 
366*67e74705SXin Li // Extra tests
367*67e74705SXin Li void regularEnableIf(int a) __attribute__((enable_if(a, ""))); // expected-note 3{{declared here}} expected-note 3{{candidate function not viable}}
runRegularEnableIf()368*67e74705SXin Li void runRegularEnableIf() {
369*67e74705SXin Li   regularEnableIf(0, 2); // expected-error{{no matching function}}
370*67e74705SXin Li   regularEnableIf(1, 2); // expected-error{{no matching function}}
371*67e74705SXin Li   regularEnableIf(); // expected-error{{no matching function}}
372*67e74705SXin Li 
373*67e74705SXin Li   // Test without getting overload resolution involved
374*67e74705SXin Li   ::PR27122::regularEnableIf(0, 2); // expected-error{{too many arguments}}
375*67e74705SXin Li   ::PR27122::regularEnableIf(1, 2); // expected-error{{too many arguments}}
376*67e74705SXin Li   ::PR27122::regularEnableIf(); // expected-error{{too few arguments}}
377*67e74705SXin Li }
378*67e74705SXin Li 
379*67e74705SXin Li struct Foo {
380*67e74705SXin Li   void bar(int i) __attribute__((enable_if(i, ""))); // expected-note 2{{declared here}}
381*67e74705SXin Li };
382*67e74705SXin Li 
runFoo()383*67e74705SXin Li void runFoo() {
384*67e74705SXin Li   Foo f;
385*67e74705SXin Li   f.bar(); // expected-error{{too few arguments}}
386*67e74705SXin Li   f.bar(1, 2); // expected-error{{too many arguments}}
387*67e74705SXin Li }
388*67e74705SXin Li }
389*67e74705SXin Li 
390*67e74705SXin Li // Ideally, we should be able to handle value-dependent expressions sanely.
391*67e74705SXin Li // Sadly, that isn't the case at the moment.
392*67e74705SXin Li namespace dependent {
393*67e74705SXin Li int error(int N) __attribute__((enable_if(N, ""))); // expected-note{{candidate disabled}}
394*67e74705SXin Li int error(int N) __attribute__((enable_if(!N, ""))); // expected-note{{candidate disabled}}
callUnavailable()395*67e74705SXin Li template <int N> int callUnavailable() {
396*67e74705SXin Li   return error(N); // expected-error{{no matching function for call to 'error'}}
397*67e74705SXin Li }
398*67e74705SXin Li 
noError(int N)399*67e74705SXin Li constexpr int noError(int N) __attribute__((enable_if(N, ""))) { return -1; }
noError(int N)400*67e74705SXin Li constexpr int noError(int N) __attribute__((enable_if(!N, ""))) { return -1; }
noError(int N)401*67e74705SXin Li constexpr int noError(int N) { return 0; }
402*67e74705SXin Li 
403*67e74705SXin Li template <int N>
callNoError()404*67e74705SXin Li constexpr int callNoError() { return noError(N); }
405*67e74705SXin Li static_assert(callNoError<0>() == 0, "");
406*67e74705SXin Li static_assert(callNoError<1>() == 0, "");
407*67e74705SXin Li 
templated()408*67e74705SXin Li template <int N> constexpr int templated() __attribute__((enable_if(N, ""))) {
409*67e74705SXin Li   return 1;
410*67e74705SXin Li }
411*67e74705SXin Li 
412*67e74705SXin Li constexpr int A = templated<0>(); // expected-error{{no matching function for call to 'templated'}} expected-note@-4{{candidate disabled}}
413*67e74705SXin Li static_assert(templated<1>() == 1, "");
414*67e74705SXin Li 
callTemplated()415*67e74705SXin Li template <int N> constexpr int callTemplated() { return templated<N>(); }
416*67e74705SXin Li 
417*67e74705SXin Li 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*67e74705SXin Li static_assert(callTemplated<1>() == 1, "");
419*67e74705SXin Li }
420