llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang accepts uninitialized local in constexpr function

Open vrukesh opened this issue 10 months ago • 3 comments

What code / commands /steps will reproduce the problem? Compile the below sample code.

template <class T>
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.

<source>:8:26: error: 'constexpr T func(T) [with T = double]' called in a constant expression
    8 | constexpr double v = func(1.5);
      |                      ~~~~^~~~~
<source>:2:13: note: 'constexpr T func(T) [with T = double]' is not usable as a 'constexpr' function because:
    2 | constexpr T func(T x) {
      |             ^~~~
<source>:3:7: error: uninitialized variable 'a' in 'constexpr' context
    3 |     T a;
      |       ^
Compiler returned: 1

vrukesh avatar Jan 15 '26 11:01 vrukesh

@llvm/issue-subscribers-clang-frontend

Author: Vrukesh Panse (vrukesh)

**What code / commands /steps will reproduce the problem?** Compile the below sample code.
template &lt;class T&gt;
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.

&lt;source&gt;:8:26: error: 'constexpr T func(T) [with T = double]' called in a constant expression
    8 | constexpr double v = func(1.5);
      |                      ~~~~^~~~~
&lt;source&gt;:2:13: note: 'constexpr T func(T) [with T = double]' is not usable as a 'constexpr' function because:
    2 | constexpr T func(T x) {
      |             ^~~~
&lt;source&gt;:3:7: error: uninitialized variable 'a' in 'constexpr' context
    3 |     T a;
      |       ^
Compiler returned: 1

llvmbot avatar Jan 15 '26 15:01 llvmbot

This was made valid by P1331, which doesn't appear to have been moved in DR status. Looks like the mistake was here: we still need the "uninitialized" diagnostic here for C++17 and earlier.

zygoloid avatar Jan 15 '26 22:01 zygoloid

This was made valid by P1331, which doesn't appear to have been moved in DR status. Looks like the mistake was here: we still need the "uninitialized" diagnostic here for C++17 and earlier.

I was just looking at this, it has C++23 status: https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2558

shafik avatar Jan 15 '26 22:01 shafik