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

Functions with comptime arguments, can't use @setEvalBranchQuota in comptime blocks #688

Closed
Hejsil opened this Issue Jan 13, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@Hejsil
Member

Hejsil commented Jan 13, 2018

fn generateTable(comptime T: type) -> [2000]T {
    var res : [2000]T = undefined;
    var i : usize = 0;
    while (i < 2000) : (i += 1) {
        res[i] = T(i);
    }

    return res;
}

fn doesAlotT(comptime T: type, value: usize) -> T {
    const table = comptime blk: {
        // @setEvalBranchQuota must be called from the top of the comptime stack
        @setEvalBranchQuota(5000);
        break :blk generateTable(T);
    };
    return table[value];
}

fn doesAlot(value: usize) -> u32 {
    const table = comptime blk: {
        @setEvalBranchQuota(5000); // This works
        break :blk generateTable(u32);
    };
    return table[value];
}


test "do things" {
    _ = doesAlotT(u32, 2);
    _ = doesAlot(2);
}
@andrewrk

This comment has been minimized.

Member

andrewrk commented Jan 14, 2018

This is by design. Let me have a look at your use case and see why it doesn't make sense to put the @setEvalBranchQuota at the top of the comptime stack.

@andrewrk

This comment has been minimized.

Member

andrewrk commented Jan 14, 2018

Hmm I'm not seeing it. Why not put @setEvalBranchQuota in the comptime block?

@andrewrk andrewrk modified the milestones: 0.2.0, 0.3.0 Jan 14, 2018

@Hejsil

This comment has been minimized.

Member

Hejsil commented Jan 14, 2018

Oooh ye. I probably just want to write a test instead of the comptime block in zig-crc. But we do agree that the code provided in this issue should work, yes?

@andrewrk

This comment has been minimized.

Member

andrewrk commented Jan 15, 2018

Ah, I see the problem. Even if I move the setEvalBranchQuota to the test, I get

    while (i < 2000) : (i += 1) {
    ^
/home/andy/downloads/zig/build/test.zig:14:33: note: called from here
        break :blk generateTable(T);
                                ^
/home/andy/downloads/zig/build/test.zig:30:18: note: called from here
    _ = doesAlotT(u32, 2);
                 ^

so there is actually nowhere you can put it to make it work.

@andrewrk andrewrk added the bug label Jan 15, 2018

@andrewrk andrewrk modified the milestones: 0.3.0, 0.2.0 Jan 15, 2018

@andrewrk andrewrk closed this in 49c3922 Mar 12, 2018

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