[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / coroutine-allocs.cpp
blobc9797208e2dc5841464574413005073e11d8b993
1 // RUN: %clang_cc1 %s -std=c++20 -fsyntax-only -verify
2 #include "Inputs/std-coroutine.h"
4 namespace std {
5 typedef decltype(sizeof(int)) size_t;
8 struct Allocator {};
10 struct resumable {
11 struct promise_type {
12 void *operator new(std::size_t sz, Allocator &);
14 resumable get_return_object() { return {}; }
15 auto initial_suspend() { return std::suspend_always(); }
16 auto final_suspend() noexcept { return std::suspend_always(); }
17 void unhandled_exception() {}
18 void return_void(){};
22 resumable f1() { // expected-error {{'operator new' provided by 'std::coroutine_traits<resumable>::promise_type' (aka 'resumable::promise_type') is not usable with the function signature of 'f1'}}
23 co_return;
26 // NOTE: Although the argument here is a rvalue reference and the corresponding
27 // allocation function in resumable::promise_type have lvalue references, it looks
28 // the signature of f2 is invalid. But according to [dcl.fct.def.coroutine]p4:
30 // In the following, pi is an lvalue of type Pi, where p1 denotes the object
31 // parameter and pi+1 denotes the ith non-object function parameter for a
32 // non-static member function.
34 // And [dcl.fct.def.coroutine]p9.1
36 // overload resolution is performed on a function call created by assembling an argument list.
37 // The first argument is the amount of space requested, and has type std::size_­t.
38 // The lvalues p1…pn are the succeeding arguments.
40 // So the actual type passed to resumable::promise_type::operator new is lvalue
41 // Allocator. It is allowed to convert a lvalue to a lvalue reference. So the
42 // following one is valid.
43 resumable f2(Allocator &&) {
44 co_return;
47 resumable f3(Allocator &) {
48 co_return;
51 resumable f4(Allocator) {
52 co_return;
55 resumable f5(const Allocator) { // expected-error {{operator new' provided by 'std::coroutine_traits<resumable, const Allocator>::promise_type' (aka 'resumable::promise_type') is not usable}}
56 co_return;
59 resumable f6(const Allocator &) { // expected-error {{operator new' provided by 'std::coroutine_traits<resumable, const Allocator &>::promise_type' (aka 'resumable::promise_type') is not usable}}
60 co_return;
63 struct promise_base1 {
64 void *operator new(std::size_t sz); // expected-note {{member found by ambiguous name lookup}}
67 struct promise_base2 {
68 void *operator new(std::size_t sz); // expected-note {{member found by ambiguous name lookup}}
71 struct resumable2 {
72 struct promise_type : public promise_base1, public promise_base2 {
73 resumable2 get_return_object() { return {}; }
74 auto initial_suspend() { return std::suspend_always(); }
75 auto final_suspend() noexcept { return std::suspend_always(); }
76 void unhandled_exception() {}
77 void return_void(){};
81 resumable2 f7() { // expected-error {{member 'operator new' found in multiple base classes of different types}}
82 co_return;