diff options
author | Jason Merrill <jason@redhat.com> | 2023-06-13 07:29:34 -0400 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2023-06-13 08:09:48 -0400 |
commit | 47c3144c2f6b001031b1fbfa301bb987075408a7 (patch) | |
tree | bf09f778ec639bfe20ff6324059fafcba0834c42 | |
parent | 1d4d302acd915a81f4b7d7a6db44999531f2fd31 (diff) | |
download | gcc-47c3144c2f6b001031b1fbfa301bb987075408a7.zip gcc-47c3144c2f6b001031b1fbfa301bb987075408a7.tar.gz gcc-47c3144c2f6b001031b1fbfa301bb987075408a7.tar.bz2 |
c++: mutable temps in rodata
If the type of a temporary has mutable members, we can't set TREE_READONLY
on the VAR_DECL; this is parallel to the check in
cp_apply_type_quals_to_decl.
gcc/cp/ChangeLog:
* tree.cc (build_target_expr): Check TYPE_HAS_MUTABLE_P.
gcc/testsuite/ChangeLog:
* g++.dg/tree-ssa/initlist-opt6.C: New test.
-rw-r--r-- | gcc/cp/tree.cc | 1 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/tree-ssa/initlist-opt6.C | 18 |
2 files changed, 19 insertions, 0 deletions
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 751c9ad..799183d 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -522,6 +522,7 @@ build_target_expr (tree decl, tree value, tsubst_flags_t complain) if (CP_TYPE_CONST_NON_VOLATILE_P (type) && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) && !VOID_TYPE_P (TREE_TYPE (value)) + && !TYPE_HAS_MUTABLE_P (type) && reduced_constant_expression_p (value)) TREE_READONLY (decl) = true; diff --git a/gcc/testsuite/g++.dg/tree-ssa/initlist-opt6.C b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt6.C new file mode 100644 index 0000000..ea1bf5d --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/initlist-opt6.C @@ -0,0 +1,18 @@ +// { dg-do compile { target c++11 } } +// { dg-additional-options -fdump-tree-gimple } +// { dg-final { scan-tree-dump-not {static const struct S} "gimple" } } + +// Test that mutable prevents putting this init-list array in rodata. + +#include <initializer_list> + +struct S { + constexpr S(int i) : i(i) {} + mutable int i; +}; + +void f(std::initializer_list<S>); + +int main() { + f({1,2,3}); +} |