public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pokechu022+gccbugzilla at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/108670] New: Bogus narrowing conversion warning with designated initializers for bitfield in union
Date: Sat, 04 Feb 2023 01:18:44 +0000	[thread overview]
Message-ID: <bug-108670-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 108670
           Summary: Bogus narrowing conversion warning with designated
                    initializers for bitfield in union
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pokechu022+gccbugzilla at gmail dot com
  Target Milestone: ---

This code (https://godbolt.org/z/4YGTfPoY9):

```
union Foo {
    int x : 4;
};
union Bar {
    int y : 4;
};
int foo() {
    Foo foo{.x = 5};
    Bar bar{.y = foo.x};
    return bar.y;
}
```

Yields the following warning:

```
<source>:9:22: warning: narrowing conversion of '(int)foo.Foo::x' from 'int' to
'signed char:4' [-Wnarrowing].
    9 |     Bar bar{.y = foo.x};
      |                  ~~~~^
```

This warning does not make sense since both fields are the same size, and the
field is not a signed char. (A size of 31 will still show a warning with
int:31,  while int:32 gives no warning).

Foo can be either a struct or union. Weirdly, Bar must be a union (and I
couldn't get *any* narrowing conversion warnings to occur with it as a struct,
even if the sizes don't match).

I'm pretty sure this is different from bug 106371 and bug 87292 (though the
latter seems somewhat close). Nor does it match bug 102538. Based on
godbolt.org, this bug exists in the 4.7.1, the first version to support
designated initializers (though they weren't called that then).

This was originally encountered in Dolphin Emulator (see
https://github.com/dolphin-emu/dolphin/pull/11534).

                 reply	other threads:[~2023-02-04  1:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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