public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/95213] New: GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables)
@ 2020-05-19 14:59 in-gcc at baka dot org
  2020-05-19 15:01 ` [Bug c/95213] " in-gcc at baka dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: in-gcc at baka dot org @ 2020-05-19 14:59 UTC (permalink / raw)
  To: gcc-bugs

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)) & 0xffffffU;
    num.b = ((5U << 1) | (x & 1)) & 0xffffffU;
    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).

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/95213] GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables)
  2020-05-19 14:59 [Bug c/95213] New: GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables) in-gcc at baka dot org
@ 2020-05-19 15:01 ` in-gcc at baka dot org
  2020-05-20 21:56 ` egallager at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: in-gcc at baka dot org @ 2020-05-19 15:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95213

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/95213] GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables)
  2020-05-19 14:59 [Bug c/95213] New: GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables) in-gcc at baka dot org
  2020-05-19 15:01 ` [Bug c/95213] " in-gcc at baka dot org
@ 2020-05-20 21:56 ` egallager at gcc dot gnu.org
  2020-05-20 23:16 ` in-gcc at baka dot org
  2022-03-02  2:39 ` egallager at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: egallager at gcc dot gnu.org @ 2020-05-20 21:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95213

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |egallager at gcc dot gnu.org

--- Comment #2 from Eric Gallager <egallager at gcc dot gnu.org> ---
dup of bug 39170

*** This bug has been marked as a duplicate of bug 39170 ***

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/95213] GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables)
  2020-05-19 14:59 [Bug c/95213] New: GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables) in-gcc at baka dot org
  2020-05-19 15:01 ` [Bug c/95213] " in-gcc at baka dot org
  2020-05-20 21:56 ` egallager at gcc dot gnu.org
@ 2020-05-20 23:16 ` in-gcc at baka dot org
  2022-03-02  2:39 ` egallager at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: in-gcc at baka dot org @ 2020-05-20 23:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95213

Seth Robertson <in-gcc at baka dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|DUPLICATE                   |---

--- Comment #3 from Seth Robertson <in-gcc at baka dot org> ---
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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/95213] GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables)
  2020-05-19 14:59 [Bug c/95213] New: GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables) in-gcc at baka dot org
                   ` (2 preceding siblings ...)
  2020-05-20 23:16 ` in-gcc at baka dot org
@ 2022-03-02  2:39 ` egallager at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: egallager at gcc dot gnu.org @ 2022-03-02  2:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95213

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=39170

--- Comment #4 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Seth Robertson from comment #3)
> 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.

ok, well I still think it's at least *related*, even if it isn't exactly the
same...

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-03-02  2:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-19 14:59 [Bug c/95213] New: GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables) in-gcc at baka dot org
2020-05-19 15:01 ` [Bug c/95213] " in-gcc at baka dot org
2020-05-20 21:56 ` egallager at gcc dot gnu.org
2020-05-20 23:16 ` in-gcc at baka dot org
2022-03-02  2:39 ` egallager 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).