On Wed, Sep 23, 2026 at 7:22 PM Abdulmalek Almkainzi
<[email protected]> wrote:
>
> Hello.
>
> gcc treats bit-field expressions as having different type from its
> declared integer type.
>
> for example:
>
> ```
> struct S { unsigned i : 1; };
>
> struct S s;
> int i = _Generic(s.i, unsigned: 1, default: 2);
> ```
>
> `i` is 2 in gcc, but 1 in clang and msvc.
> I think clang/msvc follow the standard more correctly here:
>
> 6.7.3.2 paragraph 5
>
>  > A bit-field shall have a type that is a qualified or unqualified
> bool, signed int, unsigned int, a
>  > bit-precise integer type, or other implementation-defined type. It is
> implementation-defined whether
>  > atomic types are permitted.
>
> I did a simple fix in c_parser_generic_selection where I convert the
> type of a bit-field expression to its declared integer type.
>
> This does make a different test fail (c23-auto-1.c), specifically this part:
> ```
>    struct b { int a : 2; unsigned b : 3; } bv = { };
>    auto bfa = bv.a;
>    auto bfb = bv.b;
>    static_assert (_Generic (bv.a, typeof (bfa) : 1, default : 2) == 1);
>    static_assert (_Generic (bv.b, typeof (bfb) : 1, default : 2) == 1);
> ```
> if the rhs of a auto type inference assignment is a bit-field, the
> variable will have a bit-field type. and that type matches a controlling
> operand of a bit-field expression.
>
>
> I don't know what's the best course of action here:
>
> 1. the current behavior is ok.
> 2. apply the _Genreic fix and remove this test.
> 3. change auto type inference. If the rhs is a bit-field, use its
> declared integer type, not its bit-field type.

The current behavior is correct.  And so is clang behavior.
See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88731 which is
exactly your testcase
Specifically https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88731#c2 .
It just happens that GCC and clang decided to have different
implementation behavior here.
Note C++ here is different from C, clang matches the C++ behavior for
both C and C++.

>
> I've attached a patch that only applies a fix to _Generic (c23-auto-1.c
> test fails), and another patch that additionally makes the auto inferred
> type not a bit-field type (test passes).
>
> Note this is my first message and patch in this mailing list, please let
> me know if I'm missing something.
>
> Regards,
> Abdulmalek Almkainzi

Reply via email to