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