* [PATCH] c++: copy elision of object arg in static memfn call [PR110441]
@ 2023-07-13 18:49 Patrick Palka
2023-07-14 22:23 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2023-07-13 18:49 UTC (permalink / raw)
To: gcc-patches; +Cc: jason, Patrick Palka
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?
-- >8 --
Here the call A().f() is represented as a COMPOUND_EXPR whose first
operand is the otherwise unused object argument A() and second operand
is the call result (both are TARGET_EXPRs). Within the return statement,
this outermost COMPOUND_EXPR ends up foiling the copy elision check in
build_special_member_call, resulting in us introducing a bogus call to the
deleted move constructor. (Within the variable initialization, which goes
through ocp_convert instead of convert_for_initialization, we've already
been eliding the copy despite the outermost COMPOUND_EXPR ever since
r10-7410-g72809d6fe8e085 made ocp_convert look through COMPOUND_EXPR).
In contrast, I noticed '(A(), A::f())' (which should be equivalent to
the above call) is represented with the COMPOUND_EXPR inside the RHS's
TARGET_EXPR initializer thanks to a special case in cp_build_compound_expr
thus avoiding the issue.
So this patch fixes this by making keep_unused_object_arg
use cp_build_compound_expr as well.
PR c++/110441
gcc/cp/ChangeLog:
* call.cc (keep_unused_object_arg): Use cp_build_compound_expr
instead of building a COMPOUND_EXPR directly.
gcc/testsuite/ChangeLog:
* g++.dg/cpp1z/elide8.C: New test.
---
gcc/cp/call.cc | 2 +-
gcc/testsuite/g++.dg/cpp1z/elide8.C | 25 +++++++++++++++++++++++++
2 files changed, 26 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/cpp1z/elide8.C
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 119063979fa..b0a69cb46d4 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -5218,7 +5218,7 @@ keep_unused_object_arg (tree result, tree obj, tree fn)
if (TREE_THIS_VOLATILE (a))
a = build_this (a);
if (TREE_SIDE_EFFECTS (a))
- return build2 (COMPOUND_EXPR, TREE_TYPE (result), a, result);
+ return cp_build_compound_expr (a, result, tf_warning_or_error);
return result;
}
diff --git a/gcc/testsuite/g++.dg/cpp1z/elide8.C b/gcc/testsuite/g++.dg/cpp1z/elide8.C
new file mode 100644
index 00000000000..7d471be8a2a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/elide8.C
@@ -0,0 +1,25 @@
+// PR c++/110441
+// { dg-do compile { target c++11 } }
+
+struct immovable {
+ immovable(immovable &&) = delete;
+};
+
+struct A {
+ static immovable f();
+};
+
+immovable f() {
+ immovable m = A().f(); // { dg-error "deleted" "" { target c++14_down } }
+ return A().f(); // { dg-error "deleted" "" { target c++14_down } }
+}
+
+struct B {
+ A* operator->();
+};
+
+immovable g() {
+ B b;
+ immovable m = b->f(); // { dg-error "deleted" "" { target c++14_down } }
+ return b->f(); // { dg-error "deleted" "" { target c++14_down } }
+}
--
2.41.0.327.gaa9166bcc0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] c++: copy elision of object arg in static memfn call [PR110441]
2023-07-13 18:49 [PATCH] c++: copy elision of object arg in static memfn call [PR110441] Patrick Palka
@ 2023-07-14 22:23 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-07-14 22:23 UTC (permalink / raw)
To: Patrick Palka, gcc-patches
On 7/13/23 14:49, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
OK.
> -- >8 --
>
> Here the call A().f() is represented as a COMPOUND_EXPR whose first
> operand is the otherwise unused object argument A() and second operand
> is the call result (both are TARGET_EXPRs). Within the return statement,
> this outermost COMPOUND_EXPR ends up foiling the copy elision check in
> build_special_member_call, resulting in us introducing a bogus call to the
> deleted move constructor. (Within the variable initialization, which goes
> through ocp_convert instead of convert_for_initialization, we've already
> been eliding the copy despite the outermost COMPOUND_EXPR ever since
> r10-7410-g72809d6fe8e085 made ocp_convert look through COMPOUND_EXPR).
>
> In contrast, I noticed '(A(), A::f())' (which should be equivalent to
> the above call) is represented with the COMPOUND_EXPR inside the RHS's
> TARGET_EXPR initializer thanks to a special case in cp_build_compound_expr
> thus avoiding the issue.
>
> So this patch fixes this by making keep_unused_object_arg
> use cp_build_compound_expr as well.
>
> PR c++/110441
>
> gcc/cp/ChangeLog:
>
> * call.cc (keep_unused_object_arg): Use cp_build_compound_expr
> instead of building a COMPOUND_EXPR directly.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp1z/elide8.C: New test.
> ---
> gcc/cp/call.cc | 2 +-
> gcc/testsuite/g++.dg/cpp1z/elide8.C | 25 +++++++++++++++++++++++++
> 2 files changed, 26 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp1z/elide8.C
>
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index 119063979fa..b0a69cb46d4 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -5218,7 +5218,7 @@ keep_unused_object_arg (tree result, tree obj, tree fn)
> if (TREE_THIS_VOLATILE (a))
> a = build_this (a);
> if (TREE_SIDE_EFFECTS (a))
> - return build2 (COMPOUND_EXPR, TREE_TYPE (result), a, result);
> + return cp_build_compound_expr (a, result, tf_warning_or_error);
> return result;
> }
>
> diff --git a/gcc/testsuite/g++.dg/cpp1z/elide8.C b/gcc/testsuite/g++.dg/cpp1z/elide8.C
> new file mode 100644
> index 00000000000..7d471be8a2a
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1z/elide8.C
> @@ -0,0 +1,25 @@
> +// PR c++/110441
> +// { dg-do compile { target c++11 } }
> +
> +struct immovable {
> + immovable(immovable &&) = delete;
> +};
> +
> +struct A {
> + static immovable f();
> +};
> +
> +immovable f() {
> + immovable m = A().f(); // { dg-error "deleted" "" { target c++14_down } }
> + return A().f(); // { dg-error "deleted" "" { target c++14_down } }
> +}
> +
> +struct B {
> + A* operator->();
> +};
> +
> +immovable g() {
> + B b;
> + immovable m = b->f(); // { dg-error "deleted" "" { target c++14_down } }
> + return b->f(); // { dg-error "deleted" "" { target c++14_down } }
> +}
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-07-14 22:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-13 18:49 [PATCH] c++: copy elision of object arg in static memfn call [PR110441] Patrick Palka
2023-07-14 22:23 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).