public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "in-gcc at baka dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/95213] New: GCC -Werror=conversion error when assigning to a bitfield (when mixing constants and variables)
Date: Tue, 19 May 2020 14:59:51 +0000	[thread overview]
Message-ID: <bug-95213-4@http.gcc.gnu.org/bugzilla/> (raw)

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).

             reply	other threads:[~2020-05-19 14:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-19 14:59 in-gcc at baka dot org [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-95213-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).