c++: Track lifetimes in constant evaluation [PR70331,PR96630,PR98675]
commit9fdbd7d6fa5e0a76898dd66658934e3184111680
authorNathaniel Shead <nathanieloshead@gmail.com>
Sat, 22 Jul 2023 15:15:14 +0000 (23 01:15 +1000)
committerJason Merrill <jason@redhat.com>
Wed, 26 Jul 2023 01:45:10 +0000 (25 21:45 -0400)
tree8091b880086e935ea1c8e884eb07b8fb8a3e6842
parentb8266af71c19a0bd7db4d08c8d2ee3c33214508c
c++: Track lifetimes in constant evaluation [PR70331,PR96630,PR98675]

This adds rudimentary lifetime tracking in C++ constexpr contexts,
allowing the compiler to report errors with using values after their
backing has gone out of scope. We don't yet handle other ways of
accessing values outside their lifetime (e.g. following explicit
destructor calls).

PR c++/96630
PR c++/98675
PR c++/70331

gcc/cp/ChangeLog:

* constexpr.cc (constexpr_global_ctx::is_outside_lifetime): New
function.
(constexpr_global_ctx::get_value): Don't return expired values.
(constexpr_global_ctx::get_value_ptr): Likewise.
(constexpr_global_ctx::remove_value): Mark value outside
lifetime.
(outside_lifetime_error): New function.
(cxx_eval_call_expression): No longer track save_exprs.
(cxx_eval_loop_expr): Likewise.
(cxx_eval_constant_expression): Add checks for outside lifetime
values. Remove local variables at end of bind exprs, and
temporaries after cleanup points.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/constexpr-lifetime1.C: New test.
* g++.dg/cpp1y/constexpr-lifetime2.C: New test.
* g++.dg/cpp1y/constexpr-lifetime3.C: New test.
* g++.dg/cpp1y/constexpr-lifetime4.C: New test.
* g++.dg/cpp1y/constexpr-lifetime5.C: New test.
* g++.dg/cpp1y/constexpr-lifetime6.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-lifetime6.C [new file with mode: 0644]