Skip to content

Assertion failed at zig/src/stage1/buffer.hpp:37 in buf_ptr #9526

@pingiun

Description

@pingiun

I'm trying to interop with Objective C in Zig and encountered a compiler bug. I compiled zig myself to get the stacktrace, but have done no further investigations.

This is with the latest head version of the compiler (0.9.0-dev.729+bd8baefaa) but I encountered the same error with 0.8.0.

main.zig
const objc = @cImport({
    @cInclude("objc/runtime.h");
    @cInclude("objc/message.h");
    @cInclude("Carbon/Carbon.h");
});

const std = @import("std");
const TypeInfo = std.builtin.TypeInfo;

fn call(id: anytype, selector_name: [*:0]const u8, args: anytype, comptime return_type: type) return_type {
    comptime var FnArgs: [2 + args.len]TypeInfo.FnArg = undefined;
    FnArgs[0] = .{
        .is_generic = false,
        .is_noalias = false,
        .arg_type = objc.Class,
    };
    FnArgs[1] = .{
        .is_generic = false,
        .is_noalias = false,
        .arg_type = objc.SEL,
    };
    inline for (args) |arg, i| {
        FnArgs[2 + i] = .{
            .is_generic = false,
            .is_noalias = false,
            .arg_type = @TypeOf(arg),
        };
    }
    const func_type = @Type(TypeInfo{.Fn = TypeInfo.Fn{
        .alignment = 8,
        .calling_convention = .C,
        .is_generic = false,
        .is_var_args = false,
        .return_type = return_type,
        .args = &FnArgs,
    }});
    const selector = objc.sel_registerName(selector_name);
    return @call(.{}, @as(func_type, objc.objc_msgSend), .{ id, selector } ++ args);
}

pub fn main() !void {
    var app: objc.id = call(objc.objc_getClass("NSApplication"), "sharedApplication", .{}, objc.id);
    std.debug.print("app: {*}", .{app});
}
build.zig
const Builder = @import("std").build.Builder;

pub fn build(b: *Builder) void {
    const exe = b.addExecutable("test", "main.zig");
    exe.linkSystemLibrary("c");
    exe.linkFramework("Cocoa");
    exe.linkFramework("Foundation");

    exe.setBuildMode(b.standardReleaseOptions());

    exe.install();

    const run_cmd = exe.run();
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}
Stacktrace
Semantic Analysis [42/128] Assertion failed at /Users/jelle/dev/tmp/zig/src/stage1/buffer.hpp:37 in buf_ptr. This is a bug in the Zig compiler.thread 12800086 panic: 
/Users/jelle/dev/tmp/zig/src/main.zig:201:26: 0x1096f8ad3 in main.mainArgs (zig1)
    const cmd_args = args[2..];
                         ^
???:?:?: 0x10e9b1f6f in ??? (???)
???:?:?: 0x10a42824d in __ZL10zig_assertbPKciS0_ (???)
???:?:?: 0x10a428311 in __ZL7buf_ptrP3Buf (???)
???:?:?: 0x10a42e551 in __Z11get_fn_typeP7CodeGenP8FnTypeId (???)
???:?:?: 0x10a5638f3 in __ZL17type_info_to_typeP9IrAnalyzeP5ScopeP7AstNode9ZigTypeIdP8ZigValue (???)
???:?:?: 0x10a51352a in __ZL27ir_analyze_instruction_typeP9IrAnalyzeP17Stage1ZirInstType (???)
???:?:?: 0x10a4f3358 in __ZL27ir_analyze_instruction_baseP9IrAnalyzeP13Stage1ZirInst (???)
???:?:?: 0x10a4f1b4f in __Z10ir_analyzeP7CodeGenP9Stage1ZirP9Stage1AirPmS5_P7ZigTypeP7AstNodeP8ZigValueP5ZigFn (???)
???:?:?: 0x10a448d65 in __ZL13analyze_fn_irP7CodeGenP5ZigFnP7AstNode (???)
???:?:?: 0x10a435b76 in __ZL15analyze_fn_bodyP7CodeGenP5ZigFn (???)
???:?:?: 0x10a436c96 in __Z16semantic_analyzeP7CodeGen (???)
???:?:?: 0x10a4a041b in __ZL15gen_root_sourceP7CodeGen (???)
???:?:?: 0x10a49ed4f in __Z20codegen_build_objectP7CodeGen (???)
???:?:?: 0x10a585576 in _zig_stage1_build_object (???)
/Users/jelle/dev/tmp/zig/lib/std/array_hash_map.zig:1566:21: 0x109af1514 in std.array_hash_map.ArrayHashMapUnmanaged([]const u8,musl.Ext,std.array_hash_map.StringContext,true).insertAllEntriesIntoNewHeaderGeneric (zig1)
                    distance_from_start_index += 1;
                    ^
/Users/jelle/dev/tmp/zig/src/Compilation.zig:3955:31: 0x109891cc3 in Compilation.buildOutputFromZig (zig1)
    try sub_compilation.update();
                              ^
/Users/jelle/dev/tmp/zig/src/Module.zig:3141:0: 0x109864e26 in Module.importPkg (zig1)
pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {

/Users/jelle/dev/tmp/zig/lib/std/multi_array_list.zig:46:33: 0x10985ec56 in std.multi_array_list.Slice.items (zig1)
                    return &[_]F{};
                                ^
/Users/jelle/dev/tmp/zig/src/main.zig:2590:9: 0x1097f8420 in main.cmdTranslateC (zig1)
        try io.getStdOut().writeFileAll(zig_file, .{});
        ^
/Users/jelle/dev/tmp/zig/src/main.zig:1861:35: 0x1097233b9 in main.buildOutputType (zig1)
    defer emit_asm_resolved.deinit();
                                  ^
/Users/jelle/dev/tmp/zig/src/main.zig:0:5: 0x1096f70bb in main.mainArgs (zig1)
/Users/jelle/dev/tmp/zig/lib/std/debug.zig:277:57: 0x1096f6afa in std.debug.panicExtra (zig1)
                    stderr.print("thread {} panic: ", .{current_thread_id}) catch os.abort();
                                                        ^
???:?:?: 0x7fff204caf5c in ??? (???)
???:?:?: 0xe in ??? (???)
test...The following command terminated unexpectedly:
/Users/jelle/dev/tmp/zig/build/zig build-exe /Users/jelle/dev/tmp/zigmacostest/main.zig -lc --cache-dir /Users/jelle/dev/tmp/zigmacostest/zig-cache --global-cache-dir /Users/jelle/.cache/zig --name test -framework Foundation -framework Cocoa --enable-cache 
error: the following build command failed with exit code 6:
/Users/jelle/dev/tmp/zigmacostest/zig-cache/o/da0254314ce5f2e2a65f0a2d5572d716/build /Users/jelle/dev/tmp/zig/build/zig /Users/jelle/dev/tmp/zigmacostest /Users/jelle/dev/tmp/zigmacostest/zig-cache /Users/jelle/.cache/zig run

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorstage1The process of building from source via WebAssembly and the C backend.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions