Returning from [[noreturn]] function not rejected in constant evaluation
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
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)
Contributor guide
Research direction
Reproduce the issue's C++ example in Clang, focusing on the constexpr static_assert and [[noreturn]] call. Trace constant evaluation for a function that returns despite [[noreturn]], then add a regression test showing that the static_assert is rejected; done when the behavior is diagnosed consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100