Open jean-dao opened 7 months ago
I got the first one trying to reduce this one, I suppose they are related.
// bug.zig
pub fn main() void {
comptime var defs: [5]union(enum) { bar: []const u8, foo } = undefined;
inline for (&defs) |*def| {
def.* = .{ .bar = "foo" };
}
}
AIR logs are similar, it crashes when trying to print the value.
Note: works fine if value assigned is explicitly marked comptime
:
pub fn main() void {
comptime var def: union(enum) { bar: []const u8, foo } = undefined;
def = comptime .{ .bar = "bar" };
}
Zig Version
0.12.0
Steps to Reproduce and Observed Behavior
With the
zig
binary from the release tarball:With a debug 0.12.0 `zig`:
```sh $ ../zig/stage3/bin/zig build-exe bug.zig thread 22657 panic: reached unreachable code /home/jean/src/zig/lib/std/debug.zig:403:14: 0x9e657fc in assert (zig) if (!ok) unreachable; // assertion failure ^ /home/jean/src/zig/src/Module.zig:6145:11: 0xa2036ad in getUnionLayout (zig) assert(u.haveLayout(ip)); ^ /home/jean/src/zig/src/type.zig:2002:34: 0xa1fe01d in unionGetLayout (zig) return mod.getUnionLayout(union_obj); ^ /home/jean/src/zig/src/codegen/llvm.zig:9351:44: 0xa790198 in airSetUnionTag (zig) const layout = un_ty.unionGetLayout(mod); ^ /home/jean/src/zig/src/codegen/llvm.zig:4933:59: 0xa3e3a4d in genBody (zig) .set_union_tag => try self.airSetUnionTag(inst), ^ /home/jean/src/zig/src/codegen/llvm.zig:1709:19: 0xa3dc9c6 in updateFunc (zig) fg.genBody(air.getMainBody()) catch |err| switch (err) { ^ /home/jean/src/zig/src/link/Elf.zig:3029:70: 0xa720819 in updateFunc (zig) if (self.llvm_object) |llvm_object| return llvm_object.updateFunc(mod, func_index, air, liveness); ^ /home/jean/src/zig/src/link.zig:420:82: 0xa3eb158 in updateFunc (zig) return @as(*tag.Type(), @fieldParentPtr("base", base)).updateFunc(module, func_index, air, liveness); ^ /home/jean/src/zig/src/Module.zig:3219:22: 0xa15dd37 in ensureFuncBodyAnalyzed (zig) lf.updateFunc(zcu, func_index, air, liveness) catch |err| switch (err) { ^ /home/jean/src/zig/src/Compilation.zig:3419:42: 0xa15ae62 in processOneJob (zig) module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) { ^ /home/jean/src/zig/src/Compilation.zig:3359:30: 0x9f89f9a in performAllTheWork (zig) try processOneJob(comp, work_item, main_progress_node); ^ /home/jean/src/zig/src/Compilation.zig:2132:31: 0x9f854d2 in update (zig) try comp.performAllTheWork(main_progress_node); ^ /home/jean/src/zig/src/main.zig:4483:24: 0x9fb6c2f in updateModule (zig) try comp.update(main_progress_node); ^ /home/jean/src/zig/src/main.zig:3405:17: 0xa01f554 in buildOutputType (zig) updateModule(comp, color) catch |err| switch (err) { ^ /home/jean/src/zig/src/main.zig:260:31: 0x9e67a31 in mainArgs (zig) return buildOutputType(gpa, arena, args, .{ .build = .Exe }); ^ /home/jean/src/zig/src/main.zig:206:20: 0x9e649c5 in main (zig) return mainArgs(gpa, arena, args); ^ /home/jean/src/zig/lib/std/start.zig:511:37: 0x9e6445e in main (zig) const result = root.main() catch |err| { ^ ../sysdeps/nptl/libc_start_call_main.h:58:16: 0x7f9e5c05ec4b in __libc_start_call_main (../sysdeps/x86/libc-start.c) ../csu/libc-start.c:360:3: 0x7f9e5c05ed04 in __libc_start_main_impl (../sysdeps/x86/libc-start.c) ../sysdeps/x86_64/start.S:115:0: 0x4f6eb60 in _start (../sysdeps/x86_64/start.S) ???:?:?: 0x0 in ??? (???) Aborted ```AIR logs (crashes in `print_value.zig`)
``` # Begin Function AIR: start._start: # Total AIR+Liveness bytes: 394B # AIR Instructions: 10 (90B) # AIR Extra Data: 48 (192B) # Liveness tomb_bits: 8B # Liveness Extra Data: 0 (0B) # Liveness special table: 0 (0B) %0!= save_err_return_trace_index() %1!= dbg_stmt(17:5) %8!= assembly(void, volatile, [argc_argv_ptr] out =m = (<*[*]usize, start.argc_argv_ptr>), [posixCallMainAndExit] in X = (<*const fn () callconv(.C) noreturn, start.posixCallMainAndExit>), " xorl %%ebp, %%ebp movq %%rsp, %[argc_argv_ptr] andq $-16, %%rsp callq %[posixCallMainAndExit:P]") %9!= trap() # End Function AIR: start._start # Begin Function AIR: start.posixCallMainAndExit: # Total AIR+Liveness bytes: 4.8603515625KiB # AIR Instructions: 273 (2.3994140625KiB) # AIR Extra Data: 411 (1.60546875KiB) # Liveness tomb_bits: 144B # Liveness Extra Data: 95 (380B) # Liveness special table: 31 (248B) %0!= save_err_return_trace_index() %1!= dbg_stmt(2:5) %2 = load([*]usize, <*[*]usize, start.argc_argv_ptr>) %3!= dbg_stmt(2:31) %4 = ptr_elem_val(%2!, @Air.Inst.Ref.zero_usize) %5!= dbg_var_val(%4, "argc") %6!= dbg_stmt(3:5) %8 = load([*]usize, <*[*]usize, start.argc_argv_ptr>) %9!= dbg_stmt(3:57) %10 = ptr_add([*]usize, %8!, @Air.Inst.Ref.one_usize) %11!= dbg_stmt(3:34) %12 = bitcast([*][*:0]u8, %10!) %13!= dbg_var_val(%12, "argv") %14!= dbg_stmt(5:70) %16 = ptr_add([*][*:0]u8, %12, %4) %17!= dbg_stmt(5:77) %18 = ptr_add([*][*:0]u8, %16!, @Air.Inst.Ref.one_usize) %19!= dbg_stmt(5:45) %20 = bitcast([*:null]?[*:0]u8, %18!) %21!= dbg_var_val(%20, "envp_optional") %22!= dbg_stmt(6:5) %23 = alloc(*usize) %24!= store_safe(%23, @Air.Inst.Ref.zero_usize) %25!= dbg_var_ptr(%23, "envp_count") %26!= block(void, { %27!= loop(noreturn, { %28!= block(void, { %29!= dbg_stmt(7:12) %30 = load(usize, %23) %31!= dbg_stmt(7:25) %32 = ptr_elem_val(%20, %30!) %33 = is_non_null(%32!) %44!= cond_br(%33!, { %34!= optional_payload([*:0]u8, %32) %36!= dbg_stmt(7:46) %38 = load(usize, %23) %39!= dbg_stmt(7:57) %40 = add_safe(%38!, @Air.Inst.Ref.one_usize) %41!= store_safe(%23, %40!) %42!= br(%28, @Air.Inst.Ref.void_value) }, { %43!= br(%26, @Air.Inst.Ref.void_value) }) }) }) }) %45!= dbg_stmt(8:34) %47 = bitcast([*][*:0]u8, %20!) %48 = alloc(*[*][*:0]u8) %49!= store_safe(%48, %47!) %50 = bitcast(*const [*][*:0]u8, %48!) %51 = load(usize, %23) %52!= dbg_stmt(8:58) %53 = load([*][*:0]u8, %50!) %54 = ptr_add([*][*:0]u8, %53!, @Air.Inst.Ref.zero_usize) %55 = cmp_lte(@Air.Inst.Ref.zero_usize, %51) %58!= block(void, { %59!= cond_br(%55!, { %60!= br(%58, @Air.Inst.Ref.void_value) }, { %12! %4! %54! %23! %56!= call(Expected Behavior
Program compiles, or an error is printed.