]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: mutable temps in rodata [PR116369]
authorMarek Polacek <polacek@redhat.com>
Thu, 29 Aug 2024 19:13:03 +0000 (15:13 -0400)
committerMarek Polacek <polacek@redhat.com>
Thu, 9 Jan 2025 19:46:16 +0000 (14:46 -0500)
Here we wrongly mark the reference temporary for g TREE_READONLY,
so it's put in .rodata and so we can't modify its subobject even
when the subobject is marked mutable.  This is so since r9-869.
r14-1785 fixed a similar problem, but not in set_up_extended_ref_temp.

PR c++/116369

gcc/cp/ChangeLog:

* call.cc (set_up_extended_ref_temp): Don't mark a temporary
TREE_READONLY if its type is TYPE_HAS_MUTABLE_P.

gcc/testsuite/ChangeLog:

* g++.dg/tree-ssa/initlist-opt7.C: New test.

(cherry picked from commit 2801a49d1144bce5568b527d1972952ad3420f66)

gcc/cp/call.cc
gcc/testsuite/g++.dg/tree-ssa/initlist-opt7.C [new file with mode: 0644]

index f75956f7dc21d9d1fe4226d7292b964f2c609418..1609a64c3fa6ccf3ee870bd023ca4ae88b10a4af 100644 (file)
@@ -13905,7 +13905,9 @@ set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
   init = cp_fully_fold (init);
   if (TREE_CONSTANT (init))
     {
-      if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
+      if (literal_type_p (type)
+         && CP_TYPE_CONST_NON_VOLATILE_P (type)
+         && !TYPE_HAS_MUTABLE_P (type))
        {
          /* 5.19 says that a constant expression can include an
             lvalue-rvalue conversion applied to "a glvalue of literal type
diff --git a/gcc/testsuite/g++.dg/tree-ssa/initlist-opt7.C b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt7.C
new file mode 100644 (file)
index 0000000..2420db5
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/116369
+// { dg-do run { target c++11 } }
+
+struct f{
+  mutable int t;
+};
+
+const f &g = {1};
+
+int main()
+{
+  g.t++;
+}