public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107104] New: semantics of __builtin_constant_p within static_assert and return value
@ 2022-09-30 16:43 me at inclyc dot cn
2022-10-03 14:48 ` [Bug c++/107104] " h2+bugs at fsfe dot org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: me at inclyc dot cn @ 2022-09-30 16:43 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107104
Bug ID: 107104
Summary: semantics of __builtin_constant_p within static_assert
and return value
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: me at inclyc dot cn
Target Milestone: ---
https://godbolt.org/z/oz4e7bobT
https://godbolt.org/z/hE6x9G49q
illustrate different occasions about this issue.
Consider passing the same operand __builtin_constant_p in static assertions and
return value, expression with in static assertion passed, but the same
expression evaluates to 0 as the return value.
Are there some different scopes / contexts that causes different result?
Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91158
Link: https://github.com/llvm/llvm-project/issues/58078
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/107104] semantics of __builtin_constant_p within static_assert and return value
2022-09-30 16:43 [Bug c++/107104] New: semantics of __builtin_constant_p within static_assert and return value me at inclyc dot cn
@ 2022-10-03 14:48 ` h2+bugs at fsfe dot org
2022-10-03 14:54 ` redi at gcc dot gnu.org
2022-11-12 12:04 ` yann at droneaud dot fr
2 siblings, 0 replies; 4+ messages in thread
From: h2+bugs at fsfe dot org @ 2022-10-03 14:48 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107104
Hannes Hauswedell <h2+bugs at fsfe dot org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |h2+bugs at fsfe dot org
--- Comment #1 from Hannes Hauswedell <h2+bugs at fsfe dot org> ---
It seems that __builtin_constant_p does not indicate whether something *can be*
a constant but whether *it is* a constant.
If you evaluate it in a non-const-context, the expression passed as argument
may or may not be evaluated at compile-time, so the value of
__builtin_constant_p may or may not be 1.
This example illustrates the behaviour:
```cpp
constexpr void foobar() {} // → test() returns 0 or 1
//consteval void foobar() {} // → test() returns 1
#define TEST_EXPR (foobar(), 0)
int test() {
static_assert(__builtin_constant_p(TEST_EXPR));
return __builtin_constant_p(TEST_EXPR);
}
```
To enforce evaluation in a const-context, you can define a macro like this:
#define IS_CONSTEXPR(...) std::integral_constant<bool,
__builtin_constant_p((__VA_ARGS__, 0))>::value
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/107104] semantics of __builtin_constant_p within static_assert and return value
2022-09-30 16:43 [Bug c++/107104] New: semantics of __builtin_constant_p within static_assert and return value me at inclyc dot cn
2022-10-03 14:48 ` [Bug c++/107104] " h2+bugs at fsfe dot org
@ 2022-10-03 14:54 ` redi at gcc dot gnu.org
2022-11-12 12:04 ` yann at droneaud dot fr
2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2022-10-03 14:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107104
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Hannes Hauswedell from comment #1)
> It seems that __builtin_constant_p does not indicate whether something *can
> be* a constant but whether *it is* a constant.
Correct.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/107104] semantics of __builtin_constant_p within static_assert and return value
2022-09-30 16:43 [Bug c++/107104] New: semantics of __builtin_constant_p within static_assert and return value me at inclyc dot cn
2022-10-03 14:48 ` [Bug c++/107104] " h2+bugs at fsfe dot org
2022-10-03 14:54 ` redi at gcc dot gnu.org
@ 2022-11-12 12:04 ` yann at droneaud dot fr
2 siblings, 0 replies; 4+ messages in thread
From: yann at droneaud dot fr @ 2022-11-12 12:04 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107104
Yann Droneaud <yann at droneaud dot fr> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |yann at droneaud dot fr
--- Comment #3 from Yann Droneaud <yann at droneaud dot fr> ---
I'm experiencing the same issue:
#include <assert.h>
void a(int v)
{
static_assert(__builtin_constant_p(v) && v == -1, "failure");
}
GCC 13 and below complains:
<source>: In function 'a':
<source>:16:43: error: expression in static assertion is not constant
16 | static_assert(__builtin_constant_p(v) && v == -1, "failure");
| ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
Compiler returned: 1
clang current trunk, eg above 15.0, seems to finally get it right:
<source>:16:5: error: static assertion failed due to requirement
'__builtin_constant_p(v) && v == -1': failure
static_assert(__builtin_constant_p(v) && v == -1, "failure");
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/assert.h:143:24: note: expanded from macro 'static_assert'
# define static_assert _Static_assert
^
1 error generated.
Compiler returned: 1
see https://godbolt.org/z/KKn6xTG8a
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-11-12 12:04 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-30 16:43 [Bug c++/107104] New: semantics of __builtin_constant_p within static_assert and return value me at inclyc dot cn
2022-10-03 14:48 ` [Bug c++/107104] " h2+bugs at fsfe dot org
2022-10-03 14:54 ` redi at gcc dot gnu.org
2022-11-12 12:04 ` yann at droneaud dot fr
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).