diff options
author | Jason Merrill <jason@redhat.com> | 2025-08-21 13:52:25 -0400 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2025-08-21 13:52:48 -0400 |
commit | 70f33ad677e6350a724b56d4cb766480ed8367fc (patch) | |
tree | 7bc7b74a07b9a753695e11127dedccd9fe576af0 | |
parent | 4196389cd2dd0e4f612df4a664be9164cbc50989 (diff) | |
download | gcc-70f33ad677e6350a724b56d4cb766480ed8367fc.zip gcc-70f33ad677e6350a724b56d4cb766480ed8367fc.tar.gz gcc-70f33ad677e6350a724b56d4cb766480ed8367fc.tar.bz2 |
c++: constexpr clobber of const [PR121068]
Since r16-3022, 20_util/variant/102912.cc was failing in C++20 and above due
to wrong errors about destruction modifying a const object; destruction is
OK.
PR c++/121068
gcc/cp/ChangeLog:
* constexpr.cc (cxx_eval_store_expression): Allow clobber of a const
object.
gcc/testsuite/ChangeLog:
* g++.dg/cpp2a/constexpr-dtor18.C: New test.
-rw-r--r-- | gcc/cp/constexpr.cc | 3 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp2a/constexpr-dtor18.C | 26 |
2 files changed, 29 insertions, 0 deletions
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 223240a..e5438b2 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -7659,6 +7659,9 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, ctx->global->destroy_value (object); return void_node; } + + /* Ending the lifetime of a const object is OK. */ + const_object_being_modified = NULL_TREE; } type = TREE_TYPE (object); diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor18.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor18.C new file mode 100644 index 0000000..dc0d174 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor18.C @@ -0,0 +1,26 @@ +// PR c++/121068 +// { dg-do compile { target c++20 } } + +template <class T> +constexpr void +destroy_at (T* p) { p->~T(); } + +template <class T> +struct V { + union { + unsigned char buf[sizeof (T)]; + const T ct; + }; + bool active; + constexpr V(): active (false) {} + constexpr V(T t): ct (t), active (true) { } + constexpr ~V() { if (active) destroy_at (&ct); } +}; + +constexpr char f() +{ + const V<int> vi {42}; + return vi.ct; +} + +static_assert (f() == 42); |