//wanted byte offsets of dataA, dataB and dataC fields in the MyNode struct
const a: usize = 0x5;
const b: usize = 0x10;
const c: usize = 0x28;
//stores dataA, dataB and dataC at wanted byte offsets - freespace variables to be ignored
//freespace1, 2 & 3 are used to "fill" space between dataA, B & C
const MyNode = struct {
freeSpace1: [a]u8, //0x0
dataA: bool, //0x5
freeSpace2: [b - @byteOffsetOf(@This(), "dataA") - sizeOf(bool)]u8, //0x6
dataB: u32, //0x10
freeSpace3: [c - @byteOffsetOf(@This(), "dataB") - sizeOf(u32)]u8, //0x14
dataC: [2]f32 //0x28
};
pub fn main() void {
var data: MyNode = undefined;
}
This (probably stupid) code results in:
```zig build-exe .\main.zig
Segmentation fault at address 0x0
Unable to dump stack trace: Unable to open debug info: InvalidPEMagic
I didn't think the code would compile, but figured it would be best to report it due to the segfault. Ideally, this would give me some kind of warning.
Running the latest master (2019-08-12), windows 10 x86-64
zig version
0.4.0+3aed7de0
```
Thanks for the issue. It looks like self-referential structs are a bit broken. Here's a gdb log and a reduced test case.
Program received signal SIGSEGV, Segmentation fault.
0x0000000000f922df in type_is_invalid (type_entry=0x0) at /home/samteb01/repos/zig/src/analyze.cpp:1504
1504 switch (type_entry->id) {
(gdb) bt
#0 0x0000000000f922df in type_is_invalid (type_entry=0x0) at /home/samteb01/repos/zig/src/analyze.cpp:1504
#1 0x0000000000f9c8ec in type_resolve (g=0x6bd0ac0, ty=0x0, status=ResolveStatusSizeKnown)
at /home/samteb01/repos/zig/src/analyze.cpp:5036
#2 0x0000000000f92972 in resolve_struct_type (g=0x6bd0ac0, struct_type=0xd077000) at /home/samteb01/repos/zig/src/analyze.cpp:1635
#3 0x0000000000f9ca3e in type_resolve (g=0x6bd0ac0, ty=0xd077000, status=ResolveStatusSizeKnown)
at /home/samteb01/repos/zig/src/analyze.cpp:5063
#4 0x0000000000f9c8cb in ensure_complete_type (g=0x6bd0ac0, type_entry=0xd077000) at /home/samteb01/repos/zig/src/analyze.cpp:5032
#5 0x0000000000f38dbc in validate_byte_offset (ira=0xd07ea20, type_value=0xd080d70, field_name_value=0xd080fb0,
byte_offset=0x7fffffffd360) at /home/samteb01/repos/zig/src/ir.cpp:20087
#6 0x0000000000f38fc3 in ir_analyze_instruction_byte_offset_of (ira=0xd07ea20, instruction=0xd07d600)
at /home/samteb01/repos/zig/src/ir.cpp:20128
#7 0x0000000000f4d74a in ir_analyze_instruction_base (ira=0xd07ea20, instruction=0xd07d600)
at /home/samteb01/repos/zig/src/ir.cpp:25698
#8 0x0000000000f4df32 in ir_analyze (codegen=0x6bd0ac0, old_exec=0xd07c770, new_exec=0xd07e920, expected_type=0x6bd3c80,
expected_type_source_node=0x0) at /home/samteb01/repos/zig/src/ir.cpp:25857
#9 0x0000000000f19c57 in ir_eval_const_value (codegen=0x6bd0ac0, scope=0xd077100, node=0x6c1be90, expected_type=0x6bd3c80,
backward_branch_count=0x7fffffffd508, backward_branch_quota=0x7fffffffd510, fn_entry=0x0, c_import_buf=0x0, source_node=0x6c1be90,
exec_name=0x0, parent_exec=0x0, expected_type_source_node=0x0) at /home/samteb01/repos/zig/src/ir.cpp:11489
#10 0x0000000000f90a7e in analyze_const_value (g=0x6bd0ac0, scope=0xd077100, node=0x6c1be90, type_entry=0x6bd3c80, type_name=0x0)
at /home/samteb01/repos/zig/src/analyze.cpp:975
#11 0x0000000000f90ad1 in analyze_type_expr (g=0x6bd0ac0, scope=0xd077100, node=0x6c1be90)
at /home/samteb01/repos/zig/src/analyze.cpp:979
#12 0x0000000000f944d6 in resolve_struct_zero_bits (g=0x6bd0ac0, struct_type=0xd077000)
at /home/samteb01/repos/zig/src/analyze.cpp:2165
#13 0x0000000000f9479d in resolve_struct_alignment (g=0x6bd0ac0, struct_type=0xd077000)
at /home/samteb01/repos/zig/src/analyze.cpp:2239
#14 0x0000000000f927b9 in resolve_struct_type (g=0x6bd0ac0, struct_type=0xd077000) at /home/samteb01/repos/zig/src/analyze.cpp:1606
#15 0x0000000000f9ca3e in type_resolve (g=0x6bd0ac0, ty=0xd077000, status=ResolveStatusSizeKnown)
at /home/samteb01/repos/zig/src/analyze.cpp:5063
#16 0x0000000000f9c8cb in ensure_complete_type (g=0x6bd0ac0, type_entry=0xd077000) at /home/samteb01/repos/zig/src/analyze.cpp:5032
#17 0x0000000000f9c71f in init_const_undefined (g=0x6bd0ac0, const_val=0xd0783b0) at /home/samteb01/repos/zig/src/analyze.cpp:5001
#18 0x0000000000f1b6ad in ir_analyze_undefined_to_anything (ira=0xd076830, source_instr=0xd078200, target=0xd078200,
wanted_type=0xd077000) at /home/samteb01/repos/zig/src/ir.cpp:12002
#19 0x0000000000f1f89b in ir_analyze_cast (ira=0xd076830, source_instr=0xd078200, wanted_type=0xd077000, value=0xd078200,
result_loc=0xd075d20) at /home/samteb01/repos/zig/src/ir.cpp:13030
#20 0x0000000000f1fa61 in ir_implicit_cast_with_result (ira=0xd076830, value=0xd078200, expected_type=0xd077000, result_loc=0xd075d20)
at /home/samteb01/repos/zig/src/ir.cpp:13056
#21 0x0000000000f282a6 in ir_analyze_instruction_implicit_cast (ira=0xd076830, instruction=0xd076190)
at /home/samteb01/repos/zig/src/ir.cpp:15545
const MyNode = struct {
data: bool,
freeSpace: [@byteOffsetOf(@This(), "data")]u8
};
pub fn main() void {
var data: MyNode = undefined;
}
This now gives:
./test2.zig:8:16: error: struct 'MyNode' depends on itself
const MyNode = struct {
^
./test2.zig:11:26: note: referenced here
freeSpace2: [b - @byteOffsetOf(@This(), "dataA") - sizeOf(bool)]u8, //0x6
^
./test2.zig:18:24: note: referenced here
var data: MyNode = undefined;
^
which I think is a reasonable error, given that zig doesn't even have access to the types of the fields before being asked what the offsets are. Given that offsets depend on field order, and field order depends on types, this is a circular dependency.