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

            Bug ID: 106371
           Summary: Bogus narrowing conversion reported due to bitfield
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

Consider:

#include <cstdint>

struct A {
  uint64_t x : 32;
};

struct B {
  uint32_t x;
};

void f() {
    auto a = A{.x = 1};
    auto b = B{.x = a.x};
}

gcc currently emits a narrowing warning here:

<source>:13:24: warning: narrowing conversion of '(long unsigned int)a.A::x'
from 'long unsigned int' to 'uint32_t' {aka 'unsigned int'} [-Wnarrowing]
   13 |     auto b = B{ .x = a.x };
      |                      ~~^

It is true that a.x is a uint64_t, but also it's only a 32-bit bitfield, there
isn't actually any narrowing possible. This code should be fine. 

clang doesn't warn here (only if A::x were actually a uint64_t, or the width of
the bitfield was larger than 32).

Reply via email to