Compiler crash when building code that uses wrong-headed static initialization
Zig Version
0.14.0-dev.839+a931bfada
Steps to Reproduce and Observed Behavior
This is the contents of bug.zig:
const std = @import("std");
const Data = struct {
boolean: bool = false,
integer: i32 = 0,
};
const Global = struct {
const Allocator = std.heap.GeneralPurposeAllocator(.{});
const DataList = std.ArrayList(Data);
allocator: Allocator,
dataList: DataList,
pub fn init() Global {
var gpa = Allocator{};
const alloc = gpa.allocator();
const list = DataList.init(alloc);
return .{ .allocator = gpa, .dataList = list };
}
pub fn deinit(self: *Global) void {
self.dataList.deinit();
self.allocator.deinit();
}
pub fn add(self: *Global, bench: Data) void {
const newBench = self.dataList.addOne() catch @panic("unknown");
newBench.* = bench;
}
};
var global = Global.init();
pub fn main() !void {
const newData = global.add();
newData.* = Data{};
std.debug.print("{} {}\n", .{ newData.boolean, newData.integer });
}
There are a few problems with the code as written, but the zig bug comes in when I attempt to compile this program with:
zig build-exe .\bug.zig
It runs for a bit, then I get another PowerShell prompt without the compiler output being properly cleaned up and an indication from that the last command I tried to run failed. Running echo $LASTEXITCODE gives me a line that is half exit code, half uncleaned output: -2147483645Generation.
I am running Windows 11 Version 10.0.22631.3880, and I am using the nightly x64 build of zig.
Expected Behavior
Some sort of error message telling me about the problems with the code.
Reduced version:
const std = @import("std");
fn give_me() std.mem.Allocator {
var impl = std.heap.GeneralPurposeAllocator(.{}){};
return impl.allocator();
}
var global = give_me();
pub fn main() void {
_ = &global;
}
or
fn give_me() *anyopaque {
var x: u8 = 1;
return &x;
}
var global = give_me();
pub fn main() void {
_ = &global;
}
Also crashes the compiler with ArenaAllocator, FixedBufferAllocator and FailingAllocator.
Trace/breakpoint trap (core dumped) when compiling
Debug build of zig gives the following stack trace
thread 14996 panic: reached unreachable code
C:\Users\Krzysztof\dev\zig\src\codegen\llvm.zig:4329:60: 0xf2c100 in lowerPtr (zig.exe.obj)
.arr_elem, .comptime_field, .comptime_alloc => unreachable,
^
C:\Users\Krzysztof\dev\zig\src\codegen\llvm.zig:3866:35: 0xcb2c34 in lowerValue (zig.exe.obj)
.ptr => try o.lowerPtr(arg_val, 0),
^
C:\Users\Krzysztof\dev\zig\src\codegen\llvm.zig:4148:60: 0xcb8421 in lowerValue (zig.exe.obj)
vals[llvm_index] = try o.lowerValue(
^
C:\Users\Krzysztof\dev\zig\src\codegen\llvm.zig:4675:41: 0xcaccb8 in genDecl (zig.exe.obj)
else => try o.lowerValue(init_val),
^
C:\Users\Krzysztof\dev\zig\src\codegen\llvm.zig:1696:19: 0xccd6dc in updateDecl (zig.exe.obj)
dg.genDecl() catch |err| switch (err) {
^
C:\Users\Krzysztof\dev\zig\src\link\Coff.zig:1258:70: 0xf1caa7 in updateDecl (zig.exe.obj)
if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(pt, decl_index);
^
C:\Users\Krzysztof\dev\zig\src\link.zig:410:82: 0xcce2e0 in updateDecl (zig.exe.obj)
return @as(*tag.Type(), @fieldParentPtr("base", base)).updateDecl(pt, decl_index);
^
C:\Users\Krzysztof\dev\zig\src\Zcu\PerThread.zig:2642:22: 0xccdbf7 in linkerUpdateDecl (zig.exe.obj)
lf.updateDecl(pt, decl_index) catch |err| switch (err) {
^
C:\Users\Krzysztof\dev\zig\src\Compilation.zig:4062:36: 0x13f2952 in processOneCodegenJob (zig.exe.obj)
try pt.linkerUpdateDecl(decl_index);
^
C:\Users\Krzysztof\dev\zig\src\Compilation.zig:4023:36: 0x13f26bb in queueCodegenJob (zig.exe.obj)
return processOneCodegenJob(tid, comp, codegen_job);
^
C:\Users\Krzysztof\dev\zig\src\Compilation.zig:3722:45: 0x11136aa in processOneJob (zig.exe.obj)
try comp.queueCodegenJob(tid, .{ .decl = decl_index });
^
C:\Users\Krzysztof\dev\zig\src\Compilation.zig:3677:30: 0xe907be in performAllTheWorkInner (zig.exe.obj)
try processOneJob(@intFromEnum(Zcu.PerThread.Id.main), comp, job, main_progress_node);
^
C:\Users\Krzysztof\dev\zig\src\Compilation.zig:3550:36: 0xd03ab9 in performAllTheWork (zig.exe.obj)
try comp.performAllTheWorkInner(main_progress_node);
^
C:\Users\Krzysztof\dev\zig\src\Compilation.zig:2285:31: 0xcff2da in update (zig.exe.obj)
try comp.performAllTheWork(main_progress_node);
^
C:\Users\Krzysztof\dev\zig\src\main.zig:4492:20: 0xd330df in updateModule (zig.exe.obj)
try comp.update(prog_node);
^
C:\Users\Krzysztof\dev\zig\src\main.zig:3487:21: 0xd9ee5b in buildOutputType (zig.exe.obj)
updateModule(comp, color, root_prog_node) catch |err| switch (err) {
^
C:\Users\Krzysztof\dev\zig\src\main.zig:270:31: 0xbcee8e in mainArgs (zig.exe.obj)
return buildOutputType(gpa, arena, args, .run);
^
C:\Users\Krzysztof\dev\zig\src\main.zig:199:20: 0xbcc20e in main (zig.exe.obj)
return mainArgs(gpa, arena, args);
^
C:\Users\Krzysztof\dev\zig\lib\std\start.zig:591:75: 0xbcbf3a in main (zig.exe.obj)
return callMainWithArgs(@as(usize, @intCast(c_argc)), @as([*][*:0]u8, @ptrCast(c_argv)), envp);
^
C:\Users\Krzysztof\dev\zig\lib\libc\mingw\crt\crtexe.c:267:0: 0x353be11 in __tmainCRTStartup (crt2.obj)
mainret = _tmain (argc, argv, envp);
C:\Users\Krzysztof\dev\zig\lib\libc\mingw\crt\crtexe.c:188:0: 0x353be6b in mainCRTStartup (crt2.obj)
ret = __tmainCRTStartup ();
???:?:?: 0x7fffbcb5257c in ??? (KERNEL32.DLL)
???:?:?: 0x7fffbde8af27 in ??? (ntdll.dll)
Duplicate of #20365