Clang accepts uninitialized local in constexpr function
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
**What code / commands /steps will reproduce the problem?**
Compile the below sample code.
```c++
template
constexpr T func(T x) {
T a;
a = x;
return a;
}
constexpr double v = func(1.5);
int main() {}
```
**Compiler Explorer link:**
https://godbolt.org/z/K5v34WK9G
**What is the expected result?**
As per C++17 standard,
_The definition of a constexpr function shall satisfy the following requirements:
* it shall not be virtual;
* its return type shall be a literal type;
* each of its parameter types shall be a literal type;
* its function-body shall be = delete, = default, or a compound-statement that does not contain
* an asm-definition,
* a goto statement,
* an identifier label,
* a try-block, or
* a definition of a variable of non-literal type or of static or thread storage duration or for which no initialization is performed._
**What happens instead?**
Clang incorrectly compiles the code, without any diagnostic warning or error.
To meet the C++17 standard, Clang must throw diagnostic error.
GCC throws diagnostic compilation error.
```console
:8:26: error: 'constexpr T func(T) [with T = double]' called in a constant expression
8 | constexpr double v = func(1.5);
| ~~~~^~~~~
:2:13: note: 'constexpr T func(T) [with T = double]' is not usable as a 'constexpr' function because:
2 | constexpr T func(T x) {
| ^~~~
:3:7: error: uninitialized variable 'a' in 'constexpr' context
3 | T a;
| ^
Compiler returned: 1
```
Contributor guide
Research direction
Start by compiling the provided C++17 sample and comparing Clang's behavior with the GCC diagnostic shown in the issue. Trace the constexpr validation and diagnostic path for the uninitialized local variable; done means Clang rejects the sample with a diagnostic as required by C++17.
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
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100