Issue 64635
Summary Clang should warn for implementation-defined constant expressions
Labels new issue
Assignees
Reporter Eisenwave
    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]
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to