Skip to content

broken LLVM module on use of optional slice #4907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
daurnimator opened this issue Apr 2, 2020 · 4 comments
Closed

broken LLVM module on use of optional slice #4907

daurnimator opened this issue Apr 2, 2020 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@daurnimator
Copy link
Contributor

const std = @import("std");
const testing = std.testing;
test "crash" {
    const foo = struct {
        pub fn bar(self: *@This()) ?[]u8 {
            return null;
        }
    }{};
    const expected = @as(?[]const u8, null);
    const actual = foo.bar();
    testing.expectEqual(expected, actual);
}
$ zig test bad-codegen.zig 
Code Generation [494/713] std.mem.Allocator.alignedAlloc...broken LLVM module found: Call parameter type does not match function signature!
  %actual = alloca %"?[]u8", align 8
 %"?[]const u8"*  call fastcc void @std.testing.expectEqual(%"?[]const u8"* @65, %"?[]u8"* %actual), !dbg !2101

This is a bug in the Zig compiler.
/home/daurnimator/src/zig/src-self-hosted/stage2.zig:37:5: 0x5586e997fe3c in stage2_panic (zigstage2)
    @panic(ptr[0..len]);
    ^
/home/daurnimator/src/zig/src/util.cpp:20:17: 0x5586e993b489 in zig_panic (/home/daurnimator/src/zig/src/util.cpp)
    stage2_panic("", 0);
                ^
/home/daurnimator/src/zig/src/codegen.cpp:7997:18: 0x5586e987581c in do_code_gen (/home/daurnimator/src/zig/src/codegen.cpp)
    if (LLVMVerifyModule(g->module, LLVMReturnStatusAction, &error)) {
                 ^
/home/daurnimator/src/zig/src/codegen.cpp:10774:24: 0x5586e9880afb in codegen_build_and_link (/home/daurnimator/src/zig/src/codegen.cpp)

                       ^
/home/daurnimator/src/zig/src/main.cpp:1669:39: 0x5586e98514ea in main0 (/home/daurnimator/src/zig/src/main.cpp)
                codegen_translate_c(g, in_file_buf);
                                      ^
/home/daurnimator/src/zig/src/main.cpp:1747:24: 0x5586e985197d in main (/home/daurnimator/src/zig/src/main.cpp)
        return stage2_cmd_targets(target_string, mcpu, dynamic_linker);
                       ^
???:?:?: 0x7f346be58022 in ??? (???)
Aborted (core dumped)

Possible duplicate of #4904

@daurnimator daurnimator added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Apr 2, 2020
@LemonBoy
Copy link
Contributor

LemonBoy commented Apr 3, 2020

The test case can be further simplified:

fn fu(x: var, y: @TypeOf(x)) void {}
test "crash" {
    var x0: ?[]const u8 = null;
    var x1: ?[]u8 = null;
    fu(x0, x1);
}

@shakesoda
Copy link

certainly not simpler, but looks like I ran into this when updating zig-vulkan-triangle, too: https://gist.github.com/shakesoda/28eb50428eb683ce677b8b1903a57473

@andrewrk andrewrk added this to the 0.7.0 milestone Apr 7, 2020
@xackus
Copy link
Contributor

xackus commented Apr 25, 2020

Even simpler case:

fn fu(x: ?[]const u8) void {}

export fn bar() void {
    var x1: ?[]u8 = null;
    fu(x1);
}

The problem is caused by zig generating two different LLVM types for const and non-const optional slices.

@Vexu
Copy link
Member

Vexu commented Dec 30, 2022

$ git grep "cast slice to const slice" test
test/behavior/optional.zig:test "cast slice to const slice nested in error union and optional" {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

6 participants