Bug 90666 - Warn if an UB was met during constexpr evaluation attempt
Summary: Warn if an UB was met during constexpr evaluation attempt
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2019-05-29 12:48 UTC by Antony Polukhin
Modified: 2023-01-25 16:34 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-06-10 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Antony Polukhin 2019-05-29 12:48:15 UTC
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); 

}