https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126354
Bug ID: 126354
Summary: Returning from [[noreturn]] function not rejected in
constant evaluation
Product: gcc
Version: 16.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: gongke at ios dot ac.cn
Target Milestone: ---
Consider the following code:
```cpp
[[noreturn]] constexpr void f(int i) {
if (i == 0)
throw 42;
// otherwise it returns, which is UB
}
constexpr int g() {
f(1);
return 0; // unreachable
}
int main() {
static_assert(g() == 0, "This should cause a compile error due to UB");
(void)g(); // avoid warning about unused function
return 0;
}
```
>From C++20 final draft N4861 [dcl.attr.noreturn]/2,
> If a function f is called where f was previously declared with the noreturn
> attribute and f eventually returns, the behavior is undefined.
So this code involves an undefined behavior during constant evaluation and
should be rejected.
Neither GCC nor Clang rejects this. [godbolt](https://godbolt.org/z/Gjj5hv4qr)