https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109787
Bug ID: 109787 Summary: Warn about contract violations that can be detected at compile time Product: gcc Version: 13.1.0 Status: UNCONFIRMED Keywords: diagnostic Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: redi at gcc dot gnu.org Blocks: 87403 Target Milestone: --- struct array { int operator[](unsigned n) [[pre: n < 1]] { return 0; } }; int main() { array a; return a[1]; } When this C++ program is compiled with -fcontracts it will fail at runtime: contract violation in function array<int, 1>::operator[] at contract.cc:7: n < N terminate called without an active exception Aborted (core dumped) The front-end inserts a call to this handler when the precondition check fails: extern "C++" { void handle_contract_violation (const std::experimental::contract_violation &); } It would be nice if the middle-end could notice that the contract handler is reached on all paths, and so warn. If we can prove we're going to call the contract violation handler, then we can prove the program is wrong, and we should warn. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87403 [Bug 87403] [Meta-bug] Issues that suggest a new warning