C++: Diagnose return from [[noreturn]] function in constant evaluation.
GCC now diagnoses calls to [[noreturn]] functions that 'return' during constant evaluation.
GCC now correctly diagnoses undefined behavior when a function marked with [[noreturn]] actually returns during constant evaluation. Previously, this scenario was not explicitly checked, potentially leading to incorrect code generation or confusing runtime behavior. New test cases in the testsuite verify this new diagnostic check.
In Details
The cxx_eval_call_expression function in gcc/cp/constexpr.cc has been enhanced to detect and diagnose calls to [[noreturn]] functions that do not terminate execution as expected during constant evaluation. This addresses undefined behavior that was previously not caught by the compiler, ensuring stricter adherence to the [[noreturn]] attribute's semantics.
- [[noreturn]]
- A function attribute in C++ that suggests the function will not return to its caller. If a
[[noreturn]]function does return, it results in undefined behavior. - Constant Evaluation
- The process where the compiler computes the value of an expression at compile time, typically for
constexprvariables or functions, rather than at runtime. - Undefined Behavior (UB)
- Behavior for which the C++ standard imposes no requirements. Programs exhibiting UB can fail in unpredictable ways, crash, or produce incorrect results.