forked from gcc-mirror/gcc
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
c++/coroutines: correct passing *this to promise type [PR104981]
When passing *this to the promise type ctor (or to its operator new) (as per [dcl.fct.def.coroutine]/4), we add an explicit cast to lvalue reference. But this is unnecessary since *this is already always an lvalue. And doing so means we need to call convert_from_reference afterward to lower the reference expression to an implicit dereference, which we're currently neglecting to do and which causes overload resolution to get confused when computing argument conversions. So this patch removes this unneeded reference cast when passing *this to the promise ctor, and removes both the cast and implicit deref when passing *this to operator new, for consistency. While we're here, use cp_build_fold_indirect_ref instead of directly building INDIRECT_REF. PR c++/104981 PR c++/115550 gcc/cp/ChangeLog: * coroutines.cc (morph_fn_to_coro): Remove unneeded calls to convert_to_reference and convert_from_reference when passing *this. Use cp_build_fold_indirect_ref instead of directly building INDIRECT_REF. gcc/testsuite/ChangeLog: * g++.dg/coroutines/pr104981-preview-this.C: New test. * g++.dg/coroutines/pr115550-preview-this.C: New test. Reviewed-by: Iain Sandoe <iain@sandoe.co.uk> Reviewed-by: Jason Merrill <jason@redhat.com> (cherry picked from commit 7c5a9bf)
- Loading branch information
Patrick Palka
committed
Jul 23, 2024
1 parent
50ff112
commit 066c789
Showing
3 changed files
with
84 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// PR c++/104981 - ICE from convert_to_base when passing *this to promise ctor | ||
|
||
#include <coroutine> | ||
|
||
class Base {}; | ||
|
||
struct PromiseType; | ||
|
||
struct Result { | ||
using promise_type = PromiseType; | ||
}; | ||
|
||
struct PromiseType { | ||
PromiseType(const Base& parser, auto&&...) {} | ||
|
||
Result get_return_object() { return {}; } | ||
|
||
static std::suspend_never initial_suspend() { return {}; } | ||
static std::suspend_always final_suspend() noexcept { return {}; } | ||
[[noreturn]] static void unhandled_exception() { throw; } | ||
|
||
void return_value(int) {} | ||
}; | ||
|
||
struct Derived : Base { | ||
Result f() { | ||
co_return 42; | ||
} | ||
}; | ||
|
||
int main() { | ||
Derived d; | ||
d.f(); | ||
} |
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,47 @@ | ||
// PR c++/115550 - wrong deduction when passing *this to promise ctor template | ||
|
||
#include <coroutine> | ||
|
||
template <typename T> struct remove_reference { using type = T; }; | ||
template <typename T> struct remove_reference<T&> { using type = T; }; | ||
template <typename T> struct remove_reference<T&&> { using type = T; }; | ||
template <typename T> using remove_reference_t = remove_reference<T>::type; | ||
|
||
template <typename, typename> | ||
struct is_same { static inline constexpr bool value = false; }; | ||
template <typename T> | ||
struct is_same<T, T> { static inline constexpr bool value = true; }; | ||
|
||
template <typename T, typename U> | ||
concept same_as = is_same<T, U>::value; | ||
|
||
struct coroutine | ||
{ | ||
struct promise_type | ||
{ | ||
template <typename Arg> | ||
explicit promise_type(Arg&&) | ||
{ | ||
static_assert(same_as< | ||
remove_reference_t<remove_reference_t<Arg>>, | ||
remove_reference_t<Arg> | ||
>); | ||
} | ||
|
||
coroutine get_return_object() { return {}; } | ||
|
||
std::suspend_never initial_suspend() noexcept { return {}; } | ||
std::suspend_never final_suspend() noexcept { return {}; } | ||
|
||
void return_void() {} | ||
void unhandled_exception() {throw;} | ||
}; | ||
}; | ||
|
||
struct x | ||
{ | ||
coroutine f() | ||
{ | ||
co_return; | ||
} | ||
}; |