brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.3 KiB · 9289fc9 Raw
285 lines · cpp
1// RUN: %clang_cc1 -std=c++20 %s -Wno-c++23-extensions -verify2// RUN: %clang_cc1 -std=c++23 %s -verify3 4 5template <auto> struct Nothing {};6Nothing<[]() { return 0; }()> nothing;7 8template <typename> struct NothingT {};9Nothing<[]() { return 0; }> nothingT;10 11template <typename T>12concept True = [] { return true; }();13static_assert(True<int>);14 15static_assert(sizeof([] { return 0; }));16static_assert(sizeof([] { return 0; }()));17 18void f()  noexcept(noexcept([] { return 0; }()));19 20using a = decltype([] { return 0; });21using b = decltype([] { return 0; }());22using c = decltype([]() noexcept(noexcept([] { return 0; }())) { return 0; });23using d = decltype(sizeof([] { return 0; }));24 25template <auto T>26int unique_test1();27static_assert(&unique_test1<[](){}> != &unique_test1<[](){}>);28 29template <class T>30auto g(T) -> decltype([]() { T::invalid; } ());31auto e = g(0); // expected-error@-1{{type 'int' cannot be used prior to '::'}}32               // expected-note@-1{{while substituting deduced template}}33               // expected-note@-3{{while substituting into a lambda}}34               // expected-error@-3 {{no matching function for call to 'g'}}35               // expected-note@-5 {{substitution failure}}36 37template <typename T>38auto foo(decltype([] {39  return [] { return T(); }();40})) {}41 42void test() {43  foo<int>({});44}45 46template <typename T>47struct C {48  template <typename U>49  auto foo(decltype([] {50    return [] { return T(); }();51  })) {}52};53 54void test2() {55  C<int>{}.foo<long>({});56}57 58namespace PR52073 {59// OK, these are distinct functions not redefinitions.60template<typename> void f(decltype([]{})) {} // expected-note {{candidate}}61template<typename> void f(decltype([]{})) {} // expected-note {{candidate}}62void use_f() { f<int>({}); } // expected-error {{ambiguous}}63 64// Same.65template<int N> void g(const char (*)[([]{ return N; })()]) {} // expected-note {{candidate}}66template<int N> void g(const char (*)[([]{ return N; })()]) {} // expected-note {{candidate}}67void use_g() { g<6>(&"hello"); } // expected-error {{ambiguous}}68}69 70namespace GH51416 {71 72template <class T>73struct A { // #defined-here-A74  void spam(decltype([] {}));75};76 77template <class T>78void A<T>::spam(decltype([] {})) // expected-error{{out-of-line definition of 'spam' does not match}}79                                 // expected-note@#defined-here-A{{defined here}}80{}81 82struct B { // #defined-here-B83  template <class T>84  void spam(decltype([] {}));85};86 87template <class T>88void B::spam(decltype([] {})) {} // expected-error{{out-of-line definition of 'spam' does not match}}89                                 // expected-note@#defined-here-B{{defined here}}90 91} // namespace GH5141692 93namespace GH50376 {94 95template <typename T, typename Fn>96struct foo_t {    // expected-note 2{{candidate constructor}}97  foo_t(T ptr) {} // expected-note{{candidate constructor}}98};99 100template <typename T>101using alias = foo_t<T, decltype([](int) { return 0; })>;102 103template <typename T>104auto fun(T const &t) -> alias<T> {105  return alias<T>{t}; // expected-error{{no viable conversion from returned value of type 'alias<...>'}}106}107 108void f() {109  int i;110  auto const error = fun(i); // expected-note{{in instantiation}}111}112 113} // namespace GH50376114 115namespace GH51414 {116template <class T> void spam(decltype([] {}) (*s)[sizeof(T)] = nullptr) {}117void foo() {118  spam<int>();119}120} // namespace GH51414121 122namespace GH51641 {123template <class T>124void foo(decltype(+[](T) {}) lambda, T param);125static_assert(!__is_same(decltype(foo<int>), void));126} // namespace GH51641127 128namespace StaticLambdas {129template <auto> struct Nothing {};130Nothing<[]() static { return 0; }()> nothing;131 132template <typename> struct NothingT {};133Nothing<[]() static { return 0; }> nothingT;134 135template <typename T>136concept True = [] static { return true; }();137static_assert(True<int>);138 139static_assert(sizeof([] static { return 0; }));140static_assert(sizeof([] static { return 0; }()));141 142void f()  noexcept(noexcept([] static { return 0; }()));143 144using a = decltype([] static { return 0; });145using b = decltype([] static { return 0; }());146using c = decltype([]() static noexcept(noexcept([] { return 0; }())) { return 0; });147using d = decltype(sizeof([] static { return 0; }));148 149}150 151namespace lambda_in_trailing_decltype {152auto x = ([](auto) -> decltype([] {}()) {}(0), 2);153}154 155namespace lambda_in_constraints {156struct WithFoo { static void foo(); };157 158template <class T>159concept lambda_works = requires {160    []() { T::foo(); }; // expected-error{{type 'int' cannot be used prior to '::'}}161                        // expected-note@-1{{while substituting into a lambda expression here}}162                        // expected-note@-2{{in instantiation of requirement here}}163                        // expected-note@-4{{while substituting template arguments into constraint expression here}}164};165 166static_assert(!lambda_works<int>); // expected-note {{while checking the satisfaction of concept 'lambda_works<int>' requested here}}167static_assert(lambda_works<WithFoo>);168 169template <class T>170int* func(T) requires requires { []() { T::foo(); }; }; // expected-error{{type 'int' cannot be used prior to '::'}}171                                                        // expected-note@-1{{while substituting into a lambda expression here}}172                                                        // expected-note@-2{{in instantiation of requirement here}}173                                                        // expected-note@-3{{while substituting template arguments into constraint expression here}}174double* func(...);175 176static_assert(__is_same(decltype(func(0)), double*)); // expected-note {{while checking constraint satisfaction for template 'func<int>' required here}}177                                                      // expected-note@-1 {{while substituting deduced template arguments into function template 'func' [with T = int]}}178static_assert(__is_same(decltype(func(WithFoo())), int*));179 180template <class T>181auto direct_lambda(T) -> decltype([] { T::foo(); }) {}182void direct_lambda(...) {}183 184void recursive() {185    direct_lambda(0); // expected-error@-4 {{type 'int' cannot be used prior to '::'}}186                      // expected-note@-1 {{while substituting deduced template arguments}}187                      // expected-note@-6 {{while substituting into a lambda}}188    bool x = requires { direct_lambda(0); }; // expected-error@-7 {{type 'int' cannot be used prior to '::'}}189                                             // expected-note@-1 {{while substituting deduced template arguments}}190                                             // expected-note@-9 {{while substituting into a lambda}}191 192}193}194 195// GH63845: Test if we have skipped past RequiresExprBodyDecls in tryCaptureVariable().196namespace GH63845 {197 198template <bool> struct A {};199 200struct true_type {201  constexpr operator bool() noexcept { return true; }202};203 204constexpr bool foo() {205  true_type x{};206  return requires { typename A<x>; };207}208 209static_assert(foo());210 211} // namespace GH63845212 213// GH69307: Test if we can correctly handle param decls that have yet to get into the function scope.214namespace GH69307 {215 216constexpr auto ICE() {217  constexpr auto b = 1;218  return [=](auto c) -> int219           requires requires { b + c; }220  { return 1; };221};222 223constexpr auto Ret = ICE()(1);224 225} // namespace GH69307226 227// GH88081: Test if we evaluate the requires expression with lambda captures properly.228namespace GH88081 {229 230// Test that ActOnLambdaClosureQualifiers() is called only once.231void foo(auto value)232  requires requires { [&] -> decltype(value) {}; }233  // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}234{}235 236struct S { //#S237  S(auto value) //#S-ctor238  requires requires { [&] -> decltype(value) { return 2; }; } {} // #S-requires239 240  static auto foo(auto value) -> decltype([&]() -> decltype(value) {}()) { return {}; } // #S-foo241 242  // FIXME: 'value' does not constitute an ODR use here. Add a diagnostic for it.243  static auto bar(auto value) -> decltype([&] { return value; }()) {244    return "a"; // #bar-body245  }246};247 248S s("a"); // #use249// expected-error@#S-requires {{cannot initialize return object of type 'decltype(value)' (aka 'const char *') with an rvalue of type 'int'}}250// expected-error@#use {{no matching constructor}}251// expected-note@#S-requires {{substituting into a lambda expression here}}252// expected-note@#S-requires {{substituting template arguments into constraint expression here}}253// expected-note@#S-requires {{in instantiation of requirement here}}254// expected-note@#use {{checking constraint satisfaction for template 'S<const char *>' required here}}255// expected-note@#use {{while substituting deduced template arguments into function template 'S' [with value:auto = const char *]}}256// expected-note-re@#S 2{{candidate constructor {{.*}} not viable}}257// expected-note@#S-ctor {{constraints not satisfied}}258// expected-note-re@#S-requires {{because {{.*}} would be invalid}}259 260void func() {261  S::foo(42);262  S::bar("str");263  S::bar(0.618);264  // expected-error-re@#bar-body {{cannot initialize return object of type {{.*}} (aka 'double') with an lvalue of type 'const char[2]'}}265  // expected-note@-2 {{requested here}}266}267 268} // namespace GH88081269 270namespace GH138018 {271 272template <typename T> struct vec {};273 274auto structure_to_typelist(auto)  {275    return []<template <typename> typename T>(T<int>) {276        return 0;277    }(vec<int>{});278}279 280template <typename T> using helper = decltype(structure_to_typelist(T{}));281static_assert(__is_same_as(int, helper<int>));282 283 284} // namespace GH138018285