gnu/gcc/a5e5c22ad4862846ea705d43c640ca18cc9dd78a c++: Don't cache calls which rethrow etc. [PR126508]
The first 3 testcase below are miscompiled, we happily cache
calls during constant evaluation which don't depend just on their
arguments, but also on the current exceptions (uncaught or caught).
If we decide to cache such functions and then try to evaluate them
with different uncaught/caught exceptions (or none), we can get wrong
results.
We already don't cache calls which allocate and don't free all heap
allocations, or free some heap allocations they haven't allocated,
or which call (right now any) metafunctions, or have exited through
exception, or aren't constant.
This patch just adds the rethrow/__builtin_uncaught_exceptions/
__builtin_current_exception calls to the set of non-cacheable operations
(to be precise, e.g. rethrow would be safe to cache if we can prove
that the current exception was always thrown from within that function,
ditto __builtin_current_exception, but it is hard to figure out).
The last testcase attempts to check if we don't need something similar
also for __builtin_eh_ptr_adjust_ref, but the call to foo for some reason
isn't cached and so I don't have a proof we need to handle it too.
2026-07-31 Jakub Jelinek <jakub@redhat.com>
PR c++/126508
* constexpr.cc (cxx_eval_cxa_builtin_fn): Avoid caching
calls which rethrow or call __builtin_uncaught_exceptions
or __builtin_current_exception.
* g++.dg/cpp26/constexpr-eh20.C: New test.
* g++.dg/cpp26/constexpr-eh21.C: New test.
* g++.dg/cpp26/constexpr-eh22.C: New test.
* g++.dg/cpp26/constexpr-eh23.C: New test.
5 files changed