From 04e1749c557a5df14f8528efa451bb0e93afea80 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Mon, 19 Aug 2019 13:59:13 +0000 Subject: [PATCH] PR c++/91264 - detect modifying const objects in constexpr. * constexpr.c (modifying_const_object_error): New function. (cxx_eval_call_expression): Set TREE_READONLY on a CONSTRUCTOR of a const-qualified object after it's been fully constructed. (modifying_const_object_p): New function. (cxx_eval_store_expression): Detect modifying a const object during constant expression evaluation. (cxx_eval_increment_expression): Use a better location when building up the store. (cxx_eval_constant_expression) : Mark a constant object's constructor TREE_READONLY. * g++.dg/cpp1y/constexpr-tracking-const1.C: New test. * g++.dg/cpp1y/constexpr-tracking-const2.C: New test. * g++.dg/cpp1y/constexpr-tracking-const3.C: New test. * g++.dg/cpp1y/constexpr-tracking-const4.C: New test. * g++.dg/cpp1y/constexpr-tracking-const5.C: New test. * g++.dg/cpp1y/constexpr-tracking-const6.C: New test. * g++.dg/cpp1y/constexpr-tracking-const7.C: New test. * g++.dg/cpp1y/constexpr-tracking-const8.C: New test. * g++.dg/cpp1y/constexpr-tracking-const9.C: New test. * g++.dg/cpp1y/constexpr-tracking-const10.C: New test. * g++.dg/cpp1y/constexpr-tracking-const11.C: New test. * g++.dg/cpp1y/constexpr-tracking-const12.C: New test. * g++.dg/cpp1y/constexpr-tracking-const13.C: New test. * g++.dg/cpp1y/constexpr-tracking-const14.C: New test. From-SVN: r274671 --- gcc/cp/ChangeLog | 14 ++ gcc/cp/constexpr.c | 140 +++++++++++++++++- gcc/testsuite/ChangeLog | 18 +++ .../g++.dg/cpp1y/constexpr-tracking-const1.C | 72 +++++++++ .../g++.dg/cpp1y/constexpr-tracking-const10.C | 22 +++ .../g++.dg/cpp1y/constexpr-tracking-const11.C | 16 ++ .../g++.dg/cpp1y/constexpr-tracking-const12.C | 17 +++ .../g++.dg/cpp1y/constexpr-tracking-const13.C | 20 +++ .../g++.dg/cpp1y/constexpr-tracking-const14.C | 38 +++++ .../g++.dg/cpp1y/constexpr-tracking-const2.C | 23 +++ .../g++.dg/cpp1y/constexpr-tracking-const3.C | 22 +++ .../g++.dg/cpp1y/constexpr-tracking-const4.C | 17 +++ .../g++.dg/cpp1y/constexpr-tracking-const5.C | 17 +++ .../g++.dg/cpp1y/constexpr-tracking-const6.C | 22 +++ .../g++.dg/cpp1y/constexpr-tracking-const7.C | 23 +++ .../g++.dg/cpp1y/constexpr-tracking-const8.C | 23 +++ .../g++.dg/cpp1y/constexpr-tracking-const9.C | 23 +++ 17 files changed, 526 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const1.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const10.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const11.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const12.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const13.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const14.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const2.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const3.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const4.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const5.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const6.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const7.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const8.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const9.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 938ec486d9c..c40bc9c15b9 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,17 @@ +2019-08-19 Marek Polacek + + PR c++/91264 - detect modifying const objects in constexpr. + * constexpr.c (modifying_const_object_error): New function. + (cxx_eval_call_expression): Set TREE_READONLY on a CONSTRUCTOR of + a const-qualified object after it's been fully constructed. + (modifying_const_object_p): New function. + (cxx_eval_store_expression): Detect modifying a const object + during constant expression evaluation. + (cxx_eval_increment_expression): Use a better location when building + up the store. + (cxx_eval_constant_expression) : Mark a constant + object's constructor TREE_READONLY. + 2019-08-15 Jason Merrill PR c++/90393 - ICE with thow in ?: diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 23f2a027e2f..dbd0dc3b445 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1575,6 +1575,19 @@ clear_no_implicit_zero (tree ctor) } } +/* Complain about a const object OBJ being modified in a constant expression. + EXPR is the MODIFY_EXPR expression performing the modification. */ + +static void +modifying_const_object_error (tree expr, tree obj) +{ + location_t loc = cp_expr_loc_or_input_loc (expr); + auto_diagnostic_group d; + error_at (loc, "modifying a const object %qE is not allowed in " + "a constant expression", TREE_OPERAND (expr, 0)); + inform (location_of (obj), "originally declared % here"); +} + /* Subroutine of cxx_eval_constant_expression. Evaluate the call expression tree T in the context of OLD_CALL expression evaluation. */ @@ -1775,6 +1788,19 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, depth_ok = push_cx_call_context (t); + /* Remember the object we are constructing. */ + tree new_obj = NULL_TREE; + if (DECL_CONSTRUCTOR_P (fun)) + { + /* In a constructor, it should be the first `this' argument. + At this point it has already been evaluated in the call + to cxx_bind_parameters_in_call. */ + new_obj = TREE_VEC_ELT (new_call.bindings, 0); + STRIP_NOPS (new_obj); + if (TREE_CODE (new_obj) == ADDR_EXPR) + new_obj = TREE_OPERAND (new_obj, 0); + } + tree result = NULL_TREE; constexpr_call *entry = NULL; @@ -1910,6 +1936,23 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, } } + /* At this point, the object's constructor will have run, so + the object is no longer under construction, and its possible + 'const' semantics now apply. Make a note of this fact by + marking the CONSTRUCTOR TREE_READONLY. */ + if (new_obj + && CLASS_TYPE_P (TREE_TYPE (new_obj)) + && CP_TYPE_CONST_P (TREE_TYPE (new_obj))) + { + /* Subobjects might not be stored in ctx->values but we can + get its CONSTRUCTOR by evaluating *this. */ + tree e = cxx_eval_constant_expression (ctx, new_obj, + /*lval*/false, + non_constant_p, + overflow_p); + TREE_READONLY (e) = true; + } + /* Forget the saved values of the callee's SAVE_EXPRs. */ unsigned int i; tree save_expr; @@ -3724,6 +3767,26 @@ maybe_simplify_trivial_copy (tree &target, tree &init) } } +/* Return true if we are modifying something that is const during constant + expression evaluation. CODE is the code of the statement, OBJ is the + object in question, MUTABLE_P is true if one of the subobjects were + declared mutable. */ + +static bool +modifying_const_object_p (tree_code code, tree obj, bool mutable_p) +{ + /* If this is initialization, there's no problem. */ + if (code != MODIFY_EXPR) + return false; + + /* [basic.type.qualifier] "A const object is an object of type + const T or a non-mutable subobject of a const object." */ + if (mutable_p) + return false; + + return (TREE_READONLY (obj) || CP_TYPE_CONST_P (TREE_TYPE (obj))); +} + /* Evaluate an INIT_EXPR or MODIFY_EXPR. */ static tree @@ -3773,6 +3836,9 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, /* Find the underlying variable. */ releasing_vec refs; tree object = NULL_TREE; + /* If we're modifying a const object, save it. */ + tree const_object_being_modified = NULL_TREE; + bool mutable_p = false; for (tree probe = target; object == NULL_TREE; ) { switch (TREE_CODE (probe)) @@ -3783,6 +3849,12 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, { tree ob = TREE_OPERAND (probe, 0); tree elt = TREE_OPERAND (probe, 1); + if (DECL_P (elt) && DECL_MUTABLE_P (elt)) + mutable_p = true; + if (evaluated + && modifying_const_object_p (TREE_CODE (t), probe, mutable_p) + && const_object_being_modified == NULL_TREE) + const_object_being_modified = probe; if (TREE_CODE (probe) == ARRAY_REF) { elt = eval_and_check_array_index (ctx, probe, false, @@ -3811,6 +3883,10 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, } } + if (modifying_const_object_p (TREE_CODE (t), object, mutable_p) + && const_object_being_modified == NULL_TREE) + const_object_being_modified = object; + /* And then find/build up our initializer for the path to the subobject we're initializing. */ tree *valp; @@ -3950,6 +4026,62 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, valp = &cep->value; } + /* Detect modifying a constant object in constexpr evaluation. + We have found a const object that is being modified. Figure out + if we need to issue an error. Consider + + struct A { + int n; + constexpr A() : n(1) { n = 2; } // #1 + }; + struct B { + const A a; + constexpr B() { a.n = 3; } // #2 + }; + constexpr B b{}; + + #1 is OK, since we're modifying an object under construction, but + #2 is wrong, since "a" is const and has been fully constructed. + To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR + which means that the object is read-only. For the example above, the + *ctors stack at the point of #2 will look like: + + ctors[0] = {.a={.n=2}} TREE_READONLY = 0 + ctors[1] = {.n=2} TREE_READONLY = 1 + + and we're modifying "b.a", so we search the stack and see if the + constructor for "b.a" has already run. */ + if (const_object_being_modified) + { + bool fail = false; + if (!CLASS_TYPE_P (TREE_TYPE (const_object_being_modified))) + fail = true; + else + { + /* [class.ctor]p5 "A constructor can be invoked for a const, + volatile, or const volatile object. const and volatile + semantics are not applied on an object under construction. + They come into effect when the constructor for the most + derived object ends." */ + tree elt; + unsigned int i; + FOR_EACH_VEC_ELT (*ctors, i, elt) + if (same_type_ignoring_top_level_qualifiers_p + (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt))) + { + fail = TREE_READONLY (elt); + break; + } + } + if (fail) + { + if (!ctx->quiet) + modifying_const_object_error (t, const_object_being_modified); + *non_constant_p = true; + return t; + } + } + if (!preeval) { /* Create a new CONSTRUCTOR in case evaluation of the initializer @@ -4063,7 +4195,8 @@ cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t, VERIFY_CONSTANT (mod); /* Storing the modified value. */ - tree store = build2 (MODIFY_EXPR, type, op, mod); + tree store = build2_loc (cp_expr_loc_or_loc (t, input_location), + MODIFY_EXPR, type, op, mod); cxx_eval_constant_expression (ctx, store, true, non_constant_p, overflow_p); ggc_free (store); @@ -4650,6 +4783,11 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, non_constant_p, overflow_p); /* Don't share a CONSTRUCTOR that might be changed. */ init = unshare_constructor (init); + /* Remember that a constant object's constructor has already + run. */ + if (CLASS_TYPE_P (TREE_TYPE (r)) + && CP_TYPE_CONST_P (TREE_TYPE (r))) + TREE_READONLY (init) = true; ctx->values->put (r, init); } else if (ctx == &new_ctx) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f20c466ffad..ad514b11b8a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,21 @@ +2019-08-19 Marek Polacek + + PR c++/91264 - detect modifying const objects in constexpr. + * g++.dg/cpp1y/constexpr-tracking-const1.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const2.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const3.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const4.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const5.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const6.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const7.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const8.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const9.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const10.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const11.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const12.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const13.C: New test. + * g++.dg/cpp1y/constexpr-tracking-const14.C: New test. + 2019-08-19 Eric Botcazou * gnat.dg/elab8.adb, gnat.dg/elab8_gen.adb, diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const1.C new file mode 100644 index 00000000000..e081a535659 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const1.C @@ -0,0 +1,72 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +constexpr void +mod (int &r) +{ + r = 99; // { dg-error "modifying a const object" } +} + +constexpr int +fn1 () +{ + const int i = 0; // { dg-message "originally declared" } + mod (const_cast(i)); // { dg-message "in .constexpr. expansion of " } + return i; +} + +constexpr int i1 = fn1 (); // { dg-message "in .constexpr. expansion of " } + +constexpr int +fn2 () +{ + const int i = 5; // { dg-message "originally declared" } + const_cast(i) = 10; // { dg-error "modifying a const object" } + return i; +} + +constexpr int i2 = fn2 (); // { dg-message "in .constexpr. expansion of " } + +constexpr int +fn3 () +{ + const int i = 5; // { dg-message "originally declared" } + ++const_cast(i); // { dg-error "modifying a const object" } + return i; +} + +constexpr int i3 = fn3 (); // { dg-message "in .constexpr. expansion of " } + +constexpr int +fn4 () +{ + const int i = 5; // { dg-message "originally declared" } + const_cast(i)--; // { dg-error "modifying a const object" } + return i; +} + +constexpr int i4 = fn4 (); // { dg-message "in .constexpr. expansion of " } + +constexpr int +fn5 () +{ + const int i = 5; // { dg-message "originally declared" } + const_cast(i) += 2; // { dg-error "modifying a const object" } + return i; +} + +constexpr int i5 = fn5 (); // { dg-message "in .constexpr. expansion of " } + +constexpr int +fn6 () +{ + // This is OK. + int i = 3; + const int *cip = &i; + int *ip = const_cast(cip); + *ip = 4; + return i; +} + +constexpr int i6 = fn6 (); +static_assert(i6 == 4, ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const10.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const10.C new file mode 100644 index 00000000000..53acb37beb8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const10.C @@ -0,0 +1,22 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct B { + B() = default; + int i; +}; + +constexpr B bar() +{ + constexpr B b = B(); // { dg-message "originally declared" } + B *p = const_cast(&b); + + p->i = 11; // { dg-error "modifying a const object" } + + return *p; +} + +void foo() +{ + constexpr B y = bar(); // { dg-message "in .constexpr. expansion of" } +} diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const11.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const11.C new file mode 100644 index 00000000000..2b351cd013a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const11.C @@ -0,0 +1,16 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct S { + int a = 1; + int * ptr = &a; +}; + +constexpr bool f() { + auto const s = S{}; // { dg-message "originally declared" } + *s.ptr = 2; // { dg-error "modifying a const object" } + return s.a == 2; +} + +static_assert(f(), ""); // { dg-error "non-constant condition" } +// { dg-message "in 'constexpr' expansion of" "" { target *-*-* } .-1 } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const12.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const12.C new file mode 100644 index 00000000000..d83e2794f7f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const12.C @@ -0,0 +1,17 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct A { + const int n; + int m; + constexpr A() : n(1), m(2) { } +}; +struct B { + A a; + constexpr B() { + int *p = &a.m; + *p = 3; + } +}; +constexpr B b; +static_assert(b.a.m == 3, ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const13.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const13.C new file mode 100644 index 00000000000..bc7faa3c250 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const13.C @@ -0,0 +1,20 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct A { + mutable int i; + constexpr A() : i(0) { } +}; +struct B { + A a; + constexpr B() : a{} { } +}; + +constexpr void +g () +{ + const B b; + b.a.i = 42; +} + +static_assert((g(), 1), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const14.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const14.C new file mode 100644 index 00000000000..45c4fcf50be --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const14.C @@ -0,0 +1,38 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct F { + const int f; + constexpr F() : f(9) { } +}; + +struct C { + int n; + const F f; + constexpr C() : n(1) { n = 66; } +}; + +struct A { + int r; + const C c; + constexpr A() : r(11) { r = 14; const_cast(c).n = 42; } // { dg-error "modifying a const object" } +}; + +struct D { + const A a; + constexpr D() { } // { dg-message "in .constexpr. expansion of" } +}; + +struct E { + const D d; + constexpr E() { } // { dg-message "in .constexpr. expansion of" } +}; + +struct B { + const E e; + constexpr B(bool) { } // { dg-message "in .constexpr. expansion of" } +}; + +constexpr B b(false); // { dg-message "in .constexpr. expansion of" } +// { dg-message "originally declared" "" { target *-*-* } .-1 } +static_assert(b.e.d.a.c.n == 2, ""); // { dg-error "non-constant condition" } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const2.C new file mode 100644 index 00000000000..9803309cace --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const2.C @@ -0,0 +1,23 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct X { + int j; + constexpr X() : j(0) { } +}; + +struct Y { + X x; + constexpr Y() : x{} { } +}; + +constexpr void +g () +{ + const Y y; // { dg-message "originally declared" } + Y *p = const_cast(&y); + p->x.j = 99; // { dg-error "modifying a const object" } +} + +static_assert((g() , 1), ""); // { dg-error "non-constant condition" } +// { dg-message "in 'constexpr' expansion of" "" { target *-*-* } .-1 } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const3.C new file mode 100644 index 00000000000..6853775c1e2 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const3.C @@ -0,0 +1,22 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct A { + int n; + constexpr A() : n(1) { n = 2; } +}; + +struct B { + const A a; + constexpr B(bool b) { + if (b) + const_cast(a).n = 3; // { dg-error "modifying a const object" } + } +}; + +constexpr B b(false); +static_assert(b.a.n == 2, ""); + +constexpr B b2(true); // { dg-message "in .constexpr. expansion of " } +// { dg-message "originally declared" "" { target *-*-* } .-1 } +static_assert((b2.a.n, 1), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const4.C new file mode 100644 index 00000000000..8263a7cc505 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const4.C @@ -0,0 +1,17 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct A { + const int n; + constexpr A() : n(1) { } +}; +struct B { + A a; + constexpr B() { + int *p = const_cast(&a.n); + *p = 3; // { dg-error "modifying a const object" } + } +}; +constexpr B b; // { dg-message "in .constexpr. expansion of " } +// { dg-message "originally declared" "" { target *-*-* } .-1 } +static_assert((b.a.n, 1), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const5.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const5.C new file mode 100644 index 00000000000..bea54fb4fde --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const5.C @@ -0,0 +1,17 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct A { + mutable int n; + constexpr A() : n(1) { n = 2; } +}; + +struct B { + const A a; + constexpr B() { + const_cast(a).n = 3; + } +}; + +constexpr B b{}; +static_assert((b.a.n, 1), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const6.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const6.C new file mode 100644 index 00000000000..54d83b17d39 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const6.C @@ -0,0 +1,22 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct X { + mutable int j; + constexpr X() : j(0) { } +}; + +struct Y { + X x; + constexpr Y() : x{} { } +}; + +constexpr void +g () +{ + const Y y; + Y *p = const_cast(&y); + p->x.j = 99; +} + +static_assert((g(), 1), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const7.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const7.C new file mode 100644 index 00000000000..922e8ff126f --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const7.C @@ -0,0 +1,23 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct D { int n; }; + +struct C { const D d; }; + +struct A { + C c; + constexpr A() : c{} { } +}; + +struct B { + A a; + constexpr B() { + int &r = const_cast(a.c.d.n); + r = 3; // { dg-error "modifying a const object" } + } +}; + +constexpr B b{}; // { dg-message "in .constexpr. expansion of " } +// { dg-message "originally declared" "" { target *-*-* } .-1 } +static_assert((b.a.c.d.n, 1), ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const8.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const8.C new file mode 100644 index 00000000000..2b3fe793f83 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const8.C @@ -0,0 +1,23 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct B { + int i; + double d; +}; + +constexpr B bar() +{ + constexpr B b = {10,10.10}; // { dg-message "originally declared" } + B *p = const_cast(&b); + + p->i = 11; // { dg-error "modifying a const object" } + p->d = 11.11; + + return *p; +} + +void foo() +{ + constexpr B y = bar(); // { dg-message "in .constexpr. expansion of" } +} diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const9.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const9.C new file mode 100644 index 00000000000..0edec4d05cf --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-tracking-const9.C @@ -0,0 +1,23 @@ +// PR c++/91264 +// { dg-do compile { target c++14 } } + +struct B { + int i; + double d; +}; + +constexpr B bar() +{ + constexpr B b{}; // { dg-message "originally declared" } + B *p = const_cast(&b); + + p->i = 11; // { dg-error "modifying a const object" } + p->d = 11.11; + + return *p; +} + +void foo() +{ + constexpr B y = bar(); // { dg-message "in .constexpr. expansion of" } +} -- 2.30.2