public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107861] New: C++ static_assert() does not honor -fwrapv, leading to compilation error
@ 2022-11-24 17:25 markus at oberhumer dot com
2022-11-24 17:57 ` [Bug c++/107861] " pinskia at gcc dot gnu.org
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: markus at oberhumer dot com @ 2022-11-24 17:25 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107861
Bug ID: 107861
Summary: C++ static_assert() does not honor -fwrapv, leading to
compilation error
Product: gcc
Version: 12.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: markus at oberhumer dot com
Target Milestone: ---
C++ static_assert() does not honor -fwrapv, leading to compilation error
godbolt link: https://gcc.godbolt.org/z/Po8vc5Kex
$ g++ --version
g++ (GCC) 12.2.1 20220819 (Red Hat 12.2.1-
$ g++ -fwrapv -Wno-overflow -c test.cpp
test.cpp:7:34: error: non-constant condition for static assertion
$ cat test.cpp
// compile with: g++ -fwrapv -Wno-overflow -c test.cpp
//
// extra rant: -Wno-overflow should not be needed here!
#include <limits.h>
#define wrap_inc(x) ((x) + 1 < (x))
constexpr int int_max = INT_MAX;
bool b0 = wrap_inc(int_max);
const bool b1 = wrap_inc(int_max);
constexpr bool b2 = wrap_inc(int_max);
static_assert(b2, ""); // works
// error: non-constant condition for static assertion
static_assert(wrap_inc(int_max), "");
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/107861] C++ static_assert() does not honor -fwrapv, leading to compilation error
2022-11-24 17:25 [Bug c++/107861] New: C++ static_assert() does not honor -fwrapv, leading to compilation error markus at oberhumer dot com
@ 2022-11-24 17:57 ` pinskia at gcc dot gnu.org
2022-11-24 17:57 ` pinskia at gcc dot gnu.org
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-24 17:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107861
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Right, this is by design and I don't think it is a bug. The reasoning is the
C++ constant expressions have a way of requiring you to having to figure out if
it was going to overflow and cause different behavior (of well defined code) at
compile time.
There is most likely a way to get a subsitutation failure with the overflow
too.
I tried with C++20 concepts but I am not so good at it.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/107861] C++ static_assert() does not honor -fwrapv, leading to compilation error
2022-11-24 17:25 [Bug c++/107861] New: C++ static_assert() does not honor -fwrapv, leading to compilation error markus at oberhumer dot com
2022-11-24 17:57 ` [Bug c++/107861] " pinskia at gcc dot gnu.org
@ 2022-11-24 17:57 ` pinskia at gcc dot gnu.org
2022-11-24 18:18 ` markus at oberhumer dot com
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-24 17:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107861
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note clang rejects even just:
#include <limits.h>
#define wrap_inc(x) ((x) + 1 < (x))
constexpr int int_max = INT_MAX;
bool b0 = wrap_inc(int_max);
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/107861] C++ static_assert() does not honor -fwrapv, leading to compilation error
2022-11-24 17:25 [Bug c++/107861] New: C++ static_assert() does not honor -fwrapv, leading to compilation error markus at oberhumer dot com
2022-11-24 17:57 ` [Bug c++/107861] " pinskia at gcc dot gnu.org
2022-11-24 17:57 ` pinskia at gcc dot gnu.org
@ 2022-11-24 18:18 ` markus at oberhumer dot com
2022-11-24 19:29 ` redi at gcc dot gnu.org
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: markus at oberhumer dot com @ 2022-11-24 18:18 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107861
--- Comment #3 from Markus F.X.J. Oberhumer <markus at oberhumer dot com> ---
Indeed.
And just for reference I had also reported this as clang bug in
https://github.com/llvm/llvm-project/issues/59195
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/107861] C++ static_assert() does not honor -fwrapv, leading to compilation error
2022-11-24 17:25 [Bug c++/107861] New: C++ static_assert() does not honor -fwrapv, leading to compilation error markus at oberhumer dot com
` (2 preceding siblings ...)
2022-11-24 18:18 ` markus at oberhumer dot com
@ 2022-11-24 19:29 ` redi at gcc dot gnu.org
2022-11-24 19:34 ` redi at gcc dot gnu.org
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-24 19:29 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107861
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Andrew is right. The C++ standard says this is ill-formed, the -fwrapv option
isn't allowed to change that.
The option means that runtime overflow is well-defined instead of undefined,
but that doesn't change static compile-time behaviour that is ill-formed.
Overflow in constant expressions and template arguments is still an error, even
if the arithmetic would wrap at runtime.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/107861] C++ static_assert() does not honor -fwrapv, leading to compilation error
2022-11-24 17:25 [Bug c++/107861] New: C++ static_assert() does not honor -fwrapv, leading to compilation error markus at oberhumer dot com
` (3 preceding siblings ...)
2022-11-24 19:29 ` redi at gcc dot gnu.org
@ 2022-11-24 19:34 ` redi at gcc dot gnu.org
2022-11-24 19:43 ` markus at oberhumer dot com
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-24 19:34 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107861
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
To expand on that, the standard allows compilers to do anything for undefined
behaviour, including making it valid with well-defined semantics. So wrapping
for undefined overflow is a conforming extension. But the outcome of overflow
in a constant expression is not undefined, so the implementation isn't allowed
to choose the behaviour. It has to be ill-formed (although maybe that could be
a pedantic warning when -fwrapv is used).
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/107861] C++ static_assert() does not honor -fwrapv, leading to compilation error
2022-11-24 17:25 [Bug c++/107861] New: C++ static_assert() does not honor -fwrapv, leading to compilation error markus at oberhumer dot com
` (4 preceding siblings ...)
2022-11-24 19:34 ` redi at gcc dot gnu.org
@ 2022-11-24 19:43 ` markus at oberhumer dot com
2022-11-24 20:56 ` redi at gcc dot gnu.org
2022-11-24 21:49 ` redi at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: markus at oberhumer dot com @ 2022-11-24 19:43 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107861
--- Comment #6 from Markus F.X.J. Oberhumer <markus at oberhumer dot com> ---
Please note that I'm explicitly using "int_max" and not "INT_MAX", and I'd
appreciate if you could give me a link where the standard says this is
"ill-formed". Thanks!
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/107861] C++ static_assert() does not honor -fwrapv, leading to compilation error
2022-11-24 17:25 [Bug c++/107861] New: C++ static_assert() does not honor -fwrapv, leading to compilation error markus at oberhumer dot com
` (5 preceding siblings ...)
2022-11-24 19:43 ` markus at oberhumer dot com
@ 2022-11-24 20:56 ` redi at gcc dot gnu.org
2022-11-24 21:49 ` redi at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-24 20:56 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107861
--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Markus F.X.J. Oberhumer from comment #6)
> Please note that I'm explicitly using "int_max" and not "INT_MAX",
It's a constexpr variable with the same value, so that makes no difference
whatsoever.
> and I'd
> appreciate if you could give me a link where the standard says this is
> "ill-formed". Thanks!
The condition is a static assert must be a constant-expression (C++20 9.1
[dcl.pre] paragraph 6).
int_max+1 is not a core constant (C++20 7.7 [expr.const] paragraph 5, bullet
5.7), so is not usable as the condition in a static_assert, so the program is
ill-formed.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/107861] C++ static_assert() does not honor -fwrapv, leading to compilation error
2022-11-24 17:25 [Bug c++/107861] New: C++ static_assert() does not honor -fwrapv, leading to compilation error markus at oberhumer dot com
` (6 preceding siblings ...)
2022-11-24 20:56 ` redi at gcc dot gnu.org
@ 2022-11-24 21:49 ` redi at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-24 21:49 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107861
--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #7)
> int_max+1 is not a core constant (C++20 7.7 [expr.const] paragraph 5, bullet
oops, s/core constant/core constant expression/
> 5.7), so is not usable as the condition in a static_assert, so the program
> is ill-formed.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2022-11-24 21:49 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-24 17:25 [Bug c++/107861] New: C++ static_assert() does not honor -fwrapv, leading to compilation error markus at oberhumer dot com
2022-11-24 17:57 ` [Bug c++/107861] " pinskia at gcc dot gnu.org
2022-11-24 17:57 ` pinskia at gcc dot gnu.org
2022-11-24 18:18 ` markus at oberhumer dot com
2022-11-24 19:29 ` redi at gcc dot gnu.org
2022-11-24 19:34 ` redi at gcc dot gnu.org
2022-11-24 19:43 ` markus at oberhumer dot com
2022-11-24 20:56 ` redi at gcc dot gnu.org
2022-11-24 21:49 ` redi at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).