Debian Codegraph @

clang/test/SemaTemplate/concepts-recursive-inst.cpp

raw
1 // RUN: %clang_cc1 -std=c++20 -verify %s
2 namespace GH53213 {
3 template<typename T>
4 concept c = requires(T t) { f(t); }; // #CDEF
5
6 auto f(c auto); // #FDEF
7
8 void g() {
9 f(0);
10 // expected-error@-1{{no matching function for call to 'f'}}
11 // expected-note@#FDEF{{constraints not satisfied}}
12 // expected-note@#FDEF{{because 'int' does not satisfy 'c'}}
13 // expected-note@#CDEF{{because 'f(t)' would be invalid: no matching function for call to 'f'}}
14 }
15 } // namespace GH53213
16
17 namespace GH45736 {
18 struct constrained;
19
20 template<typename T>
21 struct type {
22 };
23 template<typename T>
24 constexpr bool f(type<T>) {
25 return true;
26 }
27
28 template<typename T>
29 concept matches = f(type<T>());
30
31
32 struct constrained {
33 template<typename U> requires matches<U>
34 explicit constrained(U value) {
35 }
36 };
37
38 bool f(constrained const &) {
39 return true;
40 }
41
42 struct outer {
43 constrained state;
44 };
45
46 bool f(outer const & x) {
47 return f(x.state);
48 }
49 } // namespace GH45736
50
51 namespace DirectRecursiveCheck {
52 template<class T>
53 concept NotInf = true;
54 template<class T>
55 concept Inf = requires(T& v){ // #INF_REQ
56 {begin(v)}; // #INF_BEGIN_EXPR
57 };
58
59 void begin(NotInf auto& v){ } // #NOTINF_BEGIN
60 // This lookup should fail, since it results in a recursive check.
61 // However, this is a 'hard failure'(not a SFINAE failure or constraints
62 // violation), so it needs to cause the entire lookup to fail.
63 void begin(Inf auto& v){ } // #INF_BEGIN
64
65 struct my_range{
66 } rng;
67
68 void baz() {
69 auto it = begin(rng); // #BEGIN_CALL
70 // expected-error-re@#INF_REQ {{satisfaction of constraint {{.*}} depends on itself}}
71 // expected-note@#INF_BEGIN {{while checking the satisfaction of concept 'Inf<DirectRecursiveCheck::my_range>' requested here}}
72 // expected-note@#INF_BEGIN_EXPR {{while checking constraint satisfaction for template 'begin<DirectRecursiveCheck::my_range>' required here}}
73 // expected-note@#INF_BEGIN_EXPR {{while substituting deduced template arguments into function template 'begin'}}
74 // expected-note@#INF_BEGIN_EXPR {{in instantiation of requirement here}}
75 // expected-note@#INF_REQ {{while substituting template arguments into constraint expression here}}
76 // expected-note@#INF_BEGIN {{while checking the satisfaction of concept 'Inf<DirectRecursiveCheck::my_range>' requested here}}
77 // expected-note@#BEGIN_CALL {{while checking constraint satisfaction for template 'begin<DirectRecursiveCheck::my_range>' required here}}
78 // expected-note@#BEGIN_CALL {{while substituting deduced template arguments into function template}}
79
80 // Fallout of the failure is failed lookup, which is necessary to stop odd
81 // cascading errors.
82 // expected-error@#BEGIN_CALL {{no matching function for call to 'begin'}}
83 // expected-note@#NOTINF_BEGIN {{candidate function}}
84 // expected-note@#INF_BEGIN{{candidate template ignored: constraints not satisfied}}
85 }
86 } // namespace DirectRecursiveCheck
87
88 namespace GH50891 {
89 template <typename T>
90 concept Numeric = requires(T a) { // #NUMERIC
91 foo(a); // #FOO_CALL
92 };
93
94 struct Deferred {
95 friend void foo(Deferred);
96 template <Numeric TO> operator TO(); // #OP_TO
97 };
98
99 static_assert(Numeric<Deferred>); // #STATIC_ASSERT
100 // expected-error@#NUMERIC{{satisfaction of constraint 'requires (T a) { foo(a); }' depends on itself}}
101 // expected-note@#NUMERIC {{while substituting template arguments into constraint expression here}}
102 // expected-note@#OP_TO {{while checking the satisfaction of concept 'Numeric<GH50891::Deferred>' requested here}}
103 // expected-note@#OP_TO {{skipping 1 context}}
104 // expected-note@#FOO_CALL 2{{while checking constraint satisfaction for template}}
105 // expected-note@#FOO_CALL 2{{while substituting deduced template arguments into function template}}
106 // expected-note@#FOO_CALL 2{{in instantiation of requirement here}}
107 // expected-note@#NUMERIC {{while substituting template arguments into constraint expression here}}
108
109 // expected-error@#STATIC_ASSERT {{static assertion failed}}
110 // expected-note@#STATIC_ASSERT{{while checking the satisfaction of concept 'Numeric<Deferred>' requested here}}
111 // expected-note@#STATIC_ASSERT{{because 'Deferred' does not satisfy 'Numeric'}}
112 // expected-note@#FOO_CALL{{because 'foo(a)' would be invalid}}
113
114 } // namespace GH50891
115
116
117 namespace GH60323 {
118 // This should not diagnose, as it does not depend on itself.
119 struct End {
120 template<class T>
121 void go(T t) { }
122
123 template<class T>
124 auto endparens(T t)
125 requires requires { go(t); }
126 { return go(t); }
127 };
128
129 struct Size {
130 template<class T>
131 auto go(T t)
132 { return End().endparens(t); }
133
134 template<class T>
135 auto sizeparens(T t)
136 requires requires { go(t); }
137 { return go(t); }
138 };
139
140 int f()
141 {
142 int i = 42;
143 Size().sizeparens(i);
144 }
145 }
146
147 namespace CWG2369_Regressions {
148
149 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109397
150 namespace GCC_103997 {
151
152 template<typename _type, typename _stream>
153 concept streamable = requires(_stream &s, _type &&v) {
154 s << static_cast<_type &&>(v);
155 };
156
157 struct type_a {
158 template<typename _arg>
159 type_a &operator<<(_arg &&) {
160 // std::clog << "type_a" << std::endl;
161 return *this;
162 }
163 };
164
165 struct type_b {
166 type_b &operator<<(type_a const &) {
167 // std::clog << "type_b" << std::endl;
168 return *this;
169 }
170 };
171
172 struct type_c {
173 type_b b;
174 template<typename _arg>
175 requires streamable<_arg, type_b>
176 friend type_c &operator<<(type_c &c, _arg &&a) {
177 // std::clog << "type_c" << std::endl;
178 c.b << static_cast<_arg &&>(a);
179 return c;
180 }
181 };
182
183 void foo() {
184 type_a a;
185 type_c c;
186 a << c; // "type_a\n" (gcc gives error here)
187 c << a; // "type_c\ntype_b\n"
188 }
189
190 }
191
192 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108393
193 namespace GCC_108393 {
194
195 template<class>
196 struct iterator_traits
197 {};
198
199 template<class T>
200 requires requires(T __t, T __u) { __t == __u; }
201 struct iterator_traits<T>
202 {};
203
204 template<class T>
205 concept C = requires { typename iterator_traits<T>::A; };
206
207 struct unreachable_sentinel_t
208 {
209 template<C _Iter>
210 friend constexpr bool operator==(unreachable_sentinel_t, const _Iter&) noexcept;
211 };
212
213 template<class T>
214 struct S
215 {};
216
217 static_assert(!C<S<unreachable_sentinel_t>>);
218
219 }
220
221 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107429
222 namespace GCC_107429 {
223
224 struct tag_foo { } inline constexpr foo;
225 struct tag_bar { } inline constexpr bar;
226
227 template<typename... T>
228 auto f(tag_foo, T... x)
229 {
230 return (x + ...);
231 }
232
233 template<typename... T>
234 concept fooable = requires (T... x) { f(foo, x...); };
235
236 template<typename... T> requires (fooable<T...>)
237 auto f(tag_bar, T... x)
238 {
239 return f(foo, x...);
240 }
241
242 auto test()
243 {
244 return f(bar, 1, 2, 3);
245 }
246
247 }
248
249 namespace GCC_99599 {
250
251 struct foo_tag {};
252 struct bar_tag {};
253
254 template <class T>
255 concept fooable = requires(T it) {
256 invoke_tag(foo_tag{}, it); // <-- here
257 };
258
259 template <class T> auto invoke_tag(foo_tag, T in) { return in; }
260
261 template <fooable T> auto invoke_tag(bar_tag, T it) { return it; }
262
263 int main() {
264 // Neither line below compiles in GCC 11, independently of the other
265 return invoke_tag(foo_tag{}, 2) + invoke_tag(bar_tag{}, 2);
266 }
267
268 }
269
270 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99599#c22
271 namespace GCC_99599_2 {
272
273 template<typename T> class indirect {
274 public:
275 template<typename U> requires
276 requires (const T& t, const U& u) { t == u; }
277 friend constexpr bool operator==(const indirect&, const U&) { return false; }
278
279 private:
280 T* _M_ptr{};
281 };
282
283 indirect<int> i;
284 bool b = i == 1;
285
286 }
287
288 namespace GCC_99599_3 {
289
290 template<typename T>
291 struct S { T t; };
292
293 template<typename T>
294 concept C = sizeof(S<T>) > 0;
295
296 struct I;
297
298 struct from_range_t {
299 explicit from_range_t() = default;
300 };
301 inline constexpr from_range_t from_range;
302
303 template<typename T>
304 concept FromRange = __is_same_as (T, from_range_t);
305
306 //#define WORKAROUND
307 #ifdef WORKAROUND
308 template<FromRange U, C T>
309 void f(U, T*);
310 #else
311 template<C T>
312 void f(from_range_t, T*);
313 #endif
314
315 void f(...);
316
317 void g(I* p) {
318 f(0, p);
319 }
320
321 }
322
323 namespace GCC_99599_4 {
324
325 struct A {
326 A(...);
327 };
328
329 template <class T> void f(A, T) { }
330
331 int main()
332 {
333 f(42, 24);
334 }
335
336 }
337
338 namespace FAILED_GCC_110160 {
339 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110160
340 // Current heuristic FAILED; GCC trunk also failed
341 // https://godbolt.org/z/r3Pz9Tehz
342 #if 0
343 #include <sstream>
344 #include <string>
345
346 template <class T>
347 concept StreamCanReceiveString = requires(T& t, std::string s) {
348 { t << s };
349 };
350
351 struct NotAStream {};
352 struct UnrelatedType {};
353
354 template <StreamCanReceiveString S>
355 S& operator<<(S& s, UnrelatedType) {
356 return s;
357 }
358
359 static_assert(!StreamCanReceiveString<NotAStream>);
360
361 static_assert(StreamCanReceiveString<std::stringstream>);
362 #endif
363 }
364 }