Zig Version
0.15.2
Steps to Reproduce and Observed Behavior
src/main.zig:
conststd=@import("std");constDATA1=@embedFile("file1");pubconstS=packedstruct{a:bool,X:S=S{.a=true},};test{_=DATA1;std.testing.refAllDeclsRecursive(@This());}
build.zig:
conststd=@import("std");pubfnbuild(b:*std.Build)void{consttarget=b.standardTargetOptions(.{});constoptimize=b.standardOptimizeOption(.{});constmod=b.createModule(.{.root_source_file=b.path("src/main.zig"),.target=target,.optimize=optimize,});mod.addAnonymousImport("file1",.{.root_source_file=b.path("file1.bin")});consttests=b.addTest(.{.root_module=mod,});construn_tests=b.addRunArtifact(tests);consttest_step=b.step("test","Run unit tests");test_step.dependOn(&run_tests.step);}
Running zig build test gets me:
test
└─ run test
└─ compile test Debug native failure
error: the following command terminated unexpectedly:
/Users/vibhav.singamshetty/.zvm/0.15.2/zig test -ODebug --dep file1 -Mroot=/Users/vibhav.singamshetty/Developer/Personal/zig-embedfile-segfault-repro/src/main.zig -Mfile1=/Users/vibhav.singamshetty/Developer/Personal/zig-embedfile-segfault-repro/file1.bin --cache-dir .zig-cache --global-cache-dir /Users/vibhav.singamshetty/.cache/zig --name test --zig-lib-dir /Users/vibhav.singamshetty/.zvm/0.15.2/lib/ --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
test transitive failure
└─ run test transitive failure
└─ compile test Debug native failure
error: the following build command failed with exit code 1:
.zig-cache/o/5b78800b411837e91f5ff818570830fb/build /Users/vibhav.singamshetty/.zvm/0.15.2/zig /Users/vibhav.singamshetty/.zvm/0.15.2/lib /Users/vibhav.singamshetty/Developer/Personal/zig-embedfile-segfault-repro .zig-cache /Users/vibhav.singamshetty/.cache/zig --seed 0x301cc96d -Zbd7e82ca59fcc513 test
Trigger conditions: All of the following are required:
@embedFile for a binary file
addAnonymousImport in build.zig to provide that file
std.testing.refAllDeclsRecursive(@This()) in the test block
- A packed struct with a self-referential constant (e.g.
X: S = S{ .a = true })
Removing any one of these makes the build succeed. With a regular (non-packed) struct and the same self-reference, the compiler reports the error "struct 'main.S' depends on itself" instead of segfaulting.
Minimal reproduction repo here: https://github.com/goodvibs/zig-embedfile-segfault-repro
Expected Behavior
The tests should compile and run successfully. If the packed struct self-reference is invalid, the compiler should report a clear error (e.g. struct depends on itself) instead of crashing. Is the self reference invalid though?
### Zig Version
0.15.2
### Steps to Reproduce and Observed Behavior
`src/main.zig`:
```zig
const std = @import("std");
const DATA1 = @embedFile("file1");
pub const S = packed struct {
a: bool,
X: S = S{ .a = true },
};
test {
_ = DATA1;
std.testing.refAllDeclsRecursive(@This());
}
```
`build.zig`:
```zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
mod.addAnonymousImport("file1", .{ .root_source_file = b.path("file1.bin") });
const tests = b.addTest(.{
.root_module = mod,
});
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_tests.step);
}
```
Running `zig build test` gets me:
```
test
└─ run test
└─ compile test Debug native failure
error: the following command terminated unexpectedly:
/Users/vibhav.singamshetty/.zvm/0.15.2/zig test -ODebug --dep file1 -Mroot=/Users/vibhav.singamshetty/Developer/Personal/zig-embedfile-segfault-repro/src/main.zig -Mfile1=/Users/vibhav.singamshetty/Developer/Personal/zig-embedfile-segfault-repro/file1.bin --cache-dir .zig-cache --global-cache-dir /Users/vibhav.singamshetty/.cache/zig --name test --zig-lib-dir /Users/vibhav.singamshetty/.zvm/0.15.2/lib/ --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
test transitive failure
└─ run test transitive failure
└─ compile test Debug native failure
error: the following build command failed with exit code 1:
.zig-cache/o/5b78800b411837e91f5ff818570830fb/build /Users/vibhav.singamshetty/.zvm/0.15.2/zig /Users/vibhav.singamshetty/.zvm/0.15.2/lib /Users/vibhav.singamshetty/Developer/Personal/zig-embedfile-segfault-repro .zig-cache /Users/vibhav.singamshetty/.cache/zig --seed 0x301cc96d -Zbd7e82ca59fcc513 test
```
Trigger conditions: All of the following are required:
- `@embedFile` for a binary file
- `addAnonymousImport` in `build.zig` to provide that file
- `std.testing.refAllDeclsRecursive(@This())` in the test block
- A packed struct with a self-referential constant (e.g. `X: S = S{ .a = true }`)
Removing any one of these makes the build succeed. With a regular (non-packed) struct and the same self-reference, the compiler reports the error "struct 'main.S' depends on itself" instead of segfaulting.
Minimal reproduction repo here: https://github.com/goodvibs/zig-embedfile-segfault-repro
### Expected Behavior
The tests should compile and run successfully. If the packed struct self-reference is invalid, the compiler should report a clear error (e.g. struct depends on itself) instead of crashing. Is the self reference invalid though?