From: Jason Merrill Date: Mon, 25 May 2020 22:38:09 +0000 (-0400) Subject: c++: -fmerge-all-constants vs. destructors [PR91529] X-Git-Tag: misc/first-auto-changelog-9~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f76202e096fc0be4db21761399b55f10cd09b20d;p=thirdparty%2Fgcc.git 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. --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9ac4220130e6..899f47ae7cbd 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2020-05-25 Jason Merrill + + PR c++/91529 + * decl.c (cp_finish_decl): Also clear TREE_READONLY if + -fmerge-all-constants. + 2020-05-25 Jason Merrill PR c++/93822 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index bf00583e29fe..5a6105d07e51 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -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 index 000000000000..f29c7e58cfcd --- /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 }; +}