From a2f32550a085984fbaaec962bf7723514ac71dff Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Fri, 1 May 2020 13:53:32 -0400 Subject: [PATCH] c++: -fmerge-all-constants vs. destructors [PR91529] 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 PR c++/91529 * decl.c (cp_finish_decl): Also clear TREE_READONLY if -fmerge-all-constants. --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/decl.c | 5 ++++- gcc/testsuite/g++.dg/init/const14.C | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/init/const14.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 039e946f950..44686909c83 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2020-05-01 Jason Merrill + + PR c++/91529 + * decl.c (cp_finish_decl): Also clear TREE_READONLY if + -fmerge-all-constants. + 2020-05-01 Jason Merrill PR c++/93822 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 4c0ae1cfa2e..3e7ed98fed3 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -7833,7 +7833,10 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, maybe_commonize_var (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 index 00000000000..f29c7e58cfc --- /dev/null +++ b/gcc/testsuite/g++.dg/init/const14.C @@ -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 }; +}