public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: duplicated side effects of xobj arg [PR113640]
@ 2024-01-30 15:09 Patrick Palka
  2024-01-30 15:26 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2024-01-30 15:09 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

Bootstrapped and regtested on x86_64-pc-linu-xgnu, does this
look OK for trunk?

-- >8 --

We miscompile the below testcase because keep_unused_object_arg
duplicates the side effects of a (by-value) object argument of a
xobj member function.

	PR c++/113640

gcc/cp/ChangeLog:

	* call.cc (keep_unused_object_arg): Punt for an xobj member
	function.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp23/explicit-obj-lambda14.C: New test.
---
 gcc/cp/call.cc                                |  2 +-
 .../g++.dg/cpp23/explicit-obj-lambda14.C      | 27 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda14.C

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 9de0d77c423..451a189f7cf 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -5256,7 +5256,7 @@ keep_unused_object_arg (tree result, tree obj, tree fn)
 {
   if (result == NULL_TREE
       || result == error_mark_node
-      || TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
+      || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
       || !TREE_SIDE_EFFECTS (obj))
     return result;
 
diff --git a/gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda14.C b/gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda14.C
new file mode 100644
index 00000000000..03dededfe33
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda14.C
@@ -0,0 +1,27 @@
+// PR c++/113640
+// { dg-do run { target c++23 } }
+
+static int total;
+
+struct A {
+  A f(this auto self, int n) {
+    total += n;
+    return self;
+  }
+};
+
+int main() {
+  A a;
+  a.f(1).f(42).f(100);
+  if (total != 143)
+    __builtin_abort();
+
+  total = 0;
+  auto l = [](this auto self, int n) {
+    total += n;
+    return self;
+  };
+  l(1)(42)(100);
+  if (total != 143)
+    __builtin_abort();
+}
-- 
2.43.0.473.gc5b454771e


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: duplicated side effects of xobj arg [PR113640]
  2024-01-30 15:09 [PATCH] c++: duplicated side effects of xobj arg [PR113640] Patrick Palka
@ 2024-01-30 15:26 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2024-01-30 15:26 UTC (permalink / raw)
  To: Patrick Palka, gcc-patches

On 1/30/24 10:09, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linu-xgnu, does this
> look OK for trunk?

OK.

> -- >8 --
> 
> We miscompile the below testcase because keep_unused_object_arg
> duplicates the side effects of a (by-value) object argument of a
> xobj member function.
> 
> 	PR c++/113640
> 
> gcc/cp/ChangeLog:
> 
> 	* call.cc (keep_unused_object_arg): Punt for an xobj member
> 	function.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp23/explicit-obj-lambda14.C: New test.
> ---
>   gcc/cp/call.cc                                |  2 +-
>   .../g++.dg/cpp23/explicit-obj-lambda14.C      | 27 +++++++++++++++++++
>   2 files changed, 28 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda14.C
> 
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index 9de0d77c423..451a189f7cf 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -5256,7 +5256,7 @@ keep_unused_object_arg (tree result, tree obj, tree fn)
>   {
>     if (result == NULL_TREE
>         || result == error_mark_node
> -      || TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
> +      || DECL_OBJECT_MEMBER_FUNCTION_P (fn)
>         || !TREE_SIDE_EFFECTS (obj))
>       return result;
>   
> diff --git a/gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda14.C b/gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda14.C
> new file mode 100644
> index 00000000000..03dededfe33
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda14.C
> @@ -0,0 +1,27 @@
> +// PR c++/113640
> +// { dg-do run { target c++23 } }
> +
> +static int total;
> +
> +struct A {
> +  A f(this auto self, int n) {
> +    total += n;
> +    return self;
> +  }
> +};
> +
> +int main() {
> +  A a;
> +  a.f(1).f(42).f(100);
> +  if (total != 143)
> +    __builtin_abort();
> +
> +  total = 0;
> +  auto l = [](this auto self, int n) {
> +    total += n;
> +    return self;
> +  };
> +  l(1)(42)(100);
> +  if (total != 143)
> +    __builtin_abort();
> +}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-30 15:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-30 15:09 [PATCH] c++: duplicated side effects of xobj arg [PR113640] Patrick Palka
2024-01-30 15:26 ` 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).