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