]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: -fmerge-all-constants vs. destructors [PR91529]
authorJason Merrill <jason@redhat.com>
Mon, 25 May 2020 22:38:09 +0000 (18:38 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 25 May 2020 22:38:09 +0000 (18:38 -0400)
cp_finish_decl avoids setting TREE_READONLY on TREE_STATIC variables that
have non-constant construction or destruction, but -fmerge-all-constants was
converting an automatic variable to static while leaving TREE_READONLY set.

Fixed by clearing the flag in cp_finish_decl in the presence of
-fmerge-all-constants.

gcc/cp/ChangeLog
2020-05-01  Jason Merrill  <jason@redhat.com>

PR c++/91529
* decl.c (cp_finish_decl): Also clear TREE_READONLY if
-fmerge-all-constants.

gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/testsuite/g++.dg/init/const14.C [new file with mode: 0644]

index 9ac4220130e6b38d16831b355d8f9b93d4ee87d5..899f47ae7cbddd122f97f7bd5a92b2de64a140da 100644 (file)
@@ -1,3 +1,9 @@
+2020-05-25  Jason Merrill  <jason@redhat.com>
+
+       PR c++/91529
+       * decl.c (cp_finish_decl): Also clear TREE_READONLY if
+       -fmerge-all-constants.
+
 2020-05-25  Jason Merrill  <jason@redhat.com>
 
        PR c++/93822
index bf00583e29fef7a6d35042c300ff363f5a8c5217..5a6105d07e51b3010e5966c261fc2bb64d6d7b76 100644 (file)
@@ -7299,7 +7299,10 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
       /* This needs to happen after the linkage is set. */
       determine_visibility (decl);
 
-      if (var_definition_p && TREE_STATIC (decl))
+      if (var_definition_p
+         /* With -fmerge-all-constants, gimplify_init_constructor
+            might add TREE_STATIC to the variable.  */
+         && (TREE_STATIC (decl) || flag_merge_constants >= 2))
        {
          /* If a TREE_READONLY variable needs initialization
             at runtime, it is no longer readonly and we need to
diff --git a/gcc/testsuite/g++.dg/init/const14.C b/gcc/testsuite/g++.dg/init/const14.C
new file mode 100644 (file)
index 0000000..f29c7e5
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/91529
+// { dg-do run }
+// { dg-additional-options -fmerge-all-constants }
+
+struct A
+{
+  int i[2];
+  ~A() { i[0] = 0; }
+};
+
+int main()
+{
+  const A a = { 1,2 };
+}