Hi
The PR reports that we fail to destroy the object initially created from
the get-return-object call. Fixed by adding a cleanup when the DTOR is
non-trivial. In addition, to meet the specific wording that the call to
get_return_object creates the glvalue for the return, we must construct
that in-place in the return object to avoid a second copy/move CTOR.
tested on x86_64,powerpc64-linux, x86_64-darwin
OK for master?
10.2?
thanks
Iain
gcc/cp/ChangeLog:
PR c++/95477
* coroutines.cc (morph_fn_to_coro): Apply a cleanup to
the get return object when the DTOR is non-trivial.
gcc/testsuite/ChangeLog:
* g++.dg/coroutines/pr95477.C: New test.
---
gcc/cp/coroutines.cc | 61 +++++++++++++++++++----
gcc/testsuite/g++.dg/coroutines/pr95477.C | 37 ++++++++++++++
2 files changed, 89 insertions(+), 9 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/coroutines/pr95477.C
diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc
index f2d7853477d..5a78bec1c9a 100644
--- a/gcc/cp/coroutines.cc
+++ b/gcc/cp/coroutines.cc
@@ -4284,12 +4284,34 @@ morph_fn_to_coro (tree orig, tree *resumer, tree
*destroyer)
tree gro = NULL_TREE;
tree gro_bind_vars = NULL_TREE;
+ tree gro_cleanup_stmt = NULL_TREE;
/* We have to sequence the call to get_return_object before initial
suspend. */
if (gro_is_void_p)
- finish_expr_stmt (get_ro);
+ r = get_ro;
+ else if (same_type_p (gro_type, fn_return_type))
+ {
+ /* [dcl.fct.def.coroutine] / 7
+ The expression promise.get_return_object() is used to initialize the
+ glvalue result or... (see below)
+ Construct the return result directly. */
+ if (TYPE_NEEDS_CONSTRUCTING (gro_type))
+ {
+ vec<tree, va_gc> *arg = make_tree_vector_single (get_ro);
+ r = build_special_member_call (DECL_RESULT (orig),
+ complete_ctor_identifier,
+ &arg, gro_type, LOOKUP_NORMAL,
+ tf_warning_or_error);
+ release_tree_vector (arg);
+ }
+ else
+ r = build2_loc (fn_start, INIT_EXPR, gro_type,
+ DECL_RESULT (orig), get_ro);
+ }
else
{
+ /* ... or ... Construct an object that will be used as the single
+ param to the CTOR for the return object. */
gro = build_lang_decl (VAR_DECL, get_identifier ("coro.gro"), gro_type);
DECL_CONTEXT (gro) = current_scope ();
DECL_ARTIFICIAL (gro) = true;
@@ -4306,8 +4328,21 @@ morph_fn_to_coro (tree orig, tree *resumer, tree
*destroyer)
}
else
r = build2_loc (fn_start, INIT_EXPR, gro_type, gro, get_ro);
- finish_expr_stmt (r);
+ /* The constructed object might require a cleanup. */
+ if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (gro_type))
+ {
+ tree cleanup
+ = build_special_member_call (gro, complete_dtor_identifier,
+ NULL, gro_type, LOOKUP_NORMAL,
+ tf_warning_or_error);
+ gro_cleanup_stmt = build_stmt (input_location, CLEANUP_STMT, NULL,
+ cleanup, gro);
+ }
}
+ finish_expr_stmt (r);
+
+ if (gro_cleanup_stmt)
+ CLEANUP_BODY (gro_cleanup_stmt) = push_stmt_list ();
/* Initialize the resume_idx_name to 0, meaning "not started". */
tree resume_idx_m
@@ -4349,14 +4384,15 @@ morph_fn_to_coro (tree orig, tree *resumer, tree
*destroyer)
promise was constructed. We now supply a reference to that var,
either as the return value (if it's the same type) or to the CTOR
for an object of the return type. */
- if (gro_is_void_p)
- r = NULL_TREE;
- else
- r = rvalue (gro);
- if (!same_type_p (gro_type, fn_return_type))
+ if (same_type_p (gro_type, fn_return_type))
+ r = gro_is_void_p ? NULL_TREE : DECL_RESULT (orig);
+ else
{
- /* The return object is , even if the gro is void. */
+ /* If we have void gro and a non-class return type, then pick a
+ defensive initialisation value. */
+ r = gro_is_void_p ? integer_zero_node : rvalue (gro);
+ /* The return object is constructed, even if the gro is void. */