[Bug c/95213] New: GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables)

2020-05-19 Thread in-gcc at baka dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95213

Bug ID: 95213
   Summary: GCC -Werror=conversion error when assigning to a
bitfield (when mixing constants and variables)
   Product: gcc
   Version: 11.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: in-gcc at baka dot org
  Target Milestone: ---

When trying to assign to a bitfield with -Werror=conversion, having a constant
mix with a variable causes a "conversion from 'unsigned int' to 'unsigned
int:24' may change value", where as alternate formulations with two constants
or two variables work fine.  This error seems to be present on every version of
gcc ever (the ones I tried since 4.9.1, at least one in each major version, all
did the same thing).

Watch it fail on godbolt: https://gcc.godbolt.org/z/vavr-e


struct foo { unsigned int a:8; unsigned int b:24; };
void bar(struct foo num, unsigned int x) {
num.b = (5U << 1) | (1 & 1);
unsigned int z = 5U;
num.b = ((z << 1) | (x & 1)) & 0xffU;
num.b = ((5U << 1) | (x & 1)) & 0xffU;
num.a = (unsigned char)x;
}


In the above example, the last assignment of num.b fails to compile where the
other two succeed.  Note in the last example, casting the bit (x&1) to _Bool
will fix the problem, but is obviously not portable to larger expressions (e.g.
x&7).

[Bug c/95213] GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables)

2020-05-19 Thread in-gcc at baka dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95213

--- Comment #1 from Seth Robertson  ---
FYI, discussion on
https://stackoverflow.com/questions/61877799/son-of-gcc-conversion-warning-when-assigning-to-a-bitfield

[Bug c/95213] GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables)

2020-05-20 Thread in-gcc at baka dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95213

Seth Robertson  changed:

   What|Removed |Added

 Status|RESOLVED|UNCONFIRMED
 Resolution|DUPLICATE   |---

--- Comment #3 from Seth Robertson  ---
I disagree that this the same bug as 39170.  I know how to disable these
warning entirely.  I know how to disable them just for the instance it is
wrong.  However, in this bug, under some circumstances gcc is incorrectly
claiming that there is a conversion error where essentially isomorphic code is
thought just fine.

Thank you for your consideration.