zig
zig copied to clipboard
Compile crash
Zig Version
0.14.0-dev.1200+104553714
Steps to Reproduce and Observed Behavior
Create a new Zig project directory, and replace the main file content with the following :
const std = @import("std");
pub const StackAllocator = struct {
data: [40]u8 = undefined,
offset: usize = 0,
const Self = @This();
fn alloc(ctx: *anyopaque, n: usize, ptr_align: u8, return_address: usize) ?[*]u8 {
const self: *Self = @ptrCast(@alignCast(ctx));
// _ = n;
// _ = self;
_ = ptr_align;
_ = return_address;
const data = self.data[self.offset .. self.offset + n];
return data.ptr + n;
// return null;
}
pub fn allocator(comptime self: *Self) std.mem.Allocator {
return std.mem.Allocator{ .ptr = self, .vtable = &.{ .alloc = alloc, .free = undefined, .resize = undefined } };
}
};
pub const HandlerFn =
*const fn () void;
pub fn main() !void {
comptime {
var stack_allocator = StackAllocator{};
const allocator = stack_allocator.allocator();
const value =
try allocator.create(HandlerFn);
const handle_fn = struct {
fn handle() void {}
}.handle;
value.* = handle_fn;
}
}
Run zig build
Expected Behavior
The code to compile or at least to throw a real error (I think the issue comes from setting the std.mem.Allocator VTable's functions to undefined). When compiling, I get the following error :
install
└─ install zig-tiny-http
└─ zig build-exe zig-tiny-http Debug native failure
error: the following command terminated unexpectedly:
/usr/local/bin/zig build-exe -ODebug -Mroot=/home/riccardo/tiny-http-zig-reduced/src/main.zig --cache-dir /home/riccardo/tiny-http-zig-reduced/.zig-cache --global-cache-dir /home/riccardo/.cache/zig --name zig-tiny-http --zig-lib-dir /usr/local/bin/lib/ --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install zig-tiny-http transitive failure
└─ zig build-exe zig-tiny-http Debug native failure
error: the following build command failed with exit code 1:
/home/riccardo/tiny-http-zig-reduced/.zig-cache/o/1b6808dabce3e4d55c3311488c60b910/build /usr/local/bin/zig /usr/local/bin/lib /home/riccardo/tiny-http-zig-reduced /home/riccardo/tiny-http-zig-reduced/.zig-cache /home/riccardo/.cache/zig --seed 0x42d96380 -Z957a6ec646f3fd6e
When using this code :
const std = @import("std");
pub const StackAllocator = struct {
data: [40]u8 = undefined,
offset: usize = 0,
const Self = @This();
fn alloc(ctx: *anyopaque, n: usize, ptr_align: u8, return_address: usize) ?[*]u8 {
const self: *Self = @ptrCast(@alignCast(ctx));
_ = n;
_ = self;
_ = ptr_align;
_ = return_address;
// const data = self.data[self.offset .. self.offset + n];
// return data.ptr + n;
return null;
}
pub fn allocator(comptime self: *Self) std.mem.Allocator {
return std.mem.Allocator{ .ptr = self, .vtable = &.{ .alloc = alloc, .free = undefined, .resize = undefined } };
}
};
pub const HandlerFn =
*const fn () void;
pub fn main() !void {
comptime {
var stack_allocator = StackAllocator{};
const allocator = stack_allocator.allocator();
const value =
try allocator.create(HandlerFn);
const handle_fn = struct {
fn handle() void {}
}.handle;
value.* = handle_fn;
}
}
The diff is :
fn alloc(ctx: *anyopaque, n: usize, ptr_align: u8, return_address: usize) ?[*]u8 {
const self: *Self = @ptrCast(@alignCast(ctx));
- // _ = n;
- // _ = self;
+ _ = n;
+ _ = self;
_ = ptr_align;
_ = return_address;
- const data = self.data[self.offset .. self.offset + n];
+ // const data = self.data[self.offset .. self.offset + n];
- return data.ptr + n;
- // return null;
+ // return data.ptr + n;
+ return null;
}
pub fn allocator(comptime self: *Self) std.mem.Allocator {
There is a compiler error :
install
└─ install zig-tiny-http
└─ zig build-exe zig-tiny-http Debug native 1 errors
src/main.zig:38:13: error: function called at runtime cannot return value at comptime
try allocator.create(HandlerFn);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
posixCallMainAndExit: /usr/local/bin/lib/std/start.zig:614:37
_start: /usr/local/bin/lib/std/start.zig:421:40
3 reference(s) hidden; use '-freference-trace=5' to see all references
error: the following command failed with 1 compilation errors:
/usr/local/bin/zig build-exe -ODebug -Mroot=/home/riccardo/tiny-http-zig-reduced/src/main.zig --cache-dir /home/riccardo/tiny-http-zig-reduced/.zig-cache --global-cache-dir /home/riccardo/.cache/zig --name zig-tiny-http --zig-lib-dir /usr/local/bin/lib/ --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install zig-tiny-http transitive failure
└─ zig build-exe zig-tiny-http Debug native 1 errors
error: the following build command failed with exit code 1:
/home/riccardo/tiny-http-zig-reduced/.zig-cache/o/1b6808dabce3e4d55c3311488c60b910/build /usr/local/bin/zig /usr/local/bin/lib /home/riccardo/tiny-http-zig-reduced /home/riccardo/tiny-http-zig-reduced/.zig-cache /home/riccardo/.cache/zig --seed 0x8c540e68 -Za55a3e4c4a160d32
Duplicate of #20765