diff options
author | Patrick Palka <ppalka@redhat.com> | 2023-05-11 16:31:33 -0400 |
---|---|---|
committer | Patrick Palka <ppalka@redhat.com> | 2023-05-11 16:31:33 -0400 |
commit | 02777f20be4f40160f1b4ed34fa59ba75245b5b7 (patch) | |
tree | fc0f1282937654022ebe9e38a712300ba541a0d1 /gcc/cp | |
parent | bd02669579fed2617f1090c8640bdcdced1ab7a1 (diff) | |
download | gcc-02777f20be4f40160f1b4ed34fa59ba75245b5b7.zip gcc-02777f20be4f40160f1b4ed34fa59ba75245b5b7.tar.gz gcc-02777f20be4f40160f1b4ed34fa59ba75245b5b7.tar.bz2 |
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.
Diffstat (limited to 'gcc/cp')
-rw-r--r-- | gcc/cp/typeck2.cc | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc index f5cc7c8..72b3831 100644 --- a/gcc/cp/typeck2.cc +++ b/gcc/cp/typeck2.cc @@ -776,6 +776,27 @@ split_nonconstant_init (tree dest, tree init) return code; } +/* T is the initializer of a constexpr variable. Set CONSTRUCTOR_MUTABLE_POISON + for any CONSTRUCTOR within T that contains (directly or indirectly) a mutable + member, thereby poisoning it so it can't be copied to another a constexpr + variable or read during constexpr evaluation. */ + +static void +poison_mutable_constructors (tree t) +{ + if (TREE_CODE (t) != CONSTRUCTOR) + return; + + if (cp_has_mutable_p (TREE_TYPE (t))) + { + CONSTRUCTOR_MUTABLE_POISON (t) = true; + + if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (t)) + for (const constructor_elt &ce : *elts) + poison_mutable_constructors (ce.value); + } +} + /* Perform appropriate conversions on the initial value of a variable, store it in the declaration DECL, and print any error messages that are appropriate. @@ -886,10 +907,7 @@ store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags) else value = fold_non_dependent_init (value, tf_warning_or_error, /*manifestly_const_eval=*/true, decl); - if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type)) - /* Poison this CONSTRUCTOR so it can't be copied to another - constexpr variable. */ - CONSTRUCTOR_MUTABLE_POISON (value) = true; + poison_mutable_constructors (value); const_init = (reduced_constant_expression_p (value) || error_operand_p (value)); DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init; |