aboutsummaryrefslogtreecommitdiff
path: root/gcc/cp/constexpr.c
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2020-02-19 16:36:38 -0500
committerMarek Polacek <polacek@redhat.com>2020-02-19 19:14:29 -0500
commit8f9dd1b0bdd935592ba151e9d843fddf6193afbc (patch)
treee19b0e3022901799a7fae3326655494fc1ad49a6 /gcc/cp/constexpr.c
parentccf86d54cb02ed24bc4568bd9fffdcdbf0bf68a8 (diff)
downloadgcc-8f9dd1b0bdd935592ba151e9d843fddf6193afbc.zip
gcc-8f9dd1b0bdd935592ba151e9d843fddf6193afbc.tar.gz
gcc-8f9dd1b0bdd935592ba151e9d843fddf6193afbc.tar.bz2
c++: Fix wrong-code with non-constexpr constructor [PR93169]
In order to detect modifying constant objects in constexpr evaluation, which is UB, in r10-2655 I added code that sets TREE_READONLY on CONSTRUCTORs of const-qualified objects after they have been fully constructed. But I never made sure that what we're setting the flag on actually is a CONSTRUCTOR. Consequently, as this test case shows, we could set TREE_READONLY on a VAR_DECL that in fact wasn't constant, causing problems later. Fixed by setting the flag on CONSTRUCTORs only, and only when the evaluation produced something constant. 2020-02-19 Marek Polacek <polacek@redhat.com> PR c++/93169 - wrong-code with a non-constexpr constructor. * constexpr.c (cxx_eval_call_expression): Only set TREE_READONLY on constant CONSTRUCTORs. * g++.dg/cpp0x/constexpr-93169.C: New test.
Diffstat (limited to 'gcc/cp/constexpr.c')
-rw-r--r--gcc/cp/constexpr.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index bf7a264..128f760 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -2474,7 +2474,8 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
/*lval*/false,
non_constant_p,
overflow_p);
- TREE_READONLY (e) = true;
+ if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p)
+ TREE_READONLY (e) = true;
}
/* Forget the saved values of the callee's SAVE_EXPRs and