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.
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);
--- /dev/null
+// 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" }
+}