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

Reply via email to