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

            Bug ID: 101518
           Summary: Invalid value in bit-field when setting underlying
                    data through reference
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: klimkin at gmail dot com
  Target Milestone: ---

In the example below, GCC starting v10 seems to produce invalid code for
accessing a bit field when the value for the underlying memory is set through a
reference. Consider example:

struct BitFieldLayout {
    using DataType     = unsigned;

    BitFieldLayout() { reinterpret_cast<DataType&>(*this) = {}; }

    DataType& raw_ref() { return reinterpret_cast<DataType&>(*this); }

    DataType a : 1;
};

class Ref {
public:
    operator unsigned&() { return value; }
    unsigned& value;
};

int f()
{
    BitFieldLayout bfl;
    Ref ref{bfl.raw_ref()};
    ref.value = 1;
    return bfl.a; // expected value is 1
}


g++ (Compiler-Explorer-Build) 11.1.0
f():
        xor     eax, eax
        ret
g++ (Compiler-Explorer-Build-gcc--binutils-2.36.1) 9.4.0
f():
        mov     eax, 1
        ret
clang version 12.0.1
f():                                  # @f()
        mov     eax, 1
        ret

Reply via email to