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

Assertion failure when declaring struct S { m: fn(self: S) } #795

Closed
raulgrell opened this Issue Feb 27, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@raulgrell
Contributor

raulgrell commented Feb 27, 2018

This code:

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

const S = struct {
    a_member: u8,
    a_member_method: fn(self: S) u8,
};

fn aStructFunction(self: S) u8 { return self.a_member; }

test "namespacing" {
    const s = S {
        .a_member = 1,
        .a_member_method = aStructFunction  
    };
    assert(s.a_member_method(s) == 1);
}

Produces:

$ ~/zig/build/zig test test.zig 
zig: /home/raulgrell/zig/src/analyze.cpp:1645: void resolve_struct_type(CodeGen*, TypeTableEntry*): Assertion `!struct_type->data.structure.zero_bits_loop_flag' failed.

Fixed by changing original code to:

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

const S = struct {
    a_member: u8,
    a_member_method: fn(self: &const S) u8,
};

fn aStructFunction(self: &const S) u8 { return self.a_member; }

test "namespacing" {
    const s = S {
        .a_member = 1,
        .a_member_method = aStructFunction  
    };

    assert(s.a_member_method(s) == 1);
}

Which is arguably more correct, but the error needs improvement.

@andrewrk andrewrk added the bug label Feb 27, 2018

@andrewrk andrewrk added this to the 0.2.0 milestone Feb 27, 2018

bnoordhuis added a commit to bnoordhuis/zig that referenced this issue Feb 28, 2018

fix assert on self-referencing function ptr field
The construct `struct S { f: fn(S) void }` is not legal because structs
are not copyable but it should not result in an ICE.

Fixes ziglang#795.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment