Skip to content

Commit

Permalink
c++/coroutines: correct passing *this to promise type [PR104981]
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
18 changes: 3 additions & 15 deletions gcc/cp/coroutines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4618,13 +4618,8 @@ morph_fn_to_coro (tree orig, tree *resumer, tree *destroyer)
if (parm_i->this_ptr || parm_i->lambda_cobj)
{
/* We pass a reference to *this to the allocator lookup. */
tree tt = TREE_TYPE (TREE_TYPE (arg));
tree this_ref = build1 (INDIRECT_REF, tt, arg);
tt = cp_build_reference_type (tt, false);
this_ref = convert_to_reference (tt, this_ref, CONV_STATIC,
LOOKUP_NORMAL , NULL_TREE,
tf_warning_or_error);
vec_safe_push (args, convert_from_reference (this_ref));
tree this_ref = cp_build_fold_indirect_ref (arg);
vec_safe_push (args, this_ref);
}
else
vec_safe_push (args, convert_from_reference (arg));
Expand Down Expand Up @@ -4843,14 +4838,7 @@ morph_fn_to_coro (tree orig, tree *resumer, tree *destroyer)
if (parm.this_ptr || parm.lambda_cobj)
{
/* We pass a reference to *this to the param preview. */
tree tt = TREE_TYPE (arg);
gcc_checking_assert (POINTER_TYPE_P (tt));
tree ct = TREE_TYPE (tt);
tree this_ref = build1 (INDIRECT_REF, ct, arg);
tree rt = cp_build_reference_type (ct, false);
this_ref = convert_to_reference (rt, this_ref, CONV_STATIC,
LOOKUP_NORMAL, NULL_TREE,
tf_warning_or_error);
tree this_ref = cp_build_fold_indirect_ref (arg);
vec_safe_push (promise_args, this_ref);
}
else if (parm.rv_ref)
Expand Down
34 changes: 34 additions & 0 deletions gcc/testsuite/g++.dg/coroutines/pr104981-preview-this.C
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();
}
47 changes: 47 additions & 0 deletions gcc/testsuite/g++.dg/coroutines/pr115550-preview-this.C
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;
}
};

0 comments on commit 066c789

Please sign in to comment.