Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / cxx1z-lambda-star-this.cpp
blob0cee41ff5ed3841eb34228d0618b589b439a8a2b
1 // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -emit-llvm-only %s
2 // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s -DDELAYED_TEMPLATE_PARSING
3 // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -fms-extensions %s -DMS_EXTENSIONS
4 // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -fdelayed-template-parsing -fms-extensions %s -DMS_EXTENSIONS -DDELAYED_TEMPLATE_PARSING
6 template <class, class>
7 constexpr bool is_same = false;
8 template <class T>
9 constexpr bool is_same<T, T> = true;
11 namespace test_star_this {
12 namespace ns1 {
13 class A {
14 int x = 345;
15 auto foo() {
16 (void)[ *this, this ]{}; //expected-error{{'this' can appear only once}}
17 (void)[this] { ++x; };
18 (void)[*this] { ++x; }; //expected-error{{read-only variable}}
19 (void)[*this]() mutable { ++x; };
20 (void)[=] { return x; };
21 (void)[&, this ] { return x; };
22 (void)[ =, *this ] { return x; };
23 (void)[&, *this ] { return x; };
26 } // namespace ns1
28 namespace ns2 {
29 class B {
30 B(const B &) = delete; //expected-note{{deleted here}}
31 int *x = (int *)456;
32 void foo() {
33 (void)[this] { return x; };
34 (void)[*this] { return x; }; //expected-error{{call to deleted}}
37 } // namespace ns2
39 namespace ns3 {
40 class B {
41 B(const B &) = delete; //expected-note2{{deleted here}}
43 int *x = (int *)456;
45 public:
46 template <class T = int>
47 void foo() {
48 (void)[this] { return x; };
49 (void)[*this] { return x; }; //expected-error2{{call to deleted}} expected-note {{while substituting into a lambda}}
52 B() = default;
53 } b;
54 B *c = (b.foo(), nullptr); //expected-note{{in instantiation}}
55 } // namespace ns3
57 namespace ns4 {
58 template <class U>
59 class B {
60 B(const B &) = delete; //expected-note{{deleted here}}
61 double d = 3.14;
63 public:
64 template <class T = int>
65 auto foo() {
66 const auto &L = [*this](auto a) mutable { //expected-error{{call to deleted}} expected-note {{while substituting into a lambda}}
67 d += a;
68 return [this](auto b) { return d += b; };
72 B() = default;
74 void main() {
75 B<int *> b;
76 b.foo(); //expected-note{{in instantiation}}
77 } // end main
78 } // namespace ns4
80 namespace ns5 {
82 struct X {
83 double d = 3.14;
84 X(const volatile X &);
85 void foo() {
88 void foo() const { //expected-note{{const}}
90 auto L = [*this]() mutable {
91 static_assert(is_same<decltype(this), const X *>);
92 auto M = [this] {
93 static_assert(is_same<decltype(this), const X *>);
94 auto N = [] {
95 static_assert(is_same<decltype(this), const X *>);
100 auto L1 = [*this] {
101 static_assert(is_same<decltype(this), const X *>);
102 auto M = [this]() mutable {
103 static_assert(is_same<decltype(this), const X *>);
104 auto N = [] {
105 static_assert(is_same<decltype(this), const X *>);
108 auto M2 = [*this]() mutable {
109 static_assert(is_same<decltype(this), const X *>);
110 auto N = [] {
111 static_assert(is_same<decltype(this), const X *>);
116 auto GL1 = [*this](auto a) {
117 static_assert(is_same<decltype(this), const X *>);
118 auto M = [this](auto b) mutable {
119 static_assert(is_same<decltype(this), const X *>);
120 auto N = [](auto c) {
121 static_assert(is_same<decltype(this), const X *>);
123 return N;
126 auto M2 = [*this](auto a) mutable {
127 static_assert(is_same<decltype(this), const X *>);
128 auto N = [](auto b) {
129 static_assert(is_same<decltype(this), const X *>);
131 return N;
133 return [=](auto a) mutable { M(a)(a); M2(a)(a); };
136 GL1("abc")
137 ("abc");
139 auto L2 = [this]() mutable {
140 static_assert(is_same<decltype(this), const X *>);
141 ++d; //expected-error{{cannot assign}}
143 auto GL = [*this](auto a) mutable {
144 static_assert(is_same<decltype(this), const X *>);
145 auto M = [this](auto b) {
146 static_assert(is_same<decltype(this), const X *>);
147 auto N = [](auto c) {
148 static_assert(is_same<decltype(this), const X *>);
150 N(3.14);
152 M("abc");
154 GL(3.14);
156 void foo() volatile const {
157 auto L = [this]() {
158 static_assert(is_same<decltype(this), const volatile X *>);
159 auto M = [*this]() mutable {
160 static_assert(is_same<decltype(this), const volatile X *>);
161 auto N = [this] {
162 static_assert(is_same<decltype(this), const volatile X *>);
163 auto M = [] {
164 static_assert(is_same<decltype(this), const volatile X *>);
167 auto N2 = [*this] {
168 static_assert(is_same<decltype(this), const volatile X *>);
171 auto M2 = [*this]() {
172 static_assert(is_same<decltype(this), const volatile X *>);
173 auto N = [this] {
174 static_assert(is_same<decltype(this), const volatile X *>);
181 } // namespace ns5
182 namespace ns6 {
183 struct X {
184 double d;
185 auto foo() const {
186 auto L = [*this]() mutable {
187 auto M = [=](auto a) {
188 auto N = [this] {
189 static_assert(is_same<decltype(this), const X *>);
190 auto O = [*this] {
191 static_assert(is_same<decltype(this), const X *>);
194 N();
195 static_assert(is_same<decltype(this), const X *>);
197 return M;
199 return L;
203 int main() {
204 auto L = X{}.foo();
205 auto M = L();
206 M(3.14);
208 } // namespace ns6
209 namespace ns7 {
211 struct X {
212 double d;
213 X();
214 X(const X &);
215 X(X &) = delete;
216 auto foo() const {
217 //OK - the object used to initialize our capture is a const object and so prefers the non-deleted ctor.
218 const auto &&L = [*this]{};
221 int main() {
222 X x;
223 x.foo();
225 } // namespace ns7
227 } // namespace test_star_this
229 namespace PR32831 {
230 // https://bugs.llvm.org/show_bug.cgi?id=32831
231 namespace ns1 {
232 template <typename Func>
233 void fun_template(Func func) {
234 (void)[&]() {
235 func(0);
239 class A {
240 void member_foo() {
241 (void)[this] {
242 (void)[this] {
243 fun_template(
244 [this](auto X) {
245 auto L = [this](auto Y) { member_foo(); };
246 L(5);
248 fun_template(
249 [this](auto) { member_foo(); });
254 } // namespace ns1
256 namespace ns2 {
258 struct B {
259 int data = 0;
260 template <class F>
261 void mem2(F f) {
262 (void)[&](auto f) {
263 (void)[&] { f(this->data); };
265 (f);
269 class A {
270 void member_foo() {
271 (void)[this] {
272 (void)[this] {
273 B{}.mem2(
274 [this](auto X) {
275 auto L = [this](auto Y) { member_foo(); };
276 L(5);
278 B{}.mem2(
279 [this](auto) { member_foo(); });
283 int data = 0;
284 auto m2() {
285 return [this] { return [] () -> decltype(data){ return 0; }; };
287 auto m3() {
288 return [] { return [] () -> decltype(data){ return 0; }; };
292 } // namespace ns2
294 } // namespace PR32831
296 namespace PR45881 {
297 struct A {
298 void f();
300 int id(A*);
301 void A::f() {
302 auto z = [*this](auto z2, decltype(z2(this)) z3){};
303 z(id,3);
305 } // namespace PR45881
308 namespace GH50866 {
309 struct S;
311 void f(S *) = delete; // expected-note {{would lose const qualifier}}
312 void f(const S *) = delete; // expected-note {{candidate function has been explicitly deleted}}
314 struct S {
315 void g() const {
316 [*this]() mutable { f(this); }(); // expected-error {{call to deleted function}}
320 void g() {
321 S s{};
322 s.g();