1 // RUN: %clang_cc1 -std=c++11 -verify %s
2
3 typedef int (*fp)(int);
4 int surrogate(int);
5 struct Incomplete; // expected-note{{forward declaration of 'Incomplete'}} \
6 // expected-note {{forward declaration of 'Incomplete'}}
7
8 struct X {
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}}
12
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}}
15
16 void g(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero"))); // expected-note{{candidate disabled: chosen when 'n' is zero}}
17
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}}
19
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}}
21
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}}
24
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}}
27
28 operator long() __attribute__((enable_if(true, "chosen on your platform")));
29 operator int() __attribute__((enable_if(false, "chosen on other platform")));
30
operator fpX31 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 };
33
f(int n)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}}
35 {
36 }
37
f(int n)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'}}
39 {
40 }
41
42 X x1(true);
43 X x2(false); // expected-error{{no matching constructor for initialization of 'X'}}
44
old()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}}
48
49 void overloaded(int);
50 void overloaded(long);
51
52 struct Int {
IntInt53 constexpr Int(int i) : i(i) { }
operator intInt54 constexpr operator int() const { return i; }
55 int i;
56 };
57
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}}
60
61 struct Nothing { };
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, "")));
64
65 // FIXME: we skip potential constant expression evaluation on value dependent
66 // enable-if expressions
67 int not_constexpr();
68 template<int N> void valuedep() __attribute__((enable_if(N == not_constexpr(), "")));
69
70 template <typename T> void instantiationdep() __attribute__((enable_if(sizeof(sizeof(T)) != 0, "")));
71
test()72 void test() {
73 X x;
74 x.f(0);
75 x.f(1);
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'}}
78
79 x.g(0);
80 x.g(1); // expected-error{{no matching member function for call to 'g'}}
81
82 x.h(0);
83 x.h(1, 2); // expected-error{{no matching member function for call to 'h'}}
84
85 x.s(0);
86 x.s(1); // expected-error{{no matching member function for call to 's'}}
87
88 X::s(0);
89 X::s(1); // expected-error{{no matching member function for call to 's'}}
90
91 x.conflict(5); // expected-error{{call to member function 'conflict' is ambiguous}}
92
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'}}
95
96 deprec2(0);
97
98 overloaded(x);
99
100 default_argument(0);
101 default_argument(1, 2); // expected-error{{no matching function for call to 'default_argument'}}
102
103 default_argument_promotion(0);
104 default_argument_promotion(1, 2); // expected-error{{no matching function for call to 'default_argument_promotion'}}
105
106 int i = x(1); // expected-error{{no matching function for call to object of type 'X'}}
107
108 Nothing n;
109 typedep(0); // expected-error{{no matching function for call to 'typedep'}}
110 typedep(1);
111 typedep(n); // expected-note{{in instantiation of function template specialization 'typedep<Nothing>' requested here}}
112 }
113
114 template <typename T> class C {
f()115 void f() __attribute__((enable_if(T::expr == 0, ""))) {}
g()116 void g() { f(); }
117 };
118
119 int fn3(bool b) __attribute__((enable_if(b, "")));
test3()120 template <class T> void test3() {
121 fn3(sizeof(T) == 1);
122 }
123
124 template <typename T>
125 struct Y {
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}}
127 };
128
test4()129 void test4() {
130 Y<int> y;
131
132 int t0 = y.h(0);
133 int t1 = y.h(1, 2); // expected-error{{no matching member function for call to 'h'}}
134 }
135
136 // FIXME: issue an error (without instantiation) because ::h(T()) is not
137 // convertible to bool, because return types aren't overloadable.
138 void h(int);
outer()139 template <typename T> void outer() {
140 void local_function() __attribute__((enable_if(::h(T()), "")));
141 local_function();
142 };
143
144 namespace PR20988 {
145 struct Integer {
146 Integer(int);
147 };
148
149 int fn1(const Integer &) __attribute__((enable_if(true, "")));
test1()150 template <class T> void test1() {
151 int &expr = T::expr();
152 fn1(expr);
153 }
154
155 int fn2(const Integer &) __attribute__((enable_if(false, ""))); // expected-note{{candidate disabled}}
test2()156 template <class T> void test2() {
157 int &expr = T::expr();
158 fn2(expr); // expected-error{{no matching function for call to 'fn2'}}
159 }
160
161 int fn3(bool b) __attribute__((enable_if(b, "")));
test3()162 template <class T> void test3() {
163 fn3(sizeof(T) == 1);
164 }
165 }
166
167 namespace FnPtrs {
168 int ovlFoo(int m) __attribute__((enable_if(m > 0, "")));
169 int ovlFoo(int m);
170
test()171 void test() {
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;
176 int (*a)(int);
177 a = ovlFoo;
178 a = &ovlFoo;
179 }
180
181 int ovlBar(int) __attribute__((enable_if(true, "")));
182 int ovlBar(int m) __attribute__((enable_if(false, "")));
test2()183 void test2() {
184 int (*p)(int) = ovlBar;
185 int (*p2)(int) = &ovlBar;
186 int (*a)(int);
187 a = ovlBar;
188 a = &ovlBar;
189 }
190
191 int ovlConflict(int m) __attribute__((enable_if(true, "")));
192 int ovlConflict(int m) __attribute__((enable_if(1, "")));
test3()193 void test3() {
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}}
196 int (*a)(int);
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}}
199 }
200
201 template <typename T>
templated(T m)202 T templated(T m) __attribute__((enable_if(true, ""))) { return T(); }
203 template <typename T>
templated(T m)204 T templated(T m) __attribute__((enable_if(false, ""))) { return T(); }
test4()205 void test4() {
206 int (*p)(int) = templated<int>;
207 int (*p2)(int) = &templated<int>;
208 int (*a)(int);
209 a = templated<int>;
210 a = &templated<int>;
211 }
212
213 template <typename T>
templatedBar(T m)214 T templatedBar(T m) __attribute__((enable_if(m > 0, ""))) { return T(); }
test5()215 void test5() {
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}}
218 int (*a)(int);
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}}
221 }
222
223 template <typename T>
templatedConflict(T m)224 T templatedConflict(T m) __attribute__((enable_if(false, ""))) { return T(); }
225 template <typename T>
templatedConflict(T m)226 T templatedConflict(T m) __attribute__((enable_if(true, ""))) { return T(); }
227 template <typename T>
templatedConflict(T m)228 T templatedConflict(T m) __attribute__((enable_if(1, ""))) { return T(); }
test6()229 void test6() {
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}}
232 int (*a)(int);
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}}
235 }
236
237 int ovlNoCandidate(int m) __attribute__((enable_if(false, "")));
238 int ovlNoCandidate(int m) __attribute__((enable_if(0, "")));
test7()239 void test7() {
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}}
242 int (*a)(int);
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}}
245 }
246
247 int noOvlNoCandidate(int m) __attribute__((enable_if(false, "")));
test8()248 void test8() {
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}}
251 int (*a)(int);
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}}
254 }
255 }
256