Zig Version
0.17.0-dev.657+2faf8debf
Steps to Reproduce, Observed Behavior, and Expected Behavior
I also had the same problem in 0.16 and 0.15.2. This is my first bug report on zig, feedback is welcome (Especially the title). I tried to compress the code to reproduce the bug as much as possible. I originally encountered It while trying to process a json file for font stuff.
To reproduce you just need a build.zig and then src/root.zig like below:
const std = @import("std");
pub const Temp = extern struct {
arr1: [4]f32 = @splat(0),
arr2: [4]f32 = @splat(0),
};
pub const Crusher = extern struct {
a: f32 = 0,
b: f32 = 0,
temps: [128]Temp = @splat(.{}),
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
_ = optimize;
const mod = b.addModule("bug", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
});
const exe = b.addExecutable(.{
.name = "bug",
.root_module = mod,
});
const tmp: Crusher = .{};
const options = b.addOptions();
options.addOption(Crusher, "crusher", tmp);
mod.addImport("crusherMod", options.createModule());
b.installArtifact(exe);
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
}
(NOTE: I had the same bug happen if the struct wasn't extern, I just ended up using extern struct for a workaround, which was to bitcast into a [@sizeOf(Crusher)]u8 and then import that array, then I did the bitcast again to get back the struct since the generated file off that is just an array of bytes)
Then make any root.zig to use that import.
const std = @import("std");
const crusherMod = @import("crusherMod");
const crusher = crusherMod.crusher;
pub fn main() !void {
std.debug.print("{}\n", .{crusher.temps[5].arr1[0]});
}
gives the following error:
.../test/bug ❯ zig build run
run
└─ run exe bug
└─ install
└─ install bug
└─ compile exe bug Debug native 1 errors
.zig-cache/o/3e853c4eedcc88592f7c80b1cec0b968/options.zig:4:50: error: expected expression, found 'pub'
temps: @"[128]build.Temp" = [128]build.Temp {
^
error: 1 compilation errors
failed command: /home/plebosaur/.local/share/mise/installs/zig/master/zig build-exe --dep crusherMod -Mroot=/home/plebosaur/dev/zig/test/bug/src/root.zig -McrusherMod=.zig-cache/o/3e853c4eedcc88592f7c80b1cec0b968/options.zig --cache-dir .zig-cache --global-cache-dir /home/plebosaur/.cache/zig --name bug --zig-lib-dir /home/plebosaur/.local/share/mise/installs/zig/master/lib/ --listen=-
Build Summary: 1/6 steps succeeded (1 failed)
run transitive failure
└─ run exe bug transitive failure
├─ compile exe bug Debug native 1 errors
└─ install transitive failure
└─ install bug transitive failure
└─ compile exe bug Debug native (+1 more reused dependencies)
error: the following maker command exited with code 1:
/home/plebosaur/.cache/zig/o/9287f35fa1d5bca587ee0302317878b9/maker --zig /home/plebosaur/.local/share/mise/installs/zig/master/zig --zig-lib-dir /home/plebosaur/.local/share/mise/installs/zig/master/lib --build-root /home/plebosaur/dev/zig/test/bug --local-cache .zig-cache --global-cache /home/plebosaur/.cache/zig --configuration .zig-cache/c/146d9e9302b93899c940b185bf8cc270 --seed 0x4b12bf17 run
Looking at the generated file we get this:
pub const @"build.Crusher" = extern struct {
a: f32 = 0,
b: f32 = 0,
temps: @"[128]build.Temp" = [128]build.Temp {
pub const @"build.Temp" = extern struct {
arr1: [4]f32 = [4]f32 {
0,
0,
0,
0,
},
arr2: [4]f32 = [4]f32 {
0,
0,
0,
0,
},
};
},
};
pub const crusher: @"build.Crusher" = .{
.a = 0,
.b = 0,
.temps = [128]build.Temp {
},
};
Expected behaviour
The code should compile