]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: duplicated side effects of xobj arg [PR113640]
authorPatrick Palka <ppalka@redhat.com>
Tue, 30 Jan 2024 15:44:56 +0000 (10:44 -0500)
committerPatrick Palka <ppalka@redhat.com>
Tue, 30 Jan 2024 15:44:56 +0000 (10:44 -0500)
We miscompile the below testcase because keep_unused_object_arg thinks
the object argument of an xobj member function is unused, and so it ends
up duplicating the argument's side effects.

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.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/call.cc
gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda14.C [new file with mode: 0644]

index 9de0d77c423f485d3ec4b7faa670a8cce021296e..451a189f7cfb3d21a30f9c7a1ed78e99e2149abd 100644 (file)
@@ -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 (file)
index 0000000..5c1d566
--- /dev/null
@@ -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();
+
+  auto l = [](this auto self, int n) {
+    total += n;
+    return self;
+  };
+  total = 0;
+  l(1)(42)(100);
+  if (total != 143)
+    __builtin_abort();
+}