public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed] c++: mutable temps in rodata
Date: Tue, 13 Jun 2023 08:09:34 -0400	[thread overview]
Message-ID: <20230613120934.4183450-1-jason@redhat.com> (raw)

Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

If the type of a temporary has mutable members, we can't set TREE_READONLY
on the VAR_DECL; this is parallel to the check in
cp_apply_type_quals_to_decl.

gcc/cp/ChangeLog:

	* tree.cc (build_target_expr): Check TYPE_HAS_MUTABLE_P.

gcc/testsuite/ChangeLog:

	* g++.dg/tree-ssa/initlist-opt6.C: New test.
---
 gcc/cp/tree.cc                                |  1 +
 gcc/testsuite/g++.dg/tree-ssa/initlist-opt6.C | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/tree-ssa/initlist-opt6.C

diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index 751c9adeb62..799183dc646 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -522,6 +522,7 @@ build_target_expr (tree decl, tree value, tsubst_flags_t complain)
   if (CP_TYPE_CONST_NON_VOLATILE_P (type)
       && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
       && !VOID_TYPE_P (TREE_TYPE (value))
+      && !TYPE_HAS_MUTABLE_P (type)
       && reduced_constant_expression_p (value))
     TREE_READONLY (decl) = true;
 
diff --git a/gcc/testsuite/g++.dg/tree-ssa/initlist-opt6.C b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt6.C
new file mode 100644
index 00000000000..ea1bf5d935e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt6.C
@@ -0,0 +1,18 @@
+// { dg-do compile { target c++11 } }
+// { dg-additional-options -fdump-tree-gimple }
+// { dg-final { scan-tree-dump-not {static const struct S} "gimple" } }
+
+// Test that mutable prevents putting this init-list array in rodata.
+
+#include <initializer_list>
+
+struct S {
+    constexpr S(int i) : i(i) {}
+    mutable int i;
+};
+
+void f(std::initializer_list<S>);
+
+int main() {
+    f({1,2,3});
+}

base-commit: d438b67e005bf8fc9e4af26410bf69816c30e969
-- 
2.31.1


                 reply	other threads:[~2023-06-13 12:09 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=20230613120934.4183450-1-jason@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@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).