#Packed struct backing integer type too big

1 messages · Page 1 of 1 (latest)

candid haven
#

I am trying to create a packed struct(u8) backed by an integer and I receive the error

error: backing integer type 'u8' has bit size 8 but the struct fields have a total bit size of 7

I remember this worked correctly in a previous version. Is this a regression bug or intended behavior?

fallow ledge
#

whats in the struct?

candid haven
#

In my actual project it contains some enums, but I was able to reproduce the same error with this code:

const Foo = packed struct(u8) {
    a: u4,
    b: u3,
};

pub fn main() !void {
    const foo: Foo = undefined;
    _ = foo;
}
cedar flame
#

you have to be specific about where the 1 bit of padding is, add something like _padding: u1 = undefined

candid haven
#

Thanks, this one works:

const Foo = packed struct(u8) {
    a: u4,
    b: u3,
    _: u1 = undefined,
};

pub fn main() !void {
    const foo: Foo = undefined;
    _ = foo;
}