zig icon indicating copy to clipboard operation
zig copied to clipboard

Compile error at "LLVM Emit Object" step

Open crat1985 opened this issue 1 year ago • 4 comments

Zig Version

0.14.0-dev.1200+104553714

Steps to Reproduce and Observed Behavior

Create a file with the following content :

const http = struct {
    pub const Version = undefined;

    const Headers = undefined;

    pub const Request = struct {};

    pub const StatusCode = undefined;

    pub const Response = struct {};

    pub const Method = enum {};

    pub const HandlerFn =
        *const fn (req: *Request(void, void), args: []const []const u8) Response;

    pub fn NodeAllocator(comptime node_count: usize) type {
        return struct {
            data: [node_count]Node(node_count) = undefined,
        };
    }

    pub fn StackStringHashMap(comptime size: usize) type {
        return struct {
            keys: [size][]const u8 = undefined,
        };
    }

    pub fn Node(comptime node_children_count: usize) type {
        return struct {
            name: []const u8,
            is_arg: bool,
            children: ?StackStringHashMap(node_children_count),
        };
    }

    pub fn Router(comptime node_count: usize) type {
        return struct {
            root_node: Node(node_count),
            node_allocator: NodeAllocator(node_count),

            const Self = @This();

            pub fn init() Self {
                return .{
                    .root_node = .{
                        .is_arg = false,
                        .name = "/",
                        .children = null,
                    },
                    .node_allocator = NodeAllocator(node_count){},
                };
            }
        };
    }
};
const std = @import("std");

pub fn main() !void {
    comptime var router = http.Router(400 * 400).init();
    router = router;

    const router_const = router;
    _ = router_const;
}

Build it : zig build-exe <file>

Expected Behavior

The code to compile.

The full error is :

error: error: <unknown>:0: invalid number of bytes


error: the following command exited with error code 1:
/usr/local/bin/zig build-exe -ODebug -Mroot=/home/riccardo/zig-tiny-http-reduced/src/main.zig --cache-dir /home/riccardo/zig-tiny-http-reduced/.zig-cache --global-cache-dir /home/riccardo/.cache/zig --name zig-tiny-http --zig-lib-dir /usr/local/bin/lib/ --listen=-

crat1985 avatar Aug 19 '24 16:08 crat1985

http.Router(400 * 400) is 1.5 GB big and it's triggering an int cast safety check.

Reduction:

const S = struct {
    a: [1585786912]u64 = undefined,

    fn init(comptime _: void) S {
        return .{};
    }
};
pub export fn entry() void {
    comptime var router = S.init({});
    _ = &router;
}
Stack trace:

thread 10812 panic: integer cast truncated bits
Analyzing a.zig
      %15 = ret_type() node_offset:6:5 to :6:7
      %16 = dbg_stmt(2, 9)
    > %17 = ret_ptr() node_offset:7:9 to :7:19
      %18 = typeof(%17) node_offset:7:16 to :7:19
      %19 = elem_type(%18) node_offset:7:16 to :7:19
      %20 = struct_init_empty_result(%19) node_offset:7:16 to :7:19
      %21 = store_node(%17, %20) node_offset:7:16 to :7:19
      %22 = restore_err_ret_index_unconditional(.none) node_offset:7:9 to :7:19
      %23 = dbg_stmt(2, 9)
      %24 = ret_load(%17) node_offset:7:9 to :7:19
    For full context, use the command
      zig ast-check -t a.zig

  in a.zig
    > %34 = field_call(.compile_time, %32, "init", [
        {%35},
      ]) node_offset:11:27 to :11:37
  in a.zig
    > %31 = block_comptime({%32..%36}) node_offset:11:27 to :11:37

/home/vexu/Documents/zig/zig/src/Sema.zig:35454:39: 0xa8d5124 in resolveStructLayout (zig)
    struct_type.setLayoutResolved(ip, @intCast(big_align.forward(offset)), big_align);
                                      ^
/home/vexu/Documents/zig/zig/src/Type.zig:3788:44: 0xa59e31d in resolveStructInner (zig)
        .layout => sema.resolveStructLayout(ty),
                                           ^
/home/vexu/Documents/zig/zig/src/Type.zig:3547:57: 0xa30b88d in resolveLayout (zig)
            .struct_type => return ty.resolveStructInner(pt, .layout),
                                                        ^
/home/vexu/Documents/zig/zig/src/Sema.zig:37131:41: 0xa8ceb80 in typeHasOnePossibleValue (zig)
                    try ty.resolveLayout(pt);
                                        ^
/home/vexu/Documents/zig/zig/src/Sema.zig:37311:41: 0xb2108db in analyzeComptimeAlloc (zig)
    _ = try sema.typeHasOnePossibleValue(var_type);
                                        ^
/home/vexu/Documents/zig/zig/src/Sema.zig:3460:41: 0xadd01cf in zirRetPtr (zig)
        return sema.analyzeComptimeAlloc(block, sema.fn_ret_ty, .none);
                                        ^
/home/vexu/Documents/zig/zig/src/Sema.zig:1209:44: 0xa8c598b in analyzeBodyInner (zig)
            .ret_ptr  => try sema.zirRetPtr(block),
                                           ^
/home/vexu/Documents/zig/zig/src/Sema.zig:905:26: 0xb1c9ef1 in analyzeFnBody (zig)
    sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                         ^
/home/vexu/Documents/zig/zig/src/Sema.zig:7892:35: 0xae33d2c in analyzeCall (zig)
                sema.analyzeFnBody(&child_block, fn_info.body) catch |err| switch (err) {
                                  ^
/home/vexu/Documents/zig/zig/src/Sema.zig:7094:43: 0xad5cdb5 in zirCall__anon_335752 (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/vexu/Documents/zig/zig/src/Sema.zig:1036:62: 0xa8bf35d in analyzeBodyInner (zig)
            .field_call                   => try sema.zirCall(block, inst, .field),
                                                             ^
/home/vexu/Documents/zig/zig/src/Sema.zig:923:30: 0xa59a8a7 in analyzeInlineBody (zig)
    if (sema.analyzeBodyInner(block, body)) |_| {
                             ^

Vexu avatar Aug 19 '24 17:08 Vexu

Ah I didn't realise sorry 🫠 Thank you for your answer 💯

ghost avatar Aug 19 '24 17:08 ghost

Should I close this as resolved since I was the one doing something wrong, or should I keep it open since it's an LLVM error and not a Zig error ?

ghost avatar Aug 19 '24 17:08 ghost

The compiler crashing is a bug, it only makes it to LLVM using a release build without safety checks.

Vexu avatar Aug 19 '24 18:08 Vexu