1 // RUN: %clang_cc1 -std=c++20 -verify %s 2 3 namespace PR47043 { 4 template<typename T> concept True = true; 5 template<typename ...T> concept AllTrue1 = True<T>; // expected-error {{expression contains unexpanded parameter pack 'T'}} 6 template<typename ...T> concept AllTrue2 = (True<T> && ...); 7 static_assert(AllTrue2<int, float, char>); 8 } 9 10 namespace PR47025 { 11 template<typename ...T> concept AllAddable1 = requires(T ...t) { (void(t + 1), ...); }; 12 template<typename ...T> concept AllAddable2 = (requires(T ...t) { (t + 1); } && ...); // expected-error {{requirement contains unexpanded parameter pack 't'}} 13 template<typename ...T> concept AllAddable3 = (requires(T t) { (t + 1); } && ...); 14 template<typename ...T> concept AllAddable4 = requires(T t) { (t + 1); }; // expected-error {{expression contains unexpanded parameter pack 'T'}} 15 template<typename ...T> concept AllAddable5 = requires(T t) { (void(t + 1), ...); }; // expected-error {{does not contain any unexpanded}} 16 template<typename ...T> concept AllAddable6 = (requires { (T() + 1); } && ...); 17 template<typename ...T> concept AllAddable7 = requires { (T() + 1); }; // expected-error {{expression contains unexpanded parameter pack 'T'}} 18 19 static_assert(AllAddable1<int, float>); 20 static_assert(AllAddable3<int, float>); 21 static_assert(AllAddable6<int, float>); 22 static_assert(!AllAddable1<int, void>); 23 static_assert(!AllAddable3<int, void>); 24 static_assert(!AllAddable6<int, void>); 25 } 26 27 namespace PR45699 { 28 template<class> concept C = true; // expected-note 2{{here}} 29 template<class ...Ts> void f1a() requires C<Ts>; // expected-error {{requires clause contains unexpanded parameter pack 'Ts'}} 30 template<class ...Ts> requires C<Ts> void f1b(); // expected-error {{requires clause contains unexpanded parameter pack 'Ts'}} 31 template<class ...Ts> void f2a() requires (C<Ts> && ...); 32 template<class ...Ts> requires (C<Ts> && ...) void f2b(); 33 template<class ...Ts> void f3a() requires C<Ts...>; // expected-error {{pack expansion used as argument for non-pack parameter of concept}} 34 template<class ...Ts> requires C<Ts...> void f3b(); // expected-error {{pack expansion used as argument for non-pack parameter of concept}} 35 template<class ...Ts> void f4() { 36 ([] () requires C<Ts> {} ()); // expected-error {{expression contains unexpanded parameter pack 'Ts'}} 37 ([]<int = 0> requires C<Ts> () {} ()); // expected-error {{expression contains unexpanded parameter pack 'Ts'}} 38 } 39 template<class ...Ts> void f5() { 40 ([] () requires C<Ts> {} (), ...); 41 ([]<int = 0> requires C<Ts> () {} (), ...); 42 } 43 void g() { 44 f1a(); 45 f1b(); // FIXME: Bad error recovery. expected-error {{undeclared identifier}} 46 f2a(); 47 f2b(); 48 f3a(); 49 f3b(); // FIXME: Bad error recovery. expected-error {{undeclared identifier}} 50 f4(); 51 f5(); 52 } 53 } 54 55 namespace P0857R0 { 56 void f() { 57 auto x = []<bool B> requires B {}; // expected-note {{constraints not satisfied}} expected-note {{false}} 58 x.operator()<true>(); 59 x.operator()<false>(); // expected-error {{no matching member function}} 60 } 61 62 // FIXME: This is valid under P0857R0. 63 template<typename T> concept C = true; 64 template<template<typename T> requires C<T> typename U> struct X {}; // expected-error {{requires 'class'}} expected-error 0+{{}} 65 template<typename T> requires C<T> struct Y {}; 66 X<Y> xy; // expected-error {{no template named 'X'}} 67 } 68 69 namespace PR50306 { 70 template<typename T> concept NotInt = sizeof(T) != sizeof(int); // expected-note {{because}} 71 template<typename T> void f() { 72 [](NotInt auto) {}(T()); // expected-error {{no matching function}} expected-note {{constraints not satisfied}} expected-note {{because}} 73 } 74 template void f<char>(); // OK 75 template void f<int>(); // expected-note {{in instantiation of}} 76 } 77 78 namespace PackInTypeConstraint { 79 template<typename T, typename U> concept C = sizeof(T) == sizeof(int); // expected-note 3{{}} 80 81 template<typename ...T, C<T> U> void h1(); // expected-error {{type constraint contains unexpanded parameter pack 'T'}} 82 template<typename ...T, C<T> ...U> void h2(); 83 template<typename ...T> void h3(C<T> auto); // expected-error {{type constraint contains unexpanded parameter pack 'T'}} 84 template<typename ...T> void h4(C<T> auto...); 85 86 template<typename ...T> void f1() { 87 []<C<T> U>(U u){}(T()); // expected-error {{unexpanded parameter pack 'T'}} 88 } 89 template<typename ...T> void f2() { 90 ([]<C<T> U>(U u){}(T()), ...); // expected-error {{no match}} expected-note 2{{}} 91 } 92 template void f2<int, int, int>(); // OK 93 template void f2<int, char, double>(); // expected-note {{in instantiation of}} 94 void f3() { 95 ([]<typename ...T, C<T> U>(U u){}(0), // expected-error {{type constraint contains unexpanded parameter pack 'T'}} 96 ...); // expected-error {{does not contain any unexpanded}} 97 } 98 99 template<typename ...T> void g1() { 100 [](C<T> auto){}(T()); // expected-error {{expression contains unexpanded parameter pack 'T'}} 101 } 102 template<typename ...T> void g2() { 103 ([](C<T> auto){}(T()), ...); // expected-error {{no matching function}} expected-note {{constraints not satisfied}} expected-note {{because}} 104 } 105 template void g2<int, int, int>(); // OK 106 template void g2<int, char, double>(); // expected-note {{in instantiation of}} 107 void g3() { 108 ([]<typename ...T>(C<T> auto){}(1), // expected-error {{type constraint contains unexpanded parameter pack 'T'}} 109 ...); // expected-error {{does not contain any unexpanded}} 110 } 111 112 template<typename ...T> void g4() { 113 []() -> C<T> auto{ return T(); }(); // expected-error {{expression contains unexpanded parameter pack 'T'}} 114 } 115 template<typename ...T> void g5() { 116 ([]() -> C<T> auto{ // expected-error-re {{deduced type {{.*}} does not satisfy}} 117 return T(); 118 }(), ...); 119 } 120 template void g5<int, int, int>(); // OK 121 template void g5<int, char, double>(); // expected-note {{in instantiation of}} 122 void g6() { 123 ([]<typename ...T>() -> C<T> auto{ // expected-error {{declaration type contains unexpanded parameter pack 'T'}} 124 return T(); // expected-error {{expression contains unexpanded parameter pack 'T'}} 125 }(), 126 ...); // expected-error {{does not contain any unexpanded}} 127 } 128 } 129 130 namespace BuiltinIsConstantEvaluated { 131 // Check that we do all satisfaction and diagnostic checks in a constant context. 132 template<typename T> concept C = __builtin_is_constant_evaluated(); // expected-warning {{always}} 133 static_assert(C<int>); 134 135 template<typename T> concept D = __builtin_is_constant_evaluated() == true; // expected-warning {{always}} 136 static_assert(D<int>); 137 138 template<typename T> concept E = __builtin_is_constant_evaluated() == true && // expected-warning {{always}} 139 false; // expected-note {{'false' evaluated to false}} 140 static_assert(E<int>); // expected-error {{failed}} expected-note {{because 'int' does not satisfy 'E'}} 141 142 template<typename T> concept F = __builtin_is_constant_evaluated() == false; // expected-warning {{always}} 143 // expected-note@-1 {{'__builtin_is_constant_evaluated() == false' (1 == 0)}} 144 static_assert(F<int>); // expected-error {{failed}} expected-note {{because 'int' does not satisfy 'F'}} 145 146 template<typename T> concept G = __builtin_is_constant_evaluated() && // expected-warning {{always}} 147 false; // expected-note {{'false' evaluated to false}} 148 static_assert(G<int>); // expected-error {{failed}} expected-note {{because 'int' does not satisfy 'G'}} 149 } 150 151 namespace NoConstantFolding { 152 // Ensure we use strict constant evaluation rules when checking satisfaction. 153 int n; 154 template <class T> concept C = &n + 3 - 3 == &n; // expected-error {{non-constant expression}} expected-note {{cannot refer to element 3 of non-array object}} 155 static_assert(C<void>); // expected-note {{while checking}} 156 } 157 158 namespace PR50337 { 159 template <typename T> concept foo = true; 160 template <typename T> concept foo2 = foo<T> && true; 161 void f(foo auto, auto); 162 void f(foo2 auto, auto); 163 void g() { f(1, 2); } 164 } 165 166 namespace PR50561 { 167 template<typename> concept C = false; 168 template<typename T, typename U> void f(T, U); 169 template<C T, typename U> void f(T, U) = delete; 170 void g() { f(0, 0); } 171 } 172 173 namespace PR49188 { 174 template<class T> concept C = false; // expected-note 7 {{because 'false' evaluated to false}} 175 176 C auto f1() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 177 return void(); 178 } 179 C auto f2() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 180 return; 181 } 182 C auto f3() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 183 } 184 C decltype(auto) f4() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 185 return void(); 186 } 187 C decltype(auto) f5() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 188 return; 189 } 190 C decltype(auto) f6() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 191 } 192 C auto& f7() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 193 return void(); 194 } 195 C auto& f8() { 196 return; // expected-error {{cannot deduce return type 'C auto &' from omitted return expression}} 197 } 198 C auto& f9() { // expected-error {{cannot deduce return type 'C auto &' for function with no return statements}} 199 } 200 } 201 namespace PR53911 { 202 template<class T> concept C = false; // expected-note 3 {{because 'false' evaluated to false}} 203 204 C auto *f1() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 205 return (void*)nullptr; 206 } 207 C auto *f2() { // expected-error {{deduced type 'int' does not satisfy 'C'}} 208 return (int*)nullptr; 209 } 210 C auto *****f3() { // expected-error {{deduced type 'int' does not satisfy 'C'}} 211 return (int*****)nullptr; 212 } 213 } 214 215 namespace PR54379 { 216 template <int N> 217 struct A { 218 static void f() requires (N == 0) { return; } // expected-note {{candidate template ignored: constraints not satisfied}} expected-note {{evaluated to false}} 219 static void f() requires (N == 1) { return; } // expected-note {{candidate template ignored: constraints not satisfied}} expected-note {{evaluated to false}} 220 }; 221 void (*f1)() = A<2>::f; // expected-error {{address of overloaded function 'f' does not match required type}} 222 223 struct B { 224 template <int N2 = 1> static void f() requires (N2 == 0) { return; } // expected-note {{candidate template ignored: constraints not satisfied [with N2 = 1]}} expected-note {{evaluated to false}} 225 }; 226 void (*f2)() = B::f; // expected-error {{address of overloaded function 'f' does not match required type}} 227 } 228 229 namespace PR54443 { 230 231 template <class T, class U> 232 struct is_same { static constexpr bool value = false; }; 233 234 template <class T> 235 struct is_same<T, T> { static constexpr bool value = true; }; 236 237 template <class T, class U> 238 concept same_as = is_same<T, U>::value; // expected-note-re 4 {{because {{.*}} evaluated to false}} 239 240 int const &f(); 241 242 same_as<int const> auto i1 = f(); // expected-error {{deduced type 'int' does not satisfy 'same_as<const int>'}} 243 same_as<int const> auto &i2 = f(); 244 same_as<int const> auto &&i3 = f(); // expected-error {{deduced type 'const int &' does not satisfy 'same_as<const int>'}} 245 246 same_as<int const &> auto i4 = f(); // expected-error {{deduced type 'int' does not satisfy 'same_as<const int &>'}} 247 same_as<int const &> auto &i5 = f(); // expected-error {{deduced type 'const int' does not satisfy 'same_as<const int &>'}} 248 same_as<int const &> auto &&i6 = f(); 249 250 template <class T> 251 concept C = false; // expected-note 3 {{because 'false' evaluated to false}} 252 253 int **const &g(); 254 255 C auto **j1 = g(); // expected-error {{deduced type 'int' does not satisfy 'C'}} 256 C auto **&j2 = g(); // expected-error {{deduced type 'int' does not satisfy 'C'}} 257 C auto **&&j3 = g(); // expected-error {{deduced type 'int' does not satisfy 'C'}} 258 } 259 260 namespace SubConstraintChecks { 261 template <typename T> 262 concept TrueConstraint = true; 263 template <typename T> 264 concept FalseConstraint = false; 265 266 template <typename T, typename... Us> 267 class ContainsConstrainedFuncTrue { 268 public: 269 template <typename V, TrueConstraint Constrained> 270 static void func(V &&, Constrained &&C); 271 }; 272 template <typename T, typename... Us> 273 class ContainsConstrainedFuncFalse { 274 public: 275 template <typename V, FalseConstraint Constrained> 276 static void func(V &&, Constrained &&C); 277 }; 278 279 template <typename... Us> 280 concept TrueConstraint2 = 281 requires(float &&t) { 282 ContainsConstrainedFuncTrue<float, Us...>::func(5, 0.0); 283 }; 284 template <typename... Us> 285 concept FalseConstraint2 = 286 requires(float &&t) { 287 ContainsConstrainedFuncFalse<float, Us...>::func(5, 0.0); // #FC2_CONSTR 288 }; 289 290 template <typename T> 291 void useTrue(int F) 292 requires TrueConstraint2<int> 293 {} 294 295 template <typename T> 296 void useFalse(int F) // #USE_FALSE 297 requires FalseConstraint2<int> // #USE_FALSE_CONSTR 298 {} 299 300 // Should only diagnose 'false' once instantiated. 301 void UseUse() { 302 useTrue<int>(5); 303 useFalse<int>(5); 304 // expected-error@-1{{no matching function for call to 'useFalse'}} 305 // expected-note@#USE_FALSE{{constraints not satisfied}} 306 // expected-note@#USE_FALSE_CONSTR{{because 'int' does not satisfy 'FalseConstraint2'}} 307 // expected-note@#FC2_CONSTR {{would be invalid: no matching function for call to 'func'}} 308 } 309 } // namespace SubConstraintChecks 310 311 namespace DeducedTemplateArgs { 312 template <typename Itr> struct ItrTraits { 313 template <typename PtrItr> struct Ptr { 314 }; 315 template <typename PtrItr> 316 requires requires { typename PtrItr::pointer; } 317 struct Ptr<PtrItr> { 318 using type = typename Itr::pointer; 319 }; 320 using pointer = typename Ptr<Itr>::type; // #TRAITS_PTR 321 }; 322 323 struct complete_itr { 324 using pointer = int; 325 }; 326 327 template <typename T> class Complete { 328 using ItrType = ItrTraits<complete_itr>; 329 ItrType begin() noexcept { return ItrType(); } 330 }; 331 332 // This version doesn't have 'pointer', so error confirms we are in the first 333 // verison of 'Ptr'. 334 struct not_complete_itr { 335 }; 336 337 template <typename T> class NotComplete { 338 using ItrType = ItrTraits<not_complete_itr>; 339 ItrType begin() noexcept { return ItrType(); } 340 // expected-error@#TRAITS_PTR{{no type named 'type' in }} 341 // expected-note@-2{{in instantiation of template class }} 342 }; 343 } // namespace DeducedTemplateArgs 344 345 namespace DeferredInstantiationInstScope { 346 template <typename T> 347 struct remove_ref { 348 using type = T; 349 }; 350 template <typename T> 351 struct remove_ref<T &> { 352 using type = T; 353 }; 354 template <typename T> 355 struct remove_ref<T &&> { 356 using type = T; 357 }; 358 359 template <typename T> 360 constexpr bool IsInt = PR54443::is_same<typename remove_ref<T>::type, 361 int>::value; 362 363 template <typename U> 364 void SingleDepthReferencesTop(U &&u) { 365 struct lc { 366 void operator()() // #SDRT_OP 367 requires IsInt<decltype(u)> // #SDRT_REQ 368 {} 369 }; 370 lc lv; 371 lv(); // #SDRT_CALL 372 } 373 374 template <typename U> 375 void SingleDepthReferencesTopNotCalled(U &&u) { 376 struct lc { 377 void operator()() 378 requires IsInt<typename decltype(u)::FOO> 379 {} 380 }; 381 lc lv; 382 } 383 384 template <typename U> 385 void SingleDepthReferencesTopCalled(U &&u) { 386 struct lc { 387 void operator()() // #CALLOP 388 requires IsInt<typename decltype(u)::FOO> // #CONSTR 389 {} 390 }; 391 lc lv; 392 lv(); 393 // expected-error@-1{{no matching function for call to object of type 'lc'}} 394 // expected-note@#SDRTC{{in instantiation of function template}} 395 // expected-note@#CALLOP{{constraints not satisfied}} 396 // expected-note@#CONSTR{{substituted constraint expression is ill-formed}} 397 } 398 399 template <typename U> 400 void SingleDepthReferencesTopLambda(U &&u) { 401 []() 402 requires IsInt<decltype(u)> 403 {}(); 404 } 405 406 template <typename U> 407 void DoubleDepthReferencesTop(U &&u) { 408 struct lc { // #DDRT_STRCT 409 void operator()() { 410 struct lc2 { 411 void operator()() // #DDRT_OP 412 requires IsInt<decltype(u)> // #DDRT_REQ 413 {} 414 }; 415 lc2 lv2; 416 lv2(); // #DDRT_CALL 417 } 418 }; 419 lc lv; 420 lv(); 421 } 422 423 template <typename U> 424 void DoubleDepthReferencesTopLambda(U &&u) { 425 []() { []() 426 requires IsInt<decltype(u)> 427 {}(); }(); 428 } 429 430 template <typename U> 431 void DoubleDepthReferencesAll(U &&u) { 432 struct lc { // #DDRA_STRCT 433 void operator()(U &&u2) { 434 struct lc2 { 435 void operator()(U &&u3) // #DDRA_OP 436 requires IsInt<decltype(u)> && // #DDRA_REQ 437 IsInt<decltype(u2)> && IsInt<decltype(u3)> 438 {} 439 }; 440 lc2 lv2; 441 lv2(u2); // #DDRA_CALL 442 } 443 }; 444 lc lv; 445 lv(u); 446 } 447 448 template <typename U> 449 void DoubleDepthReferencesAllLambda(U &&u) { 450 [](U &&u2) { 451 [](U && u3) 452 requires IsInt<decltype(u)> && 453 IsInt<decltype(u2)> && IsInt<decltype(u3)> 454 {}(u2); 455 }(u); 456 } 457 458 template <typename U> 459 struct CausesFriendConstraint { 460 template <typename V> 461 friend void FriendFunc(CausesFriendConstraint, V) // #FF_DECL 462 requires IsInt<U> && 463 IsInt<V> // #FF_REQ 464 {} 465 }; 466 // FIXME: Re-enable this test when constraints are allowed to refer to captures. 467 // template<typename T> 468 // void ChecksCapture(T x) { 469 // [y = x]() requires(IsInt<decltype(y)>){}(); 470 // } 471 472 template <typename T> 473 void ChecksLocalVar(T x) { 474 T Local; 475 []() 476 requires(IsInt<decltype(Local)>) 477 {}(); 478 } 479 480 template <typename T> 481 void LocalStructMemberVar(T x) { 482 struct S { 483 T local; 484 void foo() 485 requires(IsInt<decltype(local)>) // #LSMV_REQ 486 {} 487 } s; 488 s.foo(); // #LSMV_CALL 489 }; 490 491 template <typename T> 492 struct ChecksMemberVar { 493 T t; 494 void foo() 495 requires(IsInt<decltype(t)>) // #CMV_FOO 496 {} 497 template <typename U> 498 void foo2() // #CMV_FOO2 499 requires(IsInt<decltype(t)>) // #CMV_FOO2_REQ 500 {} 501 }; 502 503 void test_dependent() { 504 int v = 0; 505 float will_fail; 506 SingleDepthReferencesTop(v); 507 SingleDepthReferencesTop(will_fail); 508 // expected-error@#SDRT_CALL{{no matching function for call to object of type 'lc'}} 509 // expected-note@-2{{in instantiation of function template specialization}} 510 // expected-note@#SDRT_OP{{candidate function not viable}} 511 // expected-note@#SDRT_REQ{{'IsInt<decltype(u)>' evaluated to false}} 512 513 SingleDepthReferencesTopNotCalled(v); 514 // Won't error unless we try to call it. 515 SingleDepthReferencesTopNotCalled(will_fail); 516 SingleDepthReferencesTopCalled(v); // #SDRTC 517 SingleDepthReferencesTopLambda(v); 518 // FIXME: This should error on constraint failure! (Lambda!) 519 SingleDepthReferencesTopLambda(will_fail); 520 DoubleDepthReferencesTop(v); 521 DoubleDepthReferencesTop(will_fail); 522 // expected-error@#DDRT_CALL{{no matching function for call to object of type 'lc2'}} 523 // expected-note@-2{{in instantiation of function template specialization}} 524 // expected-note@#DDRT_STRCT{{in instantiation of member function}} 525 // expected-note@#DDRT_OP{{candidate function not viable}} 526 // expected-note@#DDRT_REQ{{'IsInt<decltype(u)>' evaluated to false}} 527 528 DoubleDepthReferencesTopLambda(v); 529 // FIXME: This should error on constraint failure! (Lambda!) 530 DoubleDepthReferencesTopLambda(will_fail); 531 DoubleDepthReferencesAll(v); 532 DoubleDepthReferencesAll(will_fail); 533 // expected-error@#DDRA_CALL{{no matching function for call to object of type 'lc2'}} 534 // expected-note@-2{{in instantiation of function template specialization}} 535 // expected-note@#DDRA_STRCT{{in instantiation of member function}} 536 // expected-note@#DDRA_OP{{candidate function not viable}} 537 // expected-note@#DDRA_REQ{{'IsInt<decltype(u)>' evaluated to false}} 538 539 DoubleDepthReferencesAllLambda(v); 540 // FIXME: This should error on constraint failure! (Lambda!) 541 DoubleDepthReferencesAllLambda(will_fail); 542 543 CausesFriendConstraint<int> CFC; 544 FriendFunc(CFC, 1); 545 FriendFunc(CFC, 1.0); 546 // expected-error@-1{{no matching function for call to 'FriendFunc'}} 547 // expected-note@#FF_DECL{{constraints not satisfied}} 548 // expected-note@#FF_REQ{{because 'IsInt<double>' evaluated to false}} 549 550 // FIXME: Re-enable this test when constraints are allowed to refer to captures. 551 // ChecksCapture(v); 552 553 ChecksLocalVar(v); 554 // FIXME: This should error on constraint failure! (Lambda!) 555 ChecksLocalVar(will_fail); 556 557 LocalStructMemberVar(v); 558 LocalStructMemberVar(will_fail); 559 // expected-error@#LSMV_CALL{{invalid reference to function 'foo'}} 560 // expected-note@-2{{in instantiation of function template specialization}} 561 // expected-note@#LSMV_REQ{{because 'IsInt<decltype(this->local)>' evaluated to false}} 562 563 ChecksMemberVar<int> CMV; 564 CMV.foo(); 565 CMV.foo2<int>(); 566 567 ChecksMemberVar<float> CMV2; 568 CMV2.foo(); 569 // expected-error@-1{{invalid reference to function 'foo'}} 570 // expected-note@#CMV_FOO{{because 'IsInt<decltype(this->t)>' evaluated to false}} 571 CMV2.foo2<float>(); 572 // expected-error@-1{{no matching member function for call to 'foo2'}} 573 // expected-note@#CMV_FOO2{{constraints not satisfied}} 574 // expected-note@#CMV_FOO2_REQ{{because 'IsInt<decltype(this->t)>' evaluated to false}} 575 } 576 } // namespace DeferredInstantiationInstScope 577