c++: 'mutable' subobject of constexpr variable [PR109745]
commit02777f20be4f40160f1b4ed34fa59ba75245b5b7
authorPatrick Palka <ppalka@redhat.com>
Thu, 11 May 2023 20:31:33 +0000 (11 16:31 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 11 May 2023 20:31:33 +0000 (11 16:31 -0400)
treefc0f1282937654022ebe9e38a712300ba541a0d1
parentbd02669579fed2617f1090c8640bdcdced1ab7a1
c++: 'mutable' subobject of constexpr variable [PR109745]

r13-2701-g7107ea6fb933f1 made us correctly accept during constexpr
evaluation 'mutable' member accesses of objects constructed during
that evaluation, while continuing to reject such accesses for constexpr
objects constructed outside of that evaluation, by considering the
CONSTRUCTOR_MUTABLE_POISON flag during cxx_eval_component_reference.

However, this flag is set only for the outermost CONSTRUCTOR of a
constexpr variable initializer, so if we're accessing a 'mutable' member
of a nested CONSTRUCTOR, the flag won't be set and we won't reject the
access.  This can lead to us accepting invalid code, as in the first
testcase, or even wrong code generation due to our speculative constexpr
evaluation, as in the second and third testcase.

This patch fixes this by setting CONSTRUCTOR_MUTABLE_POISON recursively
rather than only on the outermost CONSTRUCTOR.

PR c++/109745

gcc/cp/ChangeLog:

* typeck2.cc (poison_mutable_constructors): Define.
(store_init_value): Use it instead of setting
CONSTRUCTOR_MUTABLE_POISON directly.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/constexpr-mutable4.C: New test.
* g++.dg/cpp0x/constexpr-mutable5.C: New test.
* g++.dg/cpp1y/constexpr-mutable2.C: New test.
gcc/cp/typeck2.cc
gcc/testsuite/g++.dg/cpp0x/constexpr-mutable4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/constexpr-mutable5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-mutable2.C [new file with mode: 0644]