Runtime safety check added in comptime block
Zig Version
0.13.0-dev.266+0b0625ccf
Steps to Reproduce and Observed Behavior
I am learning Zig by experimenting with writing a simple neural network code and experience a compiler crash. I tried to distill the code to the very minimum, that causes the crash. This code is not entirely correct, and being frank, I don't even know if it should compile at all. But certainly it should not crash.
const std = @import("std");
pub fn Layer(comptime M: usize, comptime N: usize) type {
return struct {
const Self = @This();
neurons: [N*M]f32,
};
}
fn MLP(comptime N: usize, comptime nin: usize, comptime nouts: [N]usize) type {
const sz = [_]usize{nin} ++ nouts;
return struct {
const Self = @This();
layers: [N]*anyopaque,
pub fn init() !Self {
var layers = [_]*anyopaque{undefined} ** N;
for (0..N) |i| {
const layer_type = Layer(sz[i], sz[i + 1]);
layers[i] = try std.heap.page_allocator.create(layer_type);
}
return Self{ .layers = layers };
}
};
}
pub fn main() !void {
const mlp = MLP(3, 2, [_]usize{ 16, 16, 1 }).init();
std.debug.print("mlp.layers.len {}\n", .{mlp.layers.len});
}
By running the above code I get the following error:
user@xi:~/Projects/zigrad$ ~/Projects/zig/build_master/stage3/bin/zig run src/zig_crash.zig
thread 81111 panic: reached unreachable code
Analyzing src/zig_crash.zig: zig_crash.zig:MLP(3,2,.{ 16, 16, 1 }).init
%82 = extended(closure_get(1))
%83 = dbg_stmt(4, 44)
> %84 = elem_val_node(%82, %73)
%85 = break_inline(%81, %84)
For full context, use the command
zig ast-check -t src/zig_crash.zig
in src/zig_crash.zig: zig_crash.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %81 = call(.auto, %79, [
{%82..%85},
{%86..%91},
])
in src/zig_crash.zig: zig_crash.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %75 = condbr(%74, {%77..%110}, {%111})
in src/zig_crash.zig: zig_crash.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %76 = block({%74, %75})
in src/zig_crash.zig: zig_crash.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %72 = loop({%73..%114})
/home/user/Projects/zig/lib/std/debug.zig:403:14: 0x18182cc in assert (zig)
if (!ok) unreachable; // assertion failure
^
/home/user/Projects/zig/src/Sema.zig:27184:11: 0x2743f45 in panicIndexOutOfBounds (zig)
assert(!parent_block.is_comptime);
^
/home/user/Projects/zig/src/Sema.zig:28786:43: 0x21d99a7 in elemValArray (zig)
try sema.panicIndexOutOfBounds(block, src, elem_index, len_inst, cmp_op);
^
/home/user/Projects/zig/src/Sema.zig:28583:43: 0x2a81b4e in elemVal (zig)
.Array => return sema.elemValArray(block, src, indexable_src, indexable, elem_index_src, elem_index, oob_safety),
^
/home/user/Projects/zig/src/Sema.zig:10848:24: 0x25c724a in zirElemValNode (zig)
return sema.elemVal(block, src, array, elem_index, elem_index_src, true);
^
/home/user/Projects/zig/src/Sema.zig:1028:69: 0x20b196a in analyzeBodyInner (zig)
.elem_val_node => try sema.zirElemValNode(block, inst),
^
/home/user/Projects/zig/src/Sema.zig:910:30: 0x1de0187 in analyzeInlineBody (zig)
if (sema.analyzeBodyInner(block, body)) |_| {
^
/home/user/Projects/zig/src/Sema.zig:936:39: 0x1b396ae in resolveInlineBody (zig)
return (try sema.analyzeInlineBody(block, body, break_target)) orelse .unreachable_value;
^
/home/user/Projects/zig/src/Sema.zig:7426:65: 0x2cae5b4 in analyzeArg (zig)
const uncoerced_arg = try sema.resolveInlineBody(block, arg_body, zir_call.call_inst);
^
/home/user/Projects/zig/src/Sema.zig:8116:56: 0x2cac915 in analyzeInlineCallArg (zig)
const casted_arg = try args_info.analyzeArg(ics.caller(), arg_block, arg_i.*, Type.fromInterned(param_ty), func_ty_info, func_inst);
^
/home/user/Projects/zig/src/Sema.zig:7827:62: 0x27313e2 in analyzeCall (zig)
const opt_noreturn_ref = try analyzeInlineCallArg(
^
/home/user/Projects/zig/src/Sema.zig:7136:43: 0x25c22ee in zirCall__anon_93341 (zig)
const call_inst = try sema.analyzeCall(block, func, func_ty, callee_src, call_src, modifier, ensure_result_used, args_info, call_dbg_node, .call);
^
/home/user/Projects/zig/src/Sema.zig:1014:62: 0x20b1057 in analyzeBodyInner (zig)
.call => try sema.zirCall(block, inst, .direct),
^
/home/user/Projects/zig/src/Sema.zig:874:26: 0x2acc31f in analyzeBodyRuntimeBreak (zig)
sema.analyzeBodyInner(block, body) catch |err| switch (err) {
^
/home/user/Projects/zig/src/Sema.zig:19287:37: 0x2677c67 in zirCondbr (zig)
try sema.analyzeBodyRuntimeBreak(&sub_block, then_body);
^
/home/user/Projects/zig/src/Sema.zig:1693:39: 0x20bd47a in analyzeBodyInner (zig)
try sema.zirCondbr(block, inst);
^
/home/user/Projects/zig/src/Sema.zig:6176:34: 0x2acc9cb in resolveBlockBody (zig)
if (sema.analyzeBodyInner(child_block, body)) |_| {
^
/home/user/Projects/zig/src/Sema.zig:6153:33: 0x267cfae in zirBlock (zig)
return sema.resolveBlockBody(parent_block, src, &child_block, body, inst, &label.merges);
^
/home/user/Projects/zig/src/Sema.zig:1559:49: 0x20bf4d0 in analyzeBodyInner (zig)
break :blk try sema.zirBlock(block, inst, tags[@intFromEnum(inst)] == .block_comptime);
^
/home/user/Projects/zig/src/Sema.zig:5968:30: 0x267427f in zirLoop (zig)
try sema.analyzeBodyInner(&loop_block, body);
^
/home/user/Projects/zig/src/Sema.zig:1534:68: 0x20bc316 in analyzeBodyInner (zig)
if (!block.is_comptime) break :blk try sema.zirLoop(block, inst);
^
/home/user/Projects/zig/src/Sema.zig:892:26: 0x20afc91 in analyzeFnBody (zig)
sema.analyzeBodyInner(block, body) catch |err| switch (err) {
^
/home/user/Projects/zig/src/Module.zig:4633:23: 0x1d9dc5f in analyzeFnBody (zig)
sema.analyzeFnBody(&inner_block, fn_info.body) catch |err| switch (err) {
^
/home/user/Projects/zig/src/Module.zig:3143:32: 0x1b07fc8 in ensureFuncBodyAnalyzed (zig)
var air = zcu.analyzeFnBody(func_index, sema_arena) catch |err| switch (err) {
^
/home/user/Projects/zig/src/Compilation.zig:3414:42: 0x1b05ae2 in processOneJob (zig)
module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
^
/home/user/Projects/zig/src/Compilation.zig:3354:30: 0x193ac7f in performAllTheWork (zig)
try processOneJob(comp, work_item, main_progress_node);
^
/home/user/Projects/zig/src/Compilation.zig:2132:31: 0x1936683 in update (zig)
try comp.performAllTheWork(main_progress_node);
^
/home/user/Projects/zig/src/main.zig:4489:24: 0x19678af in updateModule (zig)
try comp.update(main_progress_node);
^
/home/user/Projects/zig/src/main.zig:3411:17: 0x19d0352 in buildOutputType (zig)
updateModule(comp, color) catch |err| switch (err) {
^
/home/user/Projects/zig/src/main.zig:270:31: 0x181a7f9 in mainArgs (zig)
return buildOutputType(gpa, arena, args, .run);
^
/home/user/Projects/zig/src/main.zig:208:20: 0x1817495 in main (zig)
return mainArgs(gpa, arena, args);
^
/home/user/Projects/zig/lib/std/start.zig:524:37: 0x1816f2e in main (zig)
const result = root.main() catch |err| {
^
../sysdeps/nptl/libc_start_call_main.h:58:16: 0x75ea25429d8f in __libc_start_call_main (../sysdeps/x86/libc-start.c)
../csu/libc-start.c:392:3: 0x75ea25429e3f in __libc_start_main_impl (../sysdeps/x86/libc-start.c)
???:?:?: 0x1816b74 in ??? (???)
???:?:?: 0x0 in ??? (???)
Aborted (core dumped)
Tried also running it on v0.12.0 as well as 0.12.x branch. The result is the same.
Expected Behavior
The compiler should give a compilation error or compile the code.
No repo on windows.
zig-windows-x86_64-0.13.0-dev.266+0b0625ccf\zig run wat.zig
wat.zig:30:49: error: error union type '@typeInfo(@typeInfo(@TypeOf(wat.MLP(3,2,.{ 16, 16, 1 }).init)).Fn.return_type.?).ErrorUnion.error_set!wat.MLP(3,2,.{ 16, 16, 1 })' does not support field access
std.debug.print("mlp.layers.len {}\n", .{mlp.layers.len});
~~~^~~~~~~
wat.zig:30:49: note: consider using 'try', 'catch', or 'if'
referenced by:
callMain: zig-windows-x86_64-0.13.0-dev.266+0b0625ccf\lib\std\start.zig:524:32
WinStartup: zig-windows-x86_64-0.13.0-dev.266+0b0625ccf\lib\std\start.zig:363:45
remaining reference traces hidden; use '-freference-trace' to see all reference traces
wat.zig:20:44: error: unable to evaluate comptime expression
const layer_type = Layer(sz[i], sz[i + 1]);
~~^~~
wat.zig:20:45: note: operation is runtime due to this operand
const layer_type = Layer(sz[i], sz[i + 1]);
^
I was able to reproduce on Ubuntu 22.04 from 0b0625ccf4:
0b0625ccf4
run
└─ run zig_crash
└─ zig build-exe zig_crash Debug native failure
error: thread 975505 panic: reached unreachable code
Analyzing src/main.zig: main.zig:MLP(3,2,.{ 16, 16, 1 }).init
%82 = extended(closure_get(1)) node_offset:20:42 to :20:44
%83 = dbg_stmt(4, 44)
> %84 = elem_val_node(%82, %73) node_offset:20:42 to :20:47
%85 = break_inline(%81, %84)
For full context, use the command
zig ast-check -t src/main.zig
in src/main.zig: main.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %81 = call(.auto, %79, [
{%82..%85},
{%86..%91},
]) node_offset:20:36 to :20:59
in src/main.zig: main.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %75 = condbr(%74, {%77..%110}, {%111}) node_offset:19:13 to :19:16
in src/main.zig: main.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %76 = block({%74, %75}) node_offset:19:13 to :19:16
in src/main.zig: main.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %72 = loop({%73..%114}) node_offset:19:13 to :19:16
/home/lillis/zig/lib/std/debug.zig:403:14: 0x596787c in assert (zig)
if (!ok) unreachable; // assertion failure
^
/home/lillis/zig/src/Sema.zig:27184:11: 0x68934f5 in panicIndexOutOfBounds (zig)
assert(!parent_block.is_comptime);
^
/home/lillis/zig/src/Sema.zig:28786:43: 0x6328f57 in elemValArray (zig)
try sema.panicIndexOutOfBounds(block, src, elem_index, len_inst, cmp_op);
^
/home/lillis/zig/src/Sema.zig:28583:43: 0x6bd10fe in elemVal (zig)
.Array => return sema.elemValArray(block, src, indexable_src, indexable, elem_index_src, elem_index, oob_safety),
^
/home/lillis/zig/src/Sema.zig:10848:24: 0x67167fa in zirElemValNode (zig)
return sema.elemVal(block, src, array, elem_index, elem_index_src, true);
^
/home/lillis/zig/src/Sema.zig:1028:69: 0x6200f1a in analyzeBodyInner (zig)
.elem_val_node => try sema.zirElemValNode(block, inst),
^
/home/lillis/zig/src/Sema.zig:910:30: 0x5f2f737 in analyzeInlineBody (zig)
if (sema.analyzeBodyInner(block, body)) |_| {
^
/home/lillis/zig/src/Sema.zig:936:39: 0x5c88c5e in resolveInlineBody (zig)
return (try sema.analyzeInlineBody(block, body, break_target)) orelse .unreachable_value;
^
/home/lillis/zig/src/Sema.zig:7426:65: 0x6dfdb64 in analyzeArg (zig)
const uncoerced_arg = try sema.resolveInlineBody(block, arg_body, zir_call.call_inst);
^
/home/lillis/zig/src/Sema.zig:8116:56: 0x6dfbec5 in analyzeInlineCallArg (zig)
const casted_arg = try args_info.analyzeArg(ics.caller(), arg_block, arg_i.*, Type.fromInterned(param_ty), func_ty_info, func_inst);
^
/home/lillis/zig/src/Sema.zig:7827:62: 0x6880992 in analyzeCall (zig)
const opt_noreturn_ref = try analyzeInlineCallArg(
^
/home/lillis/zig/src/Sema.zig:7136:43: 0x671189e in zirCall__anon_93341 (zig)
const call_inst = try sema.analyzeCall(block, func, func_ty, callee_src, call_src, modifier, ensure_result_used, args_info, call_dbg_node, .call);
^
/home/lillis/zig/src/Sema.zig:1014:62: 0x6200607 in analyzeBodyInner (zig)
.call => try sema.zirCall(block, inst, .direct),
^
/home/lillis/zig/src/Sema.zig:874:26: 0x6c1b8cf in analyzeBodyRuntimeBreak (zig)
sema.analyzeBodyInner(block, body) catch |err| switch (err) {
^
/home/lillis/zig/src/Sema.zig:19287:37: 0x67c7217 in zirCondbr (zig)
try sema.analyzeBodyRuntimeBreak(&sub_block, then_body);
^
/home/lillis/zig/src/Sema.zig:1693:39: 0x620ca2a in analyzeBodyInner (zig)
try sema.zirCondbr(block, inst);
^
/home/lillis/zig/src/Sema.zig:6176:34: 0x6c1bf7b in resolveBlockBody (zig)
if (sema.analyzeBodyInner(child_block, body)) |_| {
^
/home/lillis/zig/src/Sema.zig:6153:33: 0x67cc55e in zirBlock (zig)
return sema.resolveBlockBody(parent_block, src, &child_block, body, inst, &label.merges);
^
/home/lillis/zig/src/Sema.zig:1559:49: 0x620ea80 in analyzeBodyInner (zig)
break :blk try sema.zirBlock(block, inst, tags[@intFromEnum(inst)] == .block_comptime);
^
/home/lillis/zig/src/Sema.zig:5968:30: 0x67c382f in zirLoop (zig)
try sema.analyzeBodyInner(&loop_block, body);
^
/home/lillis/zig/src/Sema.zig:1534:68: 0x620b8c6 in analyzeBodyInner (zig)
if (!block.is_comptime) break :blk try sema.zirLoop(block, inst);
^
/home/lillis/zig/src/Sema.zig:892:26: 0x61ff241 in analyzeFnBody (zig)
sema.analyzeBodyInner(block, body) catch |err| switch (err) {
^
/home/lillis/zig/src/Module.zig:4633:23: 0x5eed20f in analyzeFnBody (zig)
sema.analyzeFnBody(&inner_block, fn_info.body) catch |err| switch (err) {
^
/home/lillis/zig/src/Module.zig:3143:32: 0x5c57578 in ensureFuncBodyAnalyzed (zig)
var air = zcu.analyzeFnBody(func_index, sema_arena) catch |err| switch (err) {
^
/home/lillis/zig/src/Compilation.zig:3414:42: 0x5c55092 in processOneJob (zig)
module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
^
/home/lillis/zig/src/Compilation.zig:3354:30: 0x5a8a22f in performAllTheWork (zig)
try processOneJob(comp, work_item, main_progress_node);
^
/home/lillis/zig/src/Compilation.zig:2132:31: 0x5a85c33 in update (zig)
try comp.performAllTheWork(main_progress_node);
^
/home/lillis/zig/src/main.zig:4091:36: 0x5b0074c in serve (zig)
try comp.update(main_progress_node);
^
/home/lillis/zig/src/main.zig:3368:22: 0x5b1ee8f in buildOutputType (zig)
try serve(
^
/home/lillis/zig/src/main.zig:262:31: 0x5969ab1 in mainArgs (zig)
return buildOutputType(gpa, arena, args, .{ .build = .Exe });
^
/home/lillis/zig/src/main.zig:208:20: 0x5966a45 in main (zig)
return mainArgs(gpa, arena, args);
^
/home/lillis/zig/lib/std/start.zig:524:37: 0x59664de in main (zig)
const result = root.main() catch |err| {
^
???:?:?: 0x7f66dc461d8f in ??? (libc.so.6)
Unwind information for `libc.so.6:0x7f66dc461d8f` was not available, trace may be incomplete
error: the following command terminated unexpectedly:
/home/lillis/zig/build-266/stage3/bin/zig build-exe -ODebug -Mroot=/home/lillis/projects/zig_crash/src/main.zig --cache-dir /home/lillis/projects/zig_crash/zig-cache --global-cache-dir /home/lillis/.cache/zig --name zig_crash --listen=-
Build Summary: 2/7 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run zig_crash transitive failure
├─ zig build-exe zig_crash Debug native failure
└─ install transitive failure
└─ install zig_crash transitive failure
└─ zig build-exe zig_crash Debug native (reused)
error: the following build command failed with exit code 1:
/home/lillis/projects/zig_crash/zig-cache/o/079169a96abe2bbeacb8b7ac264a92cb/build /home/lillis/zig/build-266/stage3/bin/zig /home/lillis/projects/zig_crash /home/lillis/projects/zig_crash/zig-cache /home/lillis/.cache/zig --seed 0xa2b0d719 -Z4214ba78a95522ad run
as well as an older build from master branch commit 67455c5e70e86dbb7805ff9a415f1b13b14f36da:
67455c5e70
run
└─ run zig_crash
└─ zig build-exe zig_crash Debug native failure
error: thread 931437 panic: reached unreachable code
Analyzing src/main.zig: main.zig:MLP(3,2,.{ 16, 16, 1 }).init
%82 = extended(closure_get(1)) node_offset:20:42 to :20:44
%83 = dbg_stmt(4, 44)
> %84 = elem_val_node(%82, %73) node_offset:20:42 to :20:47
%85 = break_inline(%81, %84)
For full context, use the command
zig ast-check -t src/main.zig
in src/main.zig: main.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %81 = call(.auto, %79, [
{%82..%85},
{%86..%91},
]) node_offset:20:36 to :20:59
in src/main.zig: main.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %75 = condbr(%74, {%77..%110}, {%111}) node_offset:19:13 to :19:16
in src/main.zig: main.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %76 = block({%74, %75}) node_offset:19:13 to :19:16
in src/main.zig: main.zig:MLP(3,2,.{ 16, 16, 1 }).init
> %72 = loop({%73..%114}) node_offset:19:13 to :19:16
/home/lillis/zig/lib/std/debug.zig:403:14: 0x5cfc7bc in assert (zig)
if (!ok) unreachable; // assertion failure
^
/home/lillis/zig/src/Sema.zig:27169:11: 0x6c12db5 in panicIndexOutOfBounds (zig)
assert(!parent_block.is_comptime);
^
/home/lillis/zig/src/Sema.zig:28768:43: 0x66b54e7 in elemValArray (zig)
try sema.panicIndexOutOfBounds(block, src, elem_index, len_inst, cmp_op);
^
/home/lillis/zig/src/Sema.zig:28565:43: 0x6f4ec7e in elemVal (zig)
.Array => return sema.elemValArray(block, src, indexable_src, indexable, elem_index_src, elem_index, oob_safety),
^
/home/lillis/zig/src/Sema.zig:10833:24: 0x6a99c4a in zirElemValNode (zig)
return sema.elemVal(block, src, array, elem_index, elem_index_src, true);
^
/home/lillis/zig/src/Sema.zig:1028:69: 0x658f029 in analyzeBodyInner (zig)
.elem_val_node => try sema.zirElemValNode(block, inst),
^
/home/lillis/zig/src/Sema.zig:910:30: 0x62beaf7 in analyzeInlineBody (zig)
if (sema.analyzeBodyInner(block, body)) |_| {
^
/home/lillis/zig/src/Sema.zig:936:39: 0x6019efe in resolveInlineBody (zig)
return (try sema.analyzeInlineBody(block, body, break_target)) orelse .unreachable_value;
^
/home/lillis/zig/src/Sema.zig:7412:65: 0x71744cf in analyzeArg (zig)
const uncoerced_arg = try sema.resolveInlineBody(block, arg_body, zir_call.call_inst);
^
/home/lillis/zig/src/Sema.zig:8102:56: 0x7172825 in analyzeInlineCallArg (zig)
const casted_arg = try args_info.analyzeArg(ics.caller(), arg_block, arg_i.*, Type.fromInterned(param_ty), func_ty_info, func_inst);
^
/home/lillis/zig/src/Sema.zig:7813:62: 0x6c00242 in analyzeCall (zig)
const opt_noreturn_ref = try analyzeInlineCallArg(
^
/home/lillis/zig/src/Sema.zig:7122:43: 0x6a94cee in zirCall__anon_93773 (zig)
const call_inst = try sema.analyzeCall(block, func, func_ty, callee_src, call_src, modifier, ensure_result_used, args_info, call_dbg_node, .call);
^
/home/lillis/zig/src/Sema.zig:1014:62: 0x658e716 in analyzeBodyInner (zig)
.call => try sema.zirCall(block, inst, .direct),
^
/home/lillis/zig/src/Sema.zig:874:26: 0x6f992cf in analyzeBodyRuntimeBreak (zig)
sema.analyzeBodyInner(block, body) catch |err| switch (err) {
^
/home/lillis/zig/src/Sema.zig:19272:37: 0x6b4a717 in zirCondbr (zig)
try sema.analyzeBodyRuntimeBreak(&sub_block, then_body);
^
/home/lillis/zig/src/Sema.zig:1693:39: 0x659ab39 in analyzeBodyInner (zig)
try sema.zirCondbr(block, inst);
^
/home/lillis/zig/src/Sema.zig:6162:34: 0x6f9997b in resolveBlockBody (zig)
if (sema.analyzeBodyInner(child_block, body)) |_| {
^
/home/lillis/zig/src/Sema.zig:6139:33: 0x6b4fa6e in zirBlock (zig)
return sema.resolveBlockBody(parent_block, src, &child_block, body, inst, &label.merges);
^
/home/lillis/zig/src/Sema.zig:1559:49: 0x659cb8f in analyzeBodyInner (zig)
break :blk try sema.zirBlock(block, inst, tags[@intFromEnum(inst)] == .block_comptime);
^
/home/lillis/zig/src/Sema.zig:5954:30: 0x6b46cef in zirLoop (zig)
try sema.analyzeBodyInner(&loop_block, body);
^
/home/lillis/zig/src/Sema.zig:1534:68: 0x65999d5 in analyzeBodyInner (zig)
if (!block.is_comptime) break :blk try sema.zirLoop(block, inst);
^
/home/lillis/zig/src/Sema.zig:892:26: 0x658d351 in analyzeFnBody (zig)
sema.analyzeBodyInner(block, body) catch |err| switch (err) {
^
/home/lillis/zig/src/Module.zig:4633:23: 0x627e025 in analyzeFnBody (zig)
sema.analyzeFnBody(&inner_block, fn_info.body) catch |err| switch (err) {
^
/home/lillis/zig/src/Module.zig:3143:32: 0x5ff1228 in ensureFuncBodyAnalyzed (zig)
var air = zcu.analyzeFnBody(func_index, sema_arena) catch |err| switch (err) {
^
/home/lillis/zig/src/Compilation.zig:3414:42: 0x5feef32 in processOneJob (zig)
module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
^
/home/lillis/zig/src/Compilation.zig:3354:30: 0x5e1f14f in performAllTheWork (zig)
try processOneJob(comp, work_item, main_progress_node);
^
/home/lillis/zig/src/Compilation.zig:2132:31: 0x5e1abb2 in update (zig)
try comp.performAllTheWork(main_progress_node);
^
/home/lillis/zig/src/main.zig:4085:36: 0x5e955ec in serve (zig)
try comp.update(main_progress_node);
^
/home/lillis/zig/src/main.zig:3362:22: 0x5eb3d2f in buildOutputType (zig)
try serve(
^
/home/lillis/zig/src/main.zig:260:31: 0x5cfe9f1 in mainArgs (zig)
return buildOutputType(gpa, arena, args, .{ .build = .Exe });
^
/home/lillis/zig/src/main.zig:206:20: 0x5cfb985 in main (zig)
return mainArgs(gpa, arena, args);
^
/home/lillis/zig/lib/std/start.zig:511:37: 0x5cfb41e in main (zig)
const result = root.main() catch |err| {
^
???:?:?: 0x7faccc06bd8f in ??? (libc.so.6)
Unwind information for `libc.so.6:0x7faccc06bd8f` was not available, trace may be incomplete
error: the following command terminated unexpectedly:
/home/lillis/zig/build/stage3/bin/zig build-exe -ODebug -Mroot=/home/lillis/projects/zig_crash/src/main.zig --cache-dir /home/lillis/projects/zig_crash/zig-cache --global-cache-dir /home/lillis/.cache/zig --name zig_crash --listen=-
Build Summary: 2/7 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run zig_crash transitive failure
├─ zig build-exe zig_crash Debug native failure
└─ install transitive failure
└─ install zig_crash transitive failure
└─ zig build-exe zig_crash Debug native (reused)
error: the following build command failed with exit code 1:
/home/lillis/projects/zig_crash/zig-cache/o/6941b1309374dc38b3da16ae3f31f289/build /home/lillis/zig/build/stage3/bin/zig /home/lillis/projects/zig_crash /home/lillis/projects/zig_crash/zig-cache /home/lillis/.cache/zig --seed 0xf1a8e47a -Z652b9a9fcae50346 run
This issue seems to be fixed in the latest master. The code in the first post no longer crashes:
$ build/stage3/bin/zig build-obj test1.zig
test1.zig:20:44: error: unable to resolve comptime value
const layer_type = Layer(sz[i], sz[i + 1]);
~~^~~
test1.zig:20:44: note: argument to comptime parameter must be comptime-known
test1.zig:3:14: note: parameter declared comptime here
pub fn Layer(comptime M: usize, comptime N: usize) type {
^~~~~~~~
test1.zig:30:49: error: error union type '@typeInfo(@typeInfo(@TypeOf(test1.MLP(3,2,.{ 16, 16, 1 }).init)).@"fn".return_type.?).error_union.error_set!test1.MLP(3,2,.{ 16, 16, 1 })' does not support field access
std.debug.print("mlp.layers.len {}\n", .{mlp.layers.len});
~~~^~~~~~~
test1.zig:30:49: note: consider using 'try', 'catch', or 'if'
referenced by:
posixCallMainAndExit: lib/std/start.zig:656:37
_start: lib/std/start.zig:464:40
3 reference(s) hidden; use '-freference-trace=5' to see all references
Nope, definitely still crashes. You're probably using a release compiler and got lucky with the UB.