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

Generated function invoking bound function crashes the compiler #4654

Closed
SpexGuy opened this issue Mar 6, 2020 · 4 comments
Closed

Generated function invoking bound function crashes the compiler #4654

SpexGuy opened this issue Mar 6, 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

@SpexGuy
Copy link
Contributor

SpexGuy commented Mar 6, 2020

Attempting to compile this code crashes the compiler:

const std = @import("std");
const warn = std.debug.warn;

const TestStruct = struct {
    name: []const u8,

    fn func1(self: TestStruct) void {
        warn("func1: {}\n", self.name);
    }

    fn func2(self: TestStruct) void {
        warn("func2: {}\n", self.name);
    }
};

const VoidFn = fn () void;

fn nastyClosure(comptime bound_fn: var) VoidFn {
    const Codegen = struct {
        fn invoke() void {
            bound_fn();
        }
    };
    return Codegen.invoke;
}

pub fn main() void {
    var inst = TestStruct{ .name = "FirstName" };
    var functions: [4]VoidFn = undefined;
    functions[0] = nastyClosure(inst.func1);
    functions[1] = nastyClosure(inst.func2);
    inst.name = "LastName";
    functions[2] = nastyClosure(inst.func1);
    functions[3] = nastyClosure(inst.func2);

    for (functions) |func| {
        func();
    }
}
Unreachable at /deps/zig/src/analyze.cpp:6903 in const_values_equal. This is a bug in the Zig compiler.

Unable to dump stack trace: debug info stripped

Compiler returned: 255
@SpexGuy
Copy link
Contributor Author

SpexGuy commented Mar 6, 2020

This smaller one generates invalid IR:

const TestStruct = struct {
    name: []const u8,

    fn func1(self: TestStruct) void {}
};

const VoidFn = fn () void;

fn nastyClosure(comptime bound_fn: var) VoidFn {
    const Codegen = struct {
        fn invoke() void {
            bound_fn();
        }
    };
    return Codegen.invoke;
}

pub fn main() void {
    var inst = TestStruct{ .name = "FirstName" };
    _ = nastyClosure(inst.func1);
}
broken LLVM module found: Instruction does not dominate all uses!

  %inst = alloca %TestStruct, align 8

  call fastcc void @TestStruct.func1(%TestStruct* %inst), !dbg !13034


This is a bug in the Zig compiler.

Unable to dump stack trace: debug info stripped

Compiler returned: 255

@SpexGuy
Copy link
Contributor Author

SpexGuy commented Mar 6, 2020

If TestStruct is zero-size, it compiles and I can invoke the function. That might also be a bug.

@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 Mar 7, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Mar 7, 2020
@SpexGuy
Copy link
Contributor Author

SpexGuy commented Apr 19, 2020

Related: #141

@Vexu
Copy link
Member

Vexu commented Dec 28, 2022

BoundFn has been removed.

@Vexu Vexu closed this as completed Dec 28, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 28, 2022
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

3 participants