https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90666
Bug ID: 90666 Summary: Warn if an UB was met during constexpr evaluation attempt Product: gcc Version: 10.0 Status: UNCONFIRMED Keywords: diagnostic Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: antoshkka at gmail dot com Target Milestone: --- Consider the example: constexpr int test() { const char* from = "wow"; char dest[1] = {*from}; // assignment to dereferenced one-past-the-end pointer dest[1] = 0; return 0; } const auto r = test(); `test()` function is a constexpr function, yet any attempt to call it causes UB. It would be very helpful to have a warning for a constexpr evaluation attempt that met an UB and fell back to runtime evaluation. Note, that such warning would be extremely helpful for contracts. It would allow to detect contract violations at compile time: constexpr int impl(int num) [[pre: num > 0]] { return num + 42; } auto test() { // Core constant expression: const auto f0 = impl(1); // Runtime call to __on_contract_violation. // Warning would be very helpful. const auto f1 = impl(0); }