From: Patrick Palka Date: Thu, 29 Jun 2023 20:02:04 +0000 (-0400) Subject: c++: unpropagated CONSTRUCTOR_MUTABLE_POISON [PR110463] X-Git-Tag: basepoints/gcc-15~7941 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd8a1be04d4cdbfefea457b99ed8404d77b35dd6;p=thirdparty%2Fgcc.git 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) : Propagate CONSTRUCTOR_MUTABLE_POISON. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-mutable6.C: New test. --- diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index 853b1e442363..f57341977748 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -3079,6 +3079,8 @@ cp_fold (tree x, fold_flags_t flags) x = build_constructor (TREE_TYPE (x), nelts); CONSTRUCTOR_PLACEHOLDER_BOUNDARY (x) = CONSTRUCTOR_PLACEHOLDER_BOUNDARY (org_x); + CONSTRUCTOR_MUTABLE_POISON (x) + = CONSTRUCTOR_MUTABLE_POISON (org_x); } if (VECTOR_TYPE_P (TREE_TYPE (x))) x = fold (x); diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-mutable6.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-mutable6.C new file mode 100644 index 000000000000..2c946e388ab4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-mutable6.C @@ -0,0 +1,18 @@ +// PR c++/110463 +// { dg-do compile { target c++11 } } + +struct U { + mutable int x = 1; +}; + +struct V { + mutable int y = 1+1; +}; + +int main() { + constexpr U u = {}; + constexpr int x = u.x; // { dg-error "mutable" } + + constexpr V v = {}; + constexpr int y = v.y; // { dg-error "mutable" } +}