Trivial initialization shouldn't bump a variable out of .rodata; if the
result of build_aggr_init is an empty STATEMENT_LIST, throw it away.
PR c++/104142
gcc/cp/ChangeLog:
* decl.cc (check_initializer): Check TREE_SIDE_EFFECTS.
gcc/testsuite/ChangeLog:
* g++.dg/opt/const7.C: New test.
if (init && init != error_mark_node)
init_code = build2 (INIT_EXPR, type, decl, init);
+ if (init_code && !TREE_SIDE_EFFECTS (init_code)
+ && init_code != error_mark_node)
+ init_code = NULL_TREE;
+
if (init_code)
{
/* We might have set these in cp_finish_decl. */
--- /dev/null
+// PR c++/104142
+// { dg-do compile { target c++11 } }
+// { dg-additional-options -Wunused-variable }
+
+struct B { B()=default; };
+static const B b_var; // { dg-bogus "" }
+// { dg-final { scan-assembler-symbol-section {b_var} {^\.(const|rodata)|\[RO\]} } }