<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64635>64635</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang should warn for implementation-defined constant expressions
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Eisenwave
</td>
</tr>
</table>
<pre>
The following code doesn't compile for all major compilers, but receives no warnings from `-Wall -Wpedantic -Wextra`:
```c
const float x = 0;
const float y = x;
```
This code isn't portable, even with the C23 relaxations. [GCC and clang allow it, but MSVC rejects it](https://godbolt.org/z/h45qsx8aW). It is technically legal C, but the portability issues at least deserve a warning.
## Relevant Wording
> More latitude is permitted for constant expressions in initializers. Such a constant expression shall
be, or evaluate to, one of the following:
> - a named constant,
> - a compound literal constant,
> - an arithmetic constant expression,
> - a null pointer constant,
> - an address constant, or
> - an address constant for a complete object type plus or minus an integer constant expression
\- [**6.6 Constant expressions** p9][c23]
For the initialization of `y`, obviously only the first and third bullets can apply. *Arithmetic constant expressions* are built on top of *named constants*, so the only question is whether `x` is a named constant.
> An identifier that is:
> - an enumeration constant,
> - a predefined constant, or
> - declared with storage-class specifier `constexpr` and has an object type,
>
>is a *named constant*, as is [...]
\- [**6.6 Constant expressions** p7][c23]
`x` is not a named constant, because it is not `constexpr`. However, this code is not ill-formed, merely implementation-defined:
> An implementation may accept other forms of constant expressions; however, they are not an integer constant expression.
\- [**6.6 Constant expressions** p14][c23]
[c23]: https://open-std.org/JTC1/SC22/WG14/www/docs/n3096.pdf#148
## Suggested Solution
Clang should issue a warning, along the lines of:
```
initialization of 'y' relies on implementation-defined constant expressions [-Wnon-portable-constexpr]
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVk2P4roS_TVmU0oUHMLHggXQw7z3pNm8Hl3WTlwkHjl2xq7wMb_-yk5DA03P1Vyp1cR2uVx1zimXhfeqN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