public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-8078] c++: fix contracts with NRV
Date: Fri, 17 Nov 2023 00:21:32 +0000 (GMT)	[thread overview]
Message-ID: <20231117002132.6F6573875DE3@sourceware.org> (raw)

https://gcc.gnu.org/g:aabc13dc7dfdede61cc51f96d9e9b942aba66baf

commit r13-8078-gaabc13dc7dfdede61cc51f96d9e9b942aba66baf
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Jun 5 23:58:32 2023 -0400

    c++: fix contracts with NRV
    
    The NRV implementation was blindly replacing the operand of RETURN_EXPR,
    clobbering anything that check_return_expr might have added on to the actual
    initialization, such as checking the postcondition.
    
    gcc/cp/ChangeLog:
    
            * semantics.cc (finalize_nrv_r): [RETURN_EXPR]: Only replace the
            INIT_EXPR.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/contracts/contracts-post7.C: New test.

Diff:
---
 gcc/cp/semantics.cc                              | 12 ++++++++--
 gcc/testsuite/g++.dg/contracts/contracts-post7.C | 29 ++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index e4a8edb1b25..11abff9756f 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -4925,9 +4925,17 @@ finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
     *walk_subtrees = 0;
   /* Change all returns to just refer to the RESULT_DECL; this is a nop,
      but differs from using NULL_TREE in that it indicates that we care
-     about the value of the RESULT_DECL.  */
+     about the value of the RESULT_DECL.  But preserve anything appended
+     by check_return_expr.  */
   else if (TREE_CODE (*tp) == RETURN_EXPR)
-    TREE_OPERAND (*tp, 0) = dp->result;
+    {
+      tree *p = &TREE_OPERAND (*tp, 0);
+      while (TREE_CODE (*p) == COMPOUND_EXPR)
+	p = &TREE_OPERAND (*p, 0);
+      gcc_checking_assert (TREE_CODE (*p) == INIT_EXPR
+			   && TREE_OPERAND (*p, 0) == dp->result);
+      *p = dp->result;
+    }
   /* Change all cleanups for the NRV to only run when an exception is
      thrown.  */
   else if (TREE_CODE (*tp) == CLEANUP_STMT
diff --git a/gcc/testsuite/g++.dg/contracts/contracts-post7.C b/gcc/testsuite/g++.dg/contracts/contracts-post7.C
new file mode 100644
index 00000000000..1c33181b5e6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/contracts/contracts-post7.C
@@ -0,0 +1,29 @@
+// { dg-do run }
+// { dg-options "-std=c++2a -fcontracts" }
+
+#include <experimental/contract>
+
+using std::experimental::contract_violation;
+void handle_contract_violation(const contract_violation &violation)
+{
+  __builtin_exit (0);
+}
+
+struct A {
+  int i;
+  A(): i(42) {}
+  A(const A&);
+};
+
+A f()
+  [[ post r: r.i == 24 ]]
+{
+  A a;
+  return a;
+}
+
+int main()
+{
+  f();
+  return -1;
+}

                 reply	other threads:[~2023-11-17  0:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231117002132.6F6573875DE3@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).