[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Parser / cxx2b-lambdas.cpp
blob758ec9a42f56d58e309226e5084d49d9a20dbf59
1 // RUN: %clang_cc1 -std=c++03 %s -verify -Wno-c++23-extensions -Wno-c++20-extensions -Wno-c++17-extensions -Wno-c++14-extensions -Wno-c++11-extensions
2 // RUN: %clang_cc1 -std=c++11 %s -verify=expected,cxx11 -Wno-c++23-extensions -Wno-c++20-extensions -Wno-c++17-extensions -Wno-c++14-extensions
3 // RUN: %clang_cc1 -std=c++14 %s -verify -Wno-c++23-extensions -Wno-c++20-extensions -Wno-c++17-extensions
4 // RUN: %clang_cc1 -std=c++17 %s -verify -Wno-c++23-extensions -Wno-c++20-extensions
5 // RUN: %clang_cc1 -std=c++20 %s -verify -Wno-c++23-extensions
6 // RUN: %clang_cc1 -std=c++23 %s -verify
8 auto LL0 = [] {};
9 auto LL1 = []() {};
10 auto LL2 = []() mutable {};
11 #if __cplusplus >= 201103L
12 auto LL3 = []() constexpr {}; // cxx11-error {{return type 'void' is not a literal type}}
13 #endif
15 #if __cplusplus >= 201103L
16 auto L0 = [] constexpr {}; // cxx11-error {{return type 'void' is not a literal type}}
17 #endif
18 auto L1 = [] mutable {};
19 #if __cplusplus >= 201103L
20 auto L2 = [] noexcept {};
21 auto L3 = [] constexpr mutable {}; // cxx11-error {{return type 'void' is not a literal type}}
22 auto L4 = [] mutable constexpr {}; // cxx11-error {{return type 'void' is not a literal type}}
23 auto L5 = [] constexpr mutable noexcept {}; // cxx11-error {{return type 'void' is not a literal type}}
24 #endif
25 auto L6 = [s = 1] mutable {};
26 #if __cplusplus >= 201103L
27 auto L7 = [s = 1] constexpr mutable noexcept {}; // cxx11-error {{return type 'void' is not a literal type}}
28 #endif
29 auto L8 = [] -> bool { return true; };
30 auto L9 = []<typename T> { return true; };
31 #if __cplusplus >= 201103L
32 auto L10 = []<typename T> noexcept { return true; };
33 #endif
34 auto L11 = []<typename T> -> bool { return true; };
35 #if __cplusplus >= 202002L
36 auto L12 = [] consteval {};
37 auto L13 = []() requires true {}; // expected-error{{non-templated function cannot have a requires clause}}
38 auto L14 = []<auto> requires true() requires true {};
39 auto L15 = []<auto> requires true noexcept {};
40 #endif
41 auto L16 = [] [[maybe_unused]]{};
43 #if __cplusplus >= 201103L
44 auto XL0 = [] mutable constexpr mutable {}; // expected-error{{cannot appear multiple times}} cxx11-error {{return type 'void' is not a literal type}}
45 auto XL1 = [] constexpr mutable constexpr {}; // expected-error{{cannot appear multiple times}} cxx11-error {{return type 'void' is not a literal type}}
46 auto XL2 = []) constexpr mutable constexpr {}; // expected-error{{expected body of lambda expression}}
47 auto XL3 = []( constexpr mutable constexpr {}; // expected-error{{invalid storage class specifier}} \
48 // expected-error{{function parameter cannot be constexpr}} \
49 // expected-error{{a type specifier is required}} \
50 // expected-error{{expected ')'}} \
51 // expected-note{{to match this '('}} \
52 // expected-error{{expected body}} \
53 // expected-warning{{duplicate 'constexpr'}}
54 #endif
56 // http://llvm.org/PR49736
57 auto XL4 = [] requires true {}; // expected-error{{expected body}}
58 #if __cplusplus >= 201703L
59 auto XL5 = []<auto> requires true requires true {}; // expected-error{{expected body}}
60 auto XL6 = []<auto> requires true noexcept requires true {}; // expected-error{{expected body}}
61 #endif
63 auto XL7 = []() static static {}; // expected-error {{cannot appear multiple times}}
64 auto XL8 = []() static mutable {}; // expected-error {{cannot be both mutable and static}}
65 #if __cplusplus >= 202002L
66 auto XL9 = []() static consteval {};
67 #endif
68 #if __cplusplus >= 201103L
69 auto XL10 = []() static constexpr {}; // cxx11-error {{return type 'void' is not a literal type}}
70 #endif
72 auto XL11 = [] static {};
73 auto XL12 = []() static {};
74 auto XL13 = []() static extern {}; // expected-error {{expected body of lambda expression}}
75 auto XL14 = []() extern {}; // expected-error {{expected body of lambda expression}}
78 void static_captures() {
79 int x;
80 auto SC1 = [&]() static {}; // expected-error {{a static lambda cannot have any captures}}
81 auto SC4 = [x]() static {}; // expected-error {{a static lambda cannot have any captures}}
82 auto SC2 = [&x]() static {}; // expected-error {{a static lambda cannot have any captures}}
83 auto SC3 = [y=x]() static {}; // expected-error {{a static lambda cannot have any captures}}
84 auto SC5 = [&y = x]() static {}; // expected-error {{a static lambda cannot have any captures}}
85 auto SC6 = [=]() static {}; // expected-error {{a static lambda cannot have any captures}}
86 struct X {
87 int z;
88 void f() {
89 [this]() static {}(); // expected-error {{a static lambda cannot have any captures}}
90 [*this]() static {}(); // expected-error {{a static lambda cannot have any captures}}
95 #if __cplusplus >= 201703L
96 constexpr auto static_capture_constexpr() {
97 char n = 'n';
98 return [n] static { return n; }(); // expected-error {{a static lambda cannot have any captures}}
100 static_assert(static_capture_constexpr()); // expected-error {{static assertion expression is not an integral constant expression}}
102 constexpr auto capture_constexpr() {
103 char n = 'n';
104 return [n] { return n; }();
106 static_assert(capture_constexpr());
107 #endif