Debian Codegraph @

clang/test/SemaCXX/cxx2b-deducing-this.cpp

raw
1 // RUN: %clang_cc1 -fsyntax-only -std=c++2b -Woverloaded-virtual %s -verify
2
3
4 // FIXME: can we improve these diagnostics?
5 void f(this); // expected-error{{variable has incomplete type 'void'}} \
6 // expected-error{{invalid use of 'this' outside of a non-static member function}}
7
8 void g(this auto); // expected-error{{an explicit object parameter cannot appear in a non-member function}}
9
10 auto l1 = [] (this auto) static {}; // expected-error{{an explicit object parameter cannot appear in a static lambda}}
11 auto l2 = [] (this auto) mutable {}; // expected-error{{a lambda with an explicit object parameter cannot be mutable}}
12 auto l3 = [](this auto...){}; // expected-error {{the explicit object parameter cannot be a function parameter pack}}
13 auto l4 = [](int, this auto){}; // expected-error {{an explicit object parameter can only appear as the first parameter of the lambda}}
14
15 struct S {
16 static void f(this auto); // expected-error{{an explicit object parameter cannot appear in a static function}}
17 virtual void f(this S); // expected-error{{an explicit object parameter cannot appear in a virtual function}}
18
19 // new and delete are implicitly static
20 void *operator new(this unsigned long); // expected-error{{an explicit object parameter cannot appear in a static function}}
21 void operator delete(this void*); // expected-error{{an explicit object parameter cannot appear in a static function}}
22
23 void g(this auto) const; // expected-error{{explicit object member function cannot have 'const' qualifier}}
24 void h(this auto) &; // expected-error{{explicit object member function cannot have '&' qualifier}}
25 void i(this auto) &&; // expected-error{{explicit object member function cannot have '&&' qualifier}}
26 void j(this auto) volatile; // expected-error{{explicit object member function cannot have 'volatile' qualifier}}
27 void k(this auto) __restrict; // expected-error{{explicit object member function cannot have '__restrict' qualifier}}
28 void l(this auto) _Nonnull; // expected-error{{explicit object member function cannot have '' qualifie}}
29
30
31 void variadic(this auto...); // expected-error{{the explicit object parameter cannot be a function parameter pack}}
32 void not_first(int, this auto); // expected-error {{an explicit object parameter can only appear as the first parameter of the function}}
33
34 S(this auto); // expected-error {{an explicit object parameter cannot appear in a constructor}}
35 ~S(this S) {} // expected-error {{an explicit object parameter cannot appear in a destructor}} \
36 // expected-error {{destructor cannot have any parameters}}
37 };
38
39 namespace Override {
40 struct A {
41 virtual void f(); // expected-note 2{{here}}
42 virtual void g(int); // expected-note {{here}}
43 virtual void h() const; // expected-note 5{{here}}
44 };
45
46 // CWG2553
47 struct B : A {
48 int f(this B&, int); // expected-warning {{hides overloaded virtual function}}
49 int f(this B&); // expected-error {{an explicit object parameter cannot appear in a virtual function}}
50 int g(this B&); // expected-warning {{hides overloaded virtual function}}
51 int h(this B&); // expected-error {{an explicit object parameter cannot appear in a virtual function}}
52 int h(this B&&); // expected-error {{an explicit object parameter cannot appear in a virtual function}}
53 int h(this const B&&); // expected-error {{an explicit object parameter cannot appear in a virtual function}}
54 int h(this A&); // expected-error {{an explicit object parameter cannot appear in a virtual function}}
55 int h(this int); // expected-error {{an explicit object parameter cannot appear in a virtual function}}
56 };
57 }
58
59 namespace DefaultArgs {
60 struct Test { void f(this const auto& = Test{}); };
61 // expected-error@-1 {{the explicit object parameter cannot have a default argument}}
62 auto L = [](this const auto& = Test{}){};
63 // expected-error@-1 {{the explicit object parameter cannot have a default argument}}
64 }
65
66 struct CannotUseThis {
67 int fun();
68 int m;
69 void f(this auto) {
70 this->fun(); // expected-error{{invalid use of 'this' in a function with an explicit object parameter}}
71 fun(); // expected-error {{call to non-static member function without an object argument}}
72 m = 0; // expected-error {{invalid use of member 'm' in explicit object member function}}
73 }
74 };
75
76 struct CannotUseThisBase {
77 void foo();
78 int n;
79 static int i;
80 };
81
82 struct CannotUseThisDerived : CannotUseThisBase {
83 void bar(this auto) {
84 foo(); // expected-error {{call to non-static member function without an object argument}}
85 n = 12; // expected-error {{invalid use of member 'n' in explicit object member function}}
86 i = 100;
87 }
88 };
89
90 namespace ThisInLambdaWithCaptures {
91
92 struct Test {
93 Test(auto&&);
94 };
95
96 void test() {
97
98 [i = 0](this Test) { }();
99 // expected-error@-1 {{invalid explicit object parameter type 'ThisInLambdaWithCaptures::Test' in lambda with capture; the type must be the same as, or derived from, the lambda}}
100
101 struct Derived;
102 auto ok = [i = 0](this const Derived&) {};
103 auto ko = [i = 0](this const Test&) {};
104 // expected-error@-1 {{invalid explicit object parameter type 'ThisInLambdaWithCaptures::Test' in lambda with capture; the type must be the same as, or derived from, the lambda}}
105
106 struct Derived : decltype(ok){};
107 Derived dok{ok};
108 dok();
109
110 struct DerivedErr : decltype(ko){};
111 DerivedErr dko{ko};
112 dko();
113
114 auto alsoOk = [](this const Test &) {};
115 alsoOk();
116 }
117
118 struct Frobble;
119 auto nothingIsOkay = [i = 0](this const Frobble &) {}; // expected-note {{candidate function not viable: requires 0 non-object arguments, but 1 was provided}}
120 struct Frobble {} f;
121 void test2() {
122 nothingIsOkay(f); // expected-error {{no matching function for call to object of type}}
123 }
124
125 }
126
127 struct Corresponding {
128 void a(this Corresponding&); // expected-note 2{{here}}
129 void a(); // expected-error{{cannot be redeclared}}
130 void a() &; // expected-error{{cannot be redeclared}}
131 void a(this Corresponding&, int);
132 void a(this Corresponding&, double);
133
134 void b(this const Corresponding&); // expected-note 2{{here}}
135 void b() const; // expected-error{{cannot be redeclared}}
136 void b() const &; // expected-error{{cannot be redeclared}}
137
138 void c(this Corresponding&&); // expected-note {{here}}
139 void c() &&; // expected-error{{cannot be redeclared}}
140
141 void d(this Corresponding&);
142 void d(this Corresponding&&);
143 void d(this const Corresponding&);
144 void d(this const int&);
145 void d(this const int);
146 void d(this int);
147
148 void e(this const Corresponding&&); // expected-note {{here}}
149 void e() const &&; // expected-error{{cannot be redeclared}}
150
151 };
152
153 template <typename T>
154 struct CorrespondingTpl {
155 void a(this CorrespondingTpl&); // expected-note 2{{here}}
156 void a(); // expected-error{{cannot be redeclared}}
157 void a() &; // expected-error{{cannot be redeclared}}
158 void a(this Corresponding&, int);
159 void a(this Corresponding&, double);
160 void a(long);
161
162
163 void b(this const CorrespondingTpl&); // expected-note 2{{here}}
164 void b() const; // expected-error{{cannot be redeclared}}
165 void b() const &; // expected-error{{cannot be redeclared}}
166
167 void c(this CorrespondingTpl&&); // expected-note {{here}}
168 void c() &&; // expected-error{{cannot be redeclared}}
169
170 void d(this Corresponding&);
171 void d(this Corresponding&&);
172 void d(this const Corresponding&);
173 void d(this const int&);
174 void d(this const int);
175 void d(this int);
176
177 void e(this const CorrespondingTpl&&); // expected-note {{here}}
178 void e() const &&; // expected-error{{cannot be redeclared}}
179 };
180
181 struct C {
182 template <typename T>
183 C(T){}
184 };
185
186 void func(int i) {
187 (void)[=](this auto&&) { return i; }();
188 (void)[=](this const auto&) { return i; }();
189 (void)[i](this C) { return i; }(); // expected-error{{invalid explicit object parameter type 'C'}}
190 (void)[=](this C) { return i; }(); // expected-error{{invalid explicit object parameter type 'C'}}
191 (void)[](this C) { return 42; }();
192 auto l = [=](this auto&) {};
193 struct D : decltype(l) {};
194 D d{l};
195 d();
196 }
197
198 void TestMutationInLambda() {
199 [i = 0](this auto &&){ i++; }();
200 [i = 0](this auto){ i++; }();
201 [i = 0](this const auto&){ i++; }(); // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
202
203 int x;
204 const auto l1 = [x](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
205 const auto l2 = [=](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
206
207 const auto l3 = [&x](this auto&) {
208 const auto l3a = [x](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
209 l3a(); // expected-note {{in instantiation of}}
210 };
211
212 const auto l4 = [&x](this auto&) {
213 const auto l4a = [=](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
214 l4a(); // expected-note {{in instantiation of}}
215 };
216
217 const auto l5 = [x](this auto&) {
218 const auto l5a = [x](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
219 l5a(); // expected-note {{in instantiation of}}
220 };
221
222 const auto l6 = [=](this auto&) {
223 const auto l6a = [=](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
224 l6a(); // expected-note {{in instantiation of}}
225 };
226
227 const auto l7 = [x](this auto&) {
228 const auto l7a = [=](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
229 l7a(); // expected-note {{in instantiation of}}
230 };
231
232 const auto l8 = [=](this auto&) {
233 const auto l8a = [x](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
234 l8a(); // expected-note {{in instantiation of}}
235 };
236
237 const auto l9 = [&](this auto&) {
238 const auto l9a = [x](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
239 l9a(); // expected-note {{in instantiation of}}
240 };
241
242 const auto l10 = [&](this auto&) {
243 const auto l10a = [=](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
244 l10a(); // expected-note {{in instantiation of}}
245 };
246
247 const auto l11 = [x](this auto&) {
248 const auto l11a = [&x](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}} expected-note {{while substituting}}
249 l11a();
250 };
251
252 const auto l12 = [x](this auto&) {
253 const auto l12a = [&](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}} expected-note {{while substituting}}
254 l12a();
255 };
256
257 const auto l13 = [=](this auto&) {
258 const auto l13a = [&x](this auto&) { x = 42; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}} expected-note {{while substituting}}
259 l13a();
260 };
261
262 struct S {
263 int x;
264 auto f() {
265 return [*this] (this auto&&) {
266 x = 42; // expected-error {{read-only variable is not assignable}}
267 [*this] () mutable { x = 42; } ();
268 [*this] (this auto&&) { x = 42; } ();
269 [*this] () { x = 42; } (); // expected-error {{read-only variable is not assignable}}
270 const auto l = [*this] (this auto&&) { x = 42; }; // expected-error {{read-only variable is not assignable}}
271 l(); // expected-note {{in instantiation of}}
272
273 struct T {
274 int x;
275 auto g() {
276 return [&] (this auto&&) {
277 x = 42;
278 const auto l = [*this] (this auto&&) { x = 42; }; // expected-error {{read-only variable is not assignable}}
279 l(); // expected-note {{in instantiation of}}
280 };
281 }
282 };
283
284 const auto l2 = T{}.g();
285 l2(); // expected-note {{in instantiation of}}
286 };
287 }
288 };
289
290 const auto l14 = S{}.f();
291
292 l1(); // expected-note {{in instantiation of}}
293 l2(); // expected-note {{in instantiation of}}
294 l3(); // expected-note {{in instantiation of}}
295 l4(); // expected-note {{in instantiation of}}
296 l5(); // expected-note {{in instantiation of}}
297 l6(); // expected-note {{in instantiation of}}
298 l7(); // expected-note {{in instantiation of}}
299 l8(); // expected-note {{in instantiation of}}
300 l9(); // expected-note {{in instantiation of}}
301 l10(); // expected-note {{in instantiation of}}
302 l11(); // expected-note {{in instantiation of}}
303 l12(); // expected-note {{in instantiation of}}
304 l13(); // expected-note {{in instantiation of}}
305 l14(); // expected-note 3 {{in instantiation of}}
306
307 {
308 const auto l1 = [&x](this auto&) { x = 42; };
309 const auto l2 = [&](this auto&) { x = 42; };
310 l1();
311 l2();
312 }
313
314 // Check that we don't crash if the lambda has type sugar.
315 const auto l15 = [=](this auto&&) [[clang::annotate_type("foo")]] [[clang::annotate_type("bar")]] {
316 return x;
317 };
318
319 const auto l16 = [=]() [[clang::annotate_type("foo")]] [[clang::annotate_type("bar")]] {
320 return x;
321 };
322
323 l15();
324 l16();
325 }
326
327 struct Over_Call_Func_Example {
328 void a();
329 void b() {
330 a(); // ok, (*this).a()
331 }
332
333 void f(this const Over_Call_Func_Example&); // expected-note {{here}}
334 void g() const {
335 f(); // ok: (*this).f()
336 f(*this); // expected-error{{too many non-object arguments to function call}}
337 this->f(); // ok
338 }
339
340 static void h() {
341 f(); // expected-error{{call to non-static member function without an object argument}}
342 f(Over_Call_Func_Example{}); // expected-error{{call to non-static member function without an object argument}}
343 Over_Call_Func_Example{}.f(); // ok
344 }
345
346 void k(this int);
347 operator int() const;
348 void m(this const Over_Call_Func_Example& c) {
349 c.k(); // ok
350 }
351 };
352
353 struct AmbiguousConversion {
354 void f(this int); // expected-note {{candidate function}}
355 void f(this float); // expected-note {{candidate function}}
356
357 operator int() const;
358 operator float() const;
359
360 void test(this const AmbiguousConversion &s) {
361 s.f(); // expected-error {{call to member function 'f' is ambiguous}}
362 }
363 };
364
365 struct IntToShort {
366 void s(this short);
367 operator int() const;
368 void test(this const IntToShort &val) {
369 val.s();
370 }
371 };
372
373 struct ShortToInt {
374 void s(this int);
375 operator short() const;
376 void test(this const ShortToInt &val) {
377 val.s();
378 }
379 };
380
381 namespace arity_diagnostics {
382 struct S {
383 void f(this auto &&, auto, auto); // expected-note {{requires 2 non-object arguments, but 0 were provided}}
384 void g(this auto &&, auto, auto); // expected-note {{requires 2 non-object arguments, but 3 were provided}}
385 void h(this auto &&, int, int i = 0); // expected-note {{requires at least 1 non-object argument, but 0 were provided}}
386 void i(this S&&, int); // expected-note 2{{declared here}}
387 };
388
389 int test() {
390 void(*f)(S&&, int, int) = &S::f;
391 f(S{}, 1, 2);
392 f(S{}, 1); // expected-error {{too few arguments to function call, expected 3, have 2}}
393 f(S{}); // expected-error {{too few arguments to function call, expected 3, have 1}}
394 f(S{}, 1, 2, 3); //expected-error {{too many arguments to function call, expected 3, have 4}}
395
396 S{}.f(1, 2);
397 S{}.f(); // expected-error{{no matching member function for call to 'f'}}
398 S{}.g(1,2,3); // expected-error {{no matching member function for call to 'g'}}
399 S{}.h(); // expected-error {{no matching member function for call to 'h'}}
400 S{}.i(); // expected-error {{too few non-object arguments to function call, expected 1, have 0}}
401 S{}.i(1, 2, 3); // expected-error {{too many non-object arguments to function call, expected 1, have 3}}
402 }
403
404 }
405
406 namespace AddressOf {
407
408 struct s {
409 static void f(int);
410 void f(this auto &&) {}
411 void g(this s &&) {};
412
413 void test_qual() {
414 using F = void(s&&);
415 F* a = &f; // expected-error {{must explicitly qualify name of member function when taking its address}}
416 F* b = &g; // expected-error {{must explicitly qualify name of member function when taking its address}}
417 F* c = &s::f;
418 F* d = &s::g;
419 }
420 };
421
422 void test() {
423 using F = void(s&&);
424 F* a = &s::f;
425 F* b = &s::g;
426 a(s{});
427 b(s{});
428 }
429
430 }
431
432 namespace std {
433 struct strong_ordering {
434 int n;
435 constexpr operator int() const { return n; }
436 static const strong_ordering equal, greater, less;
437 };
438 constexpr strong_ordering strong_ordering::equal = {0};
439 constexpr strong_ordering strong_ordering::greater = {1};
440 constexpr strong_ordering strong_ordering::less = {-1};
441 }
442
443 namespace operators_deduction {
444
445 template <typename T, typename U>
446 constexpr bool is_same = false;
447
448 template <typename T>
449 constexpr bool is_same<T, T> = true;
450
451 template <template <typename> typename T>
452 struct Wrap {
453 void f();
454 struct S {
455 operator int(this auto&& self) {
456 static_assert(is_same<decltype(self), typename T<S>::type>);
457 return 0;
458 }
459 Wrap* operator->(this auto&& self) {
460 static_assert(is_same<decltype(self), typename T<S>::type>);
461 return new Wrap();
462 }
463 int operator[](this auto&& self, int) {
464 static_assert(is_same<decltype(self), typename T<S>::type>);
465 return 0;
466 }
467 int operator()(this auto&& self, int) {
468 static_assert(is_same<decltype(self), typename T<S>::type>);
469 return 0;
470 }
471 int operator++(this auto&& self, int) {
472 static_assert(is_same<decltype(self), typename T<S>::type>);
473 return 0;
474 }
475 int operator++(this auto&& self) {
476 static_assert(is_same<decltype(self), typename T<S>::type>);
477 return 0;
478 }
479 int operator--(this auto&& self, int) {
480 static_assert(is_same<decltype(self), typename T<S>::type>);
481 return 0;
482 }
483 int operator--(this auto&& self) {
484 static_assert(is_same<decltype(self), typename T<S>::type>);
485 return 0;
486 }
487 int operator*(this auto&& self) {
488 static_assert(is_same<decltype(self), typename T<S>::type>);
489 return 0;
490 }
491 bool operator==(this auto&& self, int) {
492 static_assert(is_same<decltype(self), typename T<S>::type>);
493 return false;
494 }
495 bool operator<=>(this auto&& self, int) {
496 static_assert(is_same<decltype(self), typename T<S>::type>);
497 return false;
498 }
499 bool operator<<(this auto&& self, int b) {
500 static_assert(is_same<decltype(self), typename T<S>::type>);
501 return false;
502 }
503 };
504 };
505
506 template <typename T>
507 struct lvalue_reference {
508 using type = T&;
509 };
510 template <typename T>
511 struct const_lvalue_reference {
512 using type = const T&;
513 };
514 template <typename T>
515 struct volatile_lvalue_reference {
516 using type = volatile T&;
517 };
518 template <typename T>
519 struct rvalue_reference {
520 using type = T&&;
521 };
522 template <typename T>
523 struct const_rvalue_reference {
524 using type = const T&&;
525 };
526
527
528 void test() {
529 {
530 Wrap<lvalue_reference>::S s;
531 s++;
532 s.operator++(0);
533 ++s;
534 s.operator++();
535 s--;
536 s.operator--(0);
537 --s;
538 s.operator--();
539 s[0];
540 s.operator[](0);
541 s(0);
542 s.operator()(0);
543 *s;
544 s.operator*();
545 s->f();
546 s.operator->();
547 int i = s;
548 (void)(s << 0);
549 s.operator<<(0);
550 (void)(s == 0);
551 s.operator==(0);
552 (void)(s <=> 0);
553 s.operator<=>(0);
554 }
555 {
556 const Wrap<const_lvalue_reference>::S s;
557 s++;
558 s.operator++(0);
559 ++s;
560 s.operator++();
561 s--;
562 s.operator--(0);
563 --s;
564 s.operator--();
565 s[0];
566 s.operator[](0);
567 s(0);
568 s.operator()(0);
569 *s;
570 s.operator*();
571 s->f();
572 s.operator->();
573 int i = s;
574 (void)(s << 0);
575 s.operator<<(0);
576 (void)(s == 0);
577 s.operator==(0);
578 (void)(s <=> 0);
579 s.operator<=>(0);
580 }
581 {
582 volatile Wrap<volatile_lvalue_reference>::S s;
583 s++;
584 s.operator++(0);
585 ++s;
586 s.operator++();
587 s--;
588 s.operator--(0);
589 --s;
590 s.operator--();
591 s[0];
592 s.operator[](0);
593 s(0);
594 s.operator()(0);
595 *s;
596 s.operator*();
597 s->f();
598 s.operator->();
599 int i = s;
600 (void)(s << 0);
601 s.operator<<(0);
602 (void)(s == 0);
603 s.operator==(0);
604 (void)(s <=> 0);
605 s.operator<=>(0);
606 }
607 {
608 Wrap<rvalue_reference>::S s;
609 using M = Wrap<rvalue_reference>::S&&;
610 ((M)s)++;
611 ((M)s).operator++(0);
612 ++((M)s);
613 ((M)s).operator++();
614 ((M)s)--;
615 ((M)s).operator--(0);
616 --((M)s);
617 ((M)s).operator--();
618 ((M)s)[0];
619 ((M)s).operator[](0);
620 ((M)s)(0);
621 ((M)s).operator()(0);
622 *((M)s);
623 ((M)s).operator*();
624 ((M)s)->f();
625 ((M)s).operator->();
626 int i = ((M)s);
627 (void)(((M)s) << 0);
628 ((M)s).operator<<(0);
629 (void)(((M)s) == 0);
630 ((M)s).operator==(0);
631 (void)(((M)s) <=> 0);
632 ((M)s).operator<=>(0);
633 }
634 }
635 }
636
637 namespace conversions {
638 //[over.best.ics]
639 struct Y { Y(int); }; //expected-note 3{{candidate}}
640 struct A { operator int(this auto&&); }; //expected-note {{candidate}}
641 Y y1 = A(); // expected-error{{no viable conversion from 'A' to 'Y'}}
642
643 struct X { X(); }; //expected-note 3{{candidate}}
644 struct B { operator X(this auto&&); };
645 B b;
646 X x{{b}}; // expected-error{{no matching constructor for initialization of 'X'}}
647
648 struct T{}; // expected-note 2{{candidate constructor}}
649 struct C {
650 operator T (this int); // expected-note {{candidate function not viable: no known conversion from 'C' to 'int' for object argument}}
651 operator int() const; // expected-note {{candidate function}}
652 };
653
654 void foo(C c) {
655 T d = c; // expected-error {{no viable conversion from 'C' to 'T'}}
656 }
657
658 }
659
660 namespace surrogate {
661 using fn_t = void();
662 struct C {
663 operator fn_t * (this C const &);
664 };
665
666 void foo(C c) {
667 c();
668 }
669
670 }
671
672
673 namespace GH69838 {
674 struct S {
675 S(this auto &self) {} // expected-error {{an explicit object parameter cannot appear in a constructor}}
676 virtual void f(this S self) {} // expected-error {{an explicit object parameter cannot appear in a virtual function}}
677 void g(this auto &self) const {} // expected-error {{explicit object member function cannot have 'const' qualifier}}
678 void h(this S self = S{}) {} // expected-error {{the explicit object parameter cannot have a default argument}}
679 void i(int i, this S self = S{}) {} // expected-error {{an explicit object parameter can only appear as the first parameter of the function}}
680 ~S(this S &&self); // expected-error {{an explicit object parameter cannot appear in a destructor}} \
681 // expected-error {{destructor cannot have any parameters}}
682
683 static void j(this S s); // expected-error {{an explicit object parameter cannot appear in a static function}}
684 };
685
686 void nonmember(this S s); // expected-error {{an explicit object parameter cannot appear in a non-member function}}
687
688 int test() {
689 S s;
690 s.f();
691 s.g();
692 s.h();
693 s.i(0);
694 s.j({});
695 nonmember(S{});
696 }
697
698 }
699
700 namespace GH69962 {
701 struct S {
702 S(const S&);
703 };
704
705 struct Thing {
706 template<typename Self, typename ... Args>
707 Thing(this Self&& self, Args&& ... args) { } // expected-error {{an explicit object parameter cannot appear in a constructor}}
708 };
709
710 class Server : public Thing {
711 S name_;
712 };
713 }
714
715 namespace GH69233 {
716 struct Base {};
717 struct S : Base {
718 int j;
719 S& operator=(this Base& self, const S&) = default;
720 // expected-warning@-1 {{explicitly defaulted copy assignment operator is implicitly deleted}}
721 // expected-note@-2 {{function is implicitly deleted because its declared type does not match the type of an implicit copy assignment operator}}
722 // expected-note@-3 {{explicitly defaulted function was implicitly deleted here}}
723 };
724
725 struct S2 {
726 S2& operator=(this int&& self, const S2&);
727 S2& operator=(this int&& self, S2&&);
728 operator int();
729 };
730
731 S2& S2::operator=(this int&& self, const S2&) = default;
732 // expected-error@-1 {{the type of the explicit object parameter of an explicitly-defaulted copy assignment operator should match the type of the class 'S2'}}
733
734 S2& S2::operator=(this int&& self, S2&&) = default;
735 // expected-error@-1 {{the type of the explicit object parameter of an explicitly-defaulted move assignment operator should match the type of the class 'S2'}}
736
737 struct Move {
738 Move& operator=(this int&, Move&&) = default;
739 // expected-warning@-1 {{explicitly defaulted move assignment operator is implicitly deleted}}
740 // expected-note@-2 {{function is implicitly deleted because its declared type does not match the type of an implicit move assignment operator}}
741 // expected-note@-3 {{copy assignment operator is implicitly deleted because 'Move' has a user-declared move assignment operator}}
742 };
743
744 void test() {
745 S s;
746 s = s; // expected-error {{object of type 'S' cannot be assigned because its copy assignment operator is implicitly deleted}}
747 S2 s2;
748 s2 = s2;
749
750 Move m;
751 m = Move{}; // expected-error {{object of type 'Move' cannot be assigned because its copy assignment operator is implicitly deleted}}
752 }
753
754 }
755
756
757 namespace GH75732 {
758 auto serialize(auto&& archive, auto&& c){ }
759 struct D {
760 auto serialize(this auto&& self, auto&& archive) {
761 serialize(archive, self); // expected-error {{call to explicit member function without an object argument}}
762 }
763 };
764 }
765
766 namespace GH80971 {
767 struct S {
768 auto f(this auto self...) { }
769 };
770
771 int bug() {
772 S{}.f(0);
773 }
774 }
775
776 namespace GH84163 {
777 struct S {
778 int x;
779
780 auto foo() {
781 return [*this](this auto&&) {
782 x = 10; // expected-error {{read-only variable is not assignable}}
783 };
784 }
785 };
786
787 int f() {
788 S s{ 5 };
789 const auto l = s.foo();
790 l(); // expected-note {{in instantiation of}}
791
792 const auto g = [x = 10](this auto&& self) { x = 20; }; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
793 g(); // expected-note {{in instantiation of}}
794 }
795 }
796
797 namespace GH86054 {
798 template<typename M>
799 struct unique_lock {
800 unique_lock(M&) {}
801 };
802 int f() {
803 struct mutex {} cursor_guard;
804 [&cursor_guard](this auto self) {
805 unique_lock a(cursor_guard);
806 }();
807 }
808 }
809
810 namespace GH86398 {
811 struct function {}; // expected-note 2 {{not viable}}
812 int f() {
813 function list;
814 [&list](this auto self) {
815 list = self; // expected-error {{no viable overloaded '='}}
816 }(); // expected-note {{in instantiation of}}
817 }
818
819 struct function2 {
820 function2& operator=(function2 const&) = delete; // expected-note {{candidate function not viable}}
821 };
822 int g() {
823 function2 list;
824 [&list](this auto self) {
825 list = self; // expected-error {{no viable overloaded '='}}
826 }(); // expected-note {{in instantiation of}}
827 }
828
829 struct function3 {
830 function3& operator=(function3 const&) = delete; // expected-note {{has been explicitly deleted}}
831 };
832 int h() {
833 function3 list;
834 [&list](this auto self) {
835 list = function3{}; // expected-error {{selected deleted operator '='}}
836 }();
837 }
838 }
839
840 namespace GH92188 {
841 struct A {
842 template<auto N>
843 void operator+=(this auto &&, const char (&)[N]);
844 void operator+=(this auto &&, auto &&) = delete;
845
846 void f1(this A &, auto &);
847 void f1(this A &, auto &&) = delete;
848
849 void f2(this auto&);
850 void f2(this auto&&) = delete;
851
852 void f3(auto&) &;
853 void f3(this A&, auto&&) = delete;
854
855 void f4(auto&&) & = delete;
856 void f4(this A&, auto&);
857
858 static void f5(auto&);
859 void f5(this A&, auto&&) = delete;
860
861 static void f6(auto&&) = delete;
862 void f6(this A&, auto&);
863
864 void implicit_this() {
865 int lval;
866 operator+=("123");
867 f1(lval);
868 f2();
869 f3(lval);
870 f4(lval);
871 f5(lval);
872 f6(lval);
873 }
874
875 void operator-(this A&, auto&&) = delete;
876 friend void operator-(A&, auto&);
877
878 void operator*(this A&, auto&);
879 friend void operator*(A&, auto&&) = delete;
880 };
881
882 void g() {
883 A a;
884 int lval;
885 a += "123";
886 a.f1(lval);
887 a.f2();
888 a.f3(lval);
889 a.f4(lval);
890 a.f5(lval);
891 a.f6(lval);
892 a - lval;
893 a * lval;
894 }
895 }
896
897 namespace P2797 {
898
899 int bar(void) { return 55; }
900 int (&fref)(void) = bar;
901
902 struct C {
903 void c(this const C&); // #first
904 void c() &; // #second
905 static void c(int = 0); // #third
906
907 void d() {
908 c(); // expected-error {{call to member function 'c' is ambiguous}}
909 // expected-note@#first {{candidate function}}
910 // expected-note@#second {{candidate function}}
911 // expected-note@#third {{candidate function}}
912
913 (C::c)(); // expected-error {{call to member function 'c' is ambiguous}}
914 // expected-note@#first {{candidate function}}
915 // expected-note@#second {{candidate function}}
916 // expected-note@#third {{candidate function}}
917
918 (&(C::c))(); // expected-error {{cannot create a non-constant pointer to member function}}
919 (&C::c)(C{});
920 (&C::c)(*this); // expected-error {{call to non-static member function without an object argument}}
921 (&C::c)();
922
923 (&fref)();
924 }
925 };
926 }
927
928 namespace GH85992 {
929 namespace N {
930 struct A {
931 int f(this A);
932 };
933
934 int f(A);
935 }
936
937 struct S {
938 int (S::*x)(this int); // expected-error {{an explicit object parameter can only appear as the first parameter of a member function}}
939 int (*y)(this int); // expected-error {{an explicit object parameter can only appear as the first parameter of a member function}}
940 int (***z)(this int); // expected-error {{an explicit object parameter can only appear as the first parameter of a member function}}
941
942 int f(this S);
943 int ((g))(this S);
944 friend int h(this S); // expected-error {{an explicit object parameter cannot appear in a non-member function}}
945 int h(int x, int (*)(this S)); // expected-error {{an explicit object parameter can only appear as the first parameter of a member function}}
946
947 struct T {
948 int f(this T);
949 };
950
951 friend int T::f(this T);
952 friend int N::A::f(this N::A);
953 friend int N::f(this N::A); // expected-error {{an explicit object parameter cannot appear in a non-member function}}
954 int friend func(this T); // expected-error {{an explicit object parameter cannot appear in a non-member function}}
955 };
956
957 using T = int (*)(this int); // expected-error {{an explicit object parameter can only appear as the first parameter of a member function}}
958 using U = int (S::*)(this int); // expected-error {{an explicit object parameter can only appear as the first parameter of a member function}}
959 int h(this int); // expected-error {{an explicit object parameter cannot appear in a non-member function}}
960
961 int S::f(this S) { return 1; }
962
963 namespace a {
964 void f();
965 };
966 void a::f(this auto) {} // expected-error {{an explicit object parameter cannot appear in a non-member function}}
967 }