#Struct of packed struct, am I stupid or is this a compiler bug?

1 messages · Page 1 of 1 (latest)

sterile bluff
#

I'm trying to embed a large packed struct into a struct, and somehow default values aren't properly propagated when I return this struct from a function. Am I missing something or is this a compiler bug?

const std = @import("std");

const Bar = packed struct {
    magic: u32 = undefined, // When this moves, assert goes away
    supposed_to_be_one: u32 = 1,
};

const Foo = struct {
    pack: Bar = .{},
};

fn create_foo() Foo {
    return .{};
}

pub fn main() !void {
    const works: Foo = .{};

    std.debug.assert(works.pack.supposed_to_be_one == 1);

    const fails = create_foo();

    std.debug.assert(fails.pack.supposed_to_be_one == 1);
}

When I run this example, the second assert triggers but not the first one. (zig 0.13.0)

warm hedge
#

this seems like it ought to work. what is the definition of MMIO?

#

i've tested a couple different layouts and i haven't seen the error you describe so it might need some very specific circumstances to occur

true zenith
#

A minimal reproducible example would be nice. Certainly seems like a bug at first glance though

warm hedge
#

oh but that doesn't have GPUSTAT

sterile bluff
#

I'm currently making the most minimal example I can find, the MMIO struct is really phat, excuse me for a few minutes

warm hedge
#

don't think i've seen an honest to god u32767 before

sterile bluff
#

I updated the OP with the best I could do

warm hedge
#

by "When this moves" do you mean that if you swap the order of the fields then the default value is set correctly?

sterile bluff
#

Yes exactly.
This looks to be solved with 0.14-master

warm hedge
#

oh nice!