-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C++20][Coroutines] Lambda-coroutine with operator new in promise_type (
#84193) Fix #84064 According to http://eel.is/c++draft/dcl.fct.def.coroutine#9 the first parameter for overload resolution of `operator new` is `size_t` followed by the arguments of the coroutine function. http://eel.is/c++draft/dcl.fct.def.coroutine#4 states that the first argument is the lvalue of `*this` if the coroutine is a member function. Before this patch, Clang handled class types correctly but ignored lambdas. This patch adds support for lambda coroutines with a `promise_type` that implements a custom `operator new`. The patch does consider C++23 `static operator()`, which already worked as there is no `this` parameter.
- Loading branch information
1 parent
0456a32
commit 35d3b33
Showing
5 changed files
with
169 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify -I%S/Inputs -std=c++20 %s | ||
|
||
// expected-no-diagnostics | ||
|
||
#include "std-coroutine.h" | ||
|
||
using size_t = decltype(sizeof(0)); | ||
|
||
struct Generator { | ||
struct promise_type { | ||
int _val{}; | ||
|
||
Generator get_return_object() noexcept | ||
{ | ||
return {}; | ||
} | ||
|
||
std::suspend_never initial_suspend() noexcept | ||
{ | ||
return {}; | ||
} | ||
|
||
std::suspend_always final_suspend() noexcept | ||
{ | ||
return {}; | ||
} | ||
|
||
void return_void() noexcept {} | ||
void unhandled_exception() noexcept {} | ||
|
||
template<typename This, typename... TheRest> | ||
static void* | ||
operator new(size_t size, | ||
This&, | ||
TheRest&&...) noexcept | ||
{ | ||
return nullptr; | ||
} | ||
|
||
static void operator delete(void*, size_t) | ||
{ | ||
} | ||
}; | ||
}; | ||
|
||
struct CapturingThisTest | ||
{ | ||
int x{}; | ||
|
||
void AsPointer() | ||
{ | ||
auto lamb = [=,this]() -> Generator { | ||
int y = x; | ||
co_return; | ||
}; | ||
|
||
static_assert(sizeof(decltype(lamb)) == sizeof(void*)); | ||
} | ||
|
||
void AsStarThis() | ||
{ | ||
auto lamb = [*this]() -> Generator { | ||
int y = x; | ||
co_return; | ||
}; | ||
|
||
static_assert(sizeof(decltype(lamb)) == sizeof(int)); | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
auto lamb = []() -> Generator { | ||
co_return; | ||
}; | ||
|
||
static_assert(sizeof(decltype(lamb)) == 1); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify -I%S/Inputs -std=c++23 %s | ||
|
||
// expected-no-diagnostics | ||
|
||
#include "std-coroutine.h" | ||
|
||
using size_t = decltype(sizeof(0)); | ||
|
||
struct GeneratorStatic { | ||
struct promise_type { | ||
int _val{}; | ||
|
||
GeneratorStatic get_return_object() noexcept | ||
{ | ||
return {}; | ||
} | ||
|
||
std::suspend_never initial_suspend() noexcept | ||
{ | ||
return {}; | ||
} | ||
|
||
std::suspend_always final_suspend() noexcept | ||
{ | ||
return {}; | ||
} | ||
|
||
void return_void() noexcept {} | ||
void unhandled_exception() noexcept {} | ||
|
||
template<typename... TheRest> | ||
static void* | ||
operator new(size_t size, | ||
TheRest&&...) noexcept | ||
{ | ||
return nullptr; | ||
} | ||
|
||
static void operator delete(void*, size_t) | ||
{ | ||
} | ||
}; | ||
}; | ||
|
||
|
||
int main() | ||
{ | ||
auto lambCpp23 = []() static -> GeneratorStatic { | ||
co_return; | ||
}; | ||
|
||
static_assert(sizeof(decltype(lambCpp23)) == 1); | ||
} |