The following program is invalid per https://timsong-cpp.github.io/cppwp/n4861/expr.const#4.8 (An object or reference is usable in constant expressions if it is ... a non-mutable subobject) struct U { mutable int x = 2; }; int main() { constexpr U u{}; u.x = 1; static_assert( u.x == 2 ); // must fail, but ok in GCC } but GCC accepts it. Online demo: https://gcc.godbolt.org/z/n8MYzhbad
Confirmed, started with r13-2701-g7107ea6fb933f1
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>: https://gcc.gnu.org/g:fd8a1be04d4cdbfefea457b99ed8404d77b35dd6 commit r14-2198-gfd8a1be04d4cdbfefea457b99ed8404d77b35dd6 Author: Patrick Palka <ppalka@redhat.com> Date: Thu Jun 29 16:02:04 2023 -0400 c++: unpropagated CONSTRUCTOR_MUTABLE_POISON [PR110463] Here we're incorrectly accepting the mutable member accesses because cp_fold neglects to propagate CONSTRUCTOR_MUTABLE_POISON when folding a CONSTRUCTOR. PR c++/110463 gcc/cp/ChangeLog: * cp-gimplify.cc (cp_fold) <case CONSTRUCTOR>: Propagate CONSTRUCTOR_MUTABLE_POISON. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-mutable6.C: New test.
The releases/gcc-13 branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>: https://gcc.gnu.org/g:8f2cafc03f645748109291710157fdeceac32ee1 commit r13-7516-g8f2cafc03f645748109291710157fdeceac32ee1 Author: Patrick Palka <ppalka@redhat.com> Date: Thu Jun 29 16:02:04 2023 -0400 c++: unpropagated CONSTRUCTOR_MUTABLE_POISON [PR110463] Here we're incorrectly accepting the mutable member accesses because cp_fold neglects to propagate CONSTRUCTOR_MUTABLE_POISON when folding a CONSTRUCTOR. PR c++/110463 gcc/cp/ChangeLog: * cp-gimplify.cc (cp_fold) <case CONSTRUCTOR>: Propagate CONSTRUCTOR_MUTABLE_POISON. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-mutable6.C: New test. (cherry picked from commit fd8a1be04d4cdbfefea457b99ed8404d77b35dd6)
Should be fixed for GCC 13.2, thanks for the bug report.
Thanks a lot for a very quick fix!