* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
@ 2024-04-01 9:40 ` fchelnokov at gmail dot com
2024-04-01 13:54 ` pinskia at gcc dot gnu.org
` (8 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: fchelnokov at gmail dot com @ 2024-04-01 9:40 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
--- Comment #1 from Fedor Chelnokov <fchelnokov at gmail dot com> ---
Probably related:
```
#include <bit>
struct A { int a: 7; };
static_assert( 1 == std::bit_cast<A>(std::bit_cast<int>(A{1})).a );
```
It looks valid and accepted by MSVC, but GCC prints:
error: '__builtin_bit_cast' accessing uninitialized byte at offset 0
Online demo: https://gcc.godbolt.org/z/3W5onY955
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
2024-04-01 9:40 ` [Bug c++/114537] " fchelnokov at gmail dot com
@ 2024-04-01 13:54 ` pinskia at gcc dot gnu.org
2024-04-03 15:10 ` jakub at gcc dot gnu.org
` (7 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-01 13:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Fedor Chelnokov from comment #1)
> Probably related:
> ```
> #include <bit>
>
> struct A { int a: 7; };
>
> static_assert( 1 == std::bit_cast<A>(std::bit_cast<int>(A{1})).a );
> ```
> It looks valid and accepted by MSVC, but GCC prints:
> error: '__builtin_bit_cast' accessing uninitialized byte at offset 0
>
> Online demo: https://gcc.godbolt.org/z/3W5onY955
That is unrelated and gcc is actually correct there see pr 99637.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
2024-04-01 9:40 ` [Bug c++/114537] " fchelnokov at gmail dot com
2024-04-01 13:54 ` pinskia at gcc dot gnu.org
@ 2024-04-03 15:10 ` jakub at gcc dot gnu.org
2024-04-04 8:48 ` cvs-commit at gcc dot gnu.org
` (6 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-04-03 15:10 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |ASSIGNED
Last reconfirmed| |2024-04-03
Ever confirmed|0 |1
Assignee|unassigned at gcc dot gnu.org |jakub at gcc dot gnu.org
--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 57863
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57863&action=edit
gcc14-pr114537.patch
Untested fix.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
` (2 preceding siblings ...)
2024-04-03 15:10 ` jakub at gcc dot gnu.org
@ 2024-04-04 8:48 ` cvs-commit at gcc dot gnu.org
2024-04-05 18:44 ` fchelnokov at gmail dot com
` (5 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-04-04 8:48 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:
https://gcc.gnu.org/g:1baec8deb014b8a7da58879a407a4c00cdeb5a09
commit r14-9784-g1baec8deb014b8a7da58879a407a4c00cdeb5a09
Author: Jakub Jelinek <jakub@redhat.com>
Date: Thu Apr 4 10:47:52 2024 +0200
fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537]
The following testcase is incorrectly rejected. The problem is that
for bit-fields native_encode_initializer expects the corresponding
CONSTRUCTOR elt value must be INTEGER_CST, but that isn't the case
here, it is wrapped into NON_LVALUE_EXPR by maybe_wrap_with_location.
We could STRIP_ANY_LOCATION_WRAPPER as well, but as all we are looking for
is INTEGER_CST inside, just looking through NON_LVALUE_EXPR seems easier.
2024-04-04 Jakub Jelinek <jakub@redhat.com>
PR c++/114537
* fold-const.cc (native_encode_initializer): Look through
NON_LVALUE_EXPR if val is INTEGER_CST.
* g++.dg/cpp2a/bit-cast16.C: New test.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
` (3 preceding siblings ...)
2024-04-04 8:48 ` cvs-commit at gcc dot gnu.org
@ 2024-04-05 18:44 ` fchelnokov at gmail dot com
2024-04-21 4:08 ` cvs-commit at gcc dot gnu.org
` (4 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: fchelnokov at gmail dot com @ 2024-04-05 18:44 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
--- Comment #5 from Fedor Chelnokov <fchelnokov at gmail dot com> ---
Sorry, in above example I had to use `unsigned char` type:
```
#include <bit>
struct A { unsigned char a : 7; };
static_assert( std::bit_cast<A>(std::bit_cast<unsigned char>(A{1})).a == 1 );
```
This program even after the fix in trunk, prints the same error as in the
original post:
<source>:5:32: in 'constexpr' expansion of 'std::bit_cast<A, unsigned
char>(std::bit_cast<unsigned char, A>(A{1}))'
/opt/compiler-explorer/gcc-trunk-20240405/include/c++/14.0.1/bit:94:33: sorry,
unimplemented: '__builtin_bit_cast' cannot be constant evaluated because the
argument cannot be encoded
94 | return __builtin_bit_cast(_To, __from);
Online demo: https://gcc.godbolt.org/z/eqrsfGEnT
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
` (4 preceding siblings ...)
2024-04-05 18:44 ` fchelnokov at gmail dot com
@ 2024-04-21 4:08 ` cvs-commit at gcc dot gnu.org
2024-06-11 10:37 ` cvs-commit at gcc dot gnu.org
` (3 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-04-21 4:08 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:
https://gcc.gnu.org/g:a297f9bbb9611414fe48f6d61a8829bf5808bd2c
commit r13-8626-ga297f9bbb9611414fe48f6d61a8829bf5808bd2c
Author: Jakub Jelinek <jakub@redhat.com>
Date: Thu Apr 4 10:47:52 2024 +0200
fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537]
The following testcase is incorrectly rejected. The problem is that
for bit-fields native_encode_initializer expects the corresponding
CONSTRUCTOR elt value must be INTEGER_CST, but that isn't the case
here, it is wrapped into NON_LVALUE_EXPR by maybe_wrap_with_location.
We could STRIP_ANY_LOCATION_WRAPPER as well, but as all we are looking for
is INTEGER_CST inside, just looking through NON_LVALUE_EXPR seems easier.
2024-04-04 Jakub Jelinek <jakub@redhat.com>
PR c++/114537
* fold-const.cc (native_encode_initializer): Look through
NON_LVALUE_EXPR if val is INTEGER_CST.
* g++.dg/cpp2a/bit-cast16.C: New test.
(cherry picked from commit 1baec8deb014b8a7da58879a407a4c00cdeb5a09)
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
` (5 preceding siblings ...)
2024-04-21 4:08 ` cvs-commit at gcc dot gnu.org
@ 2024-06-11 10:37 ` cvs-commit at gcc dot gnu.org
2024-06-11 10:57 ` jakub at gcc dot gnu.org
` (2 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-06-11 10:37 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:
https://gcc.gnu.org/g:42afabb838d511f5feb150bfa4e68b5880aae1fa
commit r12-10518-g42afabb838d511f5feb150bfa4e68b5880aae1fa
Author: Jakub Jelinek <jakub@redhat.com>
Date: Thu Apr 4 10:47:52 2024 +0200
fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537]
The following testcase is incorrectly rejected. The problem is that
for bit-fields native_encode_initializer expects the corresponding
CONSTRUCTOR elt value must be INTEGER_CST, but that isn't the case
here, it is wrapped into NON_LVALUE_EXPR by maybe_wrap_with_location.
We could STRIP_ANY_LOCATION_WRAPPER as well, but as all we are looking for
is INTEGER_CST inside, just looking through NON_LVALUE_EXPR seems easier.
2024-04-04 Jakub Jelinek <jakub@redhat.com>
PR c++/114537
* fold-const.cc (native_encode_initializer): Look through
NON_LVALUE_EXPR if val is INTEGER_CST.
* g++.dg/cpp2a/bit-cast16.C: New test.
(cherry picked from commit 1baec8deb014b8a7da58879a407a4c00cdeb5a09)
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
` (6 preceding siblings ...)
2024-06-11 10:37 ` cvs-commit at gcc dot gnu.org
@ 2024-06-11 10:57 ` jakub at gcc dot gnu.org
2024-06-20 13:22 ` cvs-commit at gcc dot gnu.org
2024-06-20 13:39 ` jakub at gcc dot gnu.org
9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-06-11 10:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Should be fixed for 12.4+ and 13.4+ too.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
` (7 preceding siblings ...)
2024-06-11 10:57 ` jakub at gcc dot gnu.org
@ 2024-06-20 13:22 ` cvs-commit at gcc dot gnu.org
2024-06-20 13:39 ` jakub at gcc dot gnu.org
9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-06-20 13:22 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
--- Comment #9 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:
https://gcc.gnu.org/g:5d6b1d8ffc4828874ecdb1cde1b794bc3b8d35af
commit r11-11501-g5d6b1d8ffc4828874ecdb1cde1b794bc3b8d35af
Author: Jakub Jelinek <jakub@redhat.com>
Date: Thu Apr 4 10:47:52 2024 +0200
fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537]
The following testcase is incorrectly rejected. The problem is that
for bit-fields native_encode_initializer expects the corresponding
CONSTRUCTOR elt value must be INTEGER_CST, but that isn't the case
here, it is wrapped into NON_LVALUE_EXPR by maybe_wrap_with_location.
We could STRIP_ANY_LOCATION_WRAPPER as well, but as all we are looking for
is INTEGER_CST inside, just looking through NON_LVALUE_EXPR seems easier.
2024-04-04 Jakub Jelinek <jakub@redhat.com>
PR c++/114537
* fold-const.c (native_encode_initializer): Look through
NON_LVALUE_EXPR if val is INTEGER_CST.
* g++.dg/cpp2a/bit-cast16.C: New test.
(cherry picked from commit 1baec8deb014b8a7da58879a407a4c00cdeb5a09)
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/114537] bit_cast does not work NSDMI of bitfields
2024-03-31 19:33 [Bug c++/114537] New: bit_cast does not work NSDMI of bitfields pinskia at gcc dot gnu.org
` (8 preceding siblings ...)
2024-06-20 13:22 ` cvs-commit at gcc dot gnu.org
@ 2024-06-20 13:39 ` jakub at gcc dot gnu.org
9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-06-20 13:39 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114537
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |FIXED
Target Milestone|--- |11.5
Status|ASSIGNED |RESOLVED
--- Comment #10 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 11.5 as well.
^ permalink raw reply [flat|nested] 11+ messages in thread