Skip to content
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

broken LLVM module with this code #3422

Closed
marler8997 opened this issue Oct 9, 2019 · 3 comments
Closed

broken LLVM module with this code #3422

marler8997 opened this issue Oct 9, 2019 · 3 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

@marler8997
Copy link
Contributor

marler8997 commented Oct 9, 2019

pub const Struct = struct {
    pub fn method(self: *@This()) ![]const u8 {
       return error.a;
    }
};
pub fn main() void {
    const optionalString : ?[]const u8 = "hello";
    if (optionalString) |s| {
        var foo = Struct {};
        s = foo.method();
    }
}
> zig build-exe bug.zig
broken LLVM module found: Call parameter type does not match function signature!
%"[]u8"* @328
 { %"[]u8", i16 }*  call fastcc void @Struct.method(%"[]u8"* sret @328, %builtin.StackTrace* %error_return_trace), !dbg !14121

Unable to dump stack trace: debug info stripped
Aborted (core dumped)

@gustavolsson provided another example that is a bit more reduced:

fn errorAndSlice() ![]const u8 {
    return error.a;
}

fn indirect() []const u8 {
    return errorAndSlice();
}

test "causes LLVM broken module" {
    var slice: []const u8 = indirect(); // gets to code generation and fails with LLVM error
}
@gustavolsson
Copy link
Contributor

I have the same problem with slices and error sets:

fn errorAndSlice() ![]const u8 {
    return error.a;
}

fn indirect() []const u8 {
    return errorAndSlice();
}

// test "catches error" {
//     var slice: []const u8 = errorAndSlice(); // properly catches the mistake (no error handling)
// }

test "doesnt catch error" {
    var slice: []const u8 = indirect(); // gets to code generation and fails with LLVM error
}

Note that this does work with primitive types:

fn errorAndNumber() !u8 {
    return error.a;
}

fn indirect() u8 {
    return errorAndNumber();
}

test "catches error" {
    var num: u8 = errorAndNumber(); // properly catches the mistake (no error handling)
}

test "doesnt catch error" {
    var num: u8 = indirect(); // properly catches the mistake (no error handling)
}

@marler8997
Copy link
Contributor Author

marler8997 commented Oct 11, 2019

@gustavolsson yeah it looks like it's probably the same issue. Zig doesn't seem to be detecting that an error union with a slice being assigned to a normal slice without an error union is an error. Since Zig doesn't detect it, the error doesn't get caught until LLVM takes over.

I tried u8 as well in my example and Zig detects the error in that case, so the type matters for some reason.

@andrewrk andrewrk added this to the 0.6.0 milestone Oct 14, 2019
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Oct 14, 2019
@marler8997
Copy link
Contributor Author

Latest compiler seems to correctly detect this as a compile error now, thanks for fixing!

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

Successfully merging a pull request may close this issue.

3 participants