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

inline loop in a function called at comptime is checked against wrong eval branch quota #1949

Closed
Hejsil opened this Issue Feb 12, 2019 · 5 comments

Comments

Projects
None yet
2 participants
@Hejsil
Copy link
Member

Hejsil commented Feb 12, 2019

comptime {
    @setEvalBranchQuota(1001);
    a();
}

fn a() void {
    inline for ([]void{{}} ** 1004) |_| {}
}

Notice the double 'evaluation exceeded X backwards branches' errors have different values

test.zig:7:12: error: evaluation exceeded 1001 backwards branches
    inline for ([]void{{}} ** 1004) |_| {}
           ^
test.zig:3:6: note: called from here
    a();
     ^
test.zig:1:10: note: called from here
comptime {
         ^
test.zig:7:12: error: evaluation exceeded 1000 backwards branches
    inline for ([]void{{}} ** 1004) |_| {}
@Hejsil

This comment has been minimized.

Copy link
Member Author

Hejsil commented Feb 12, 2019

Alright, I think i know what's going on.

The first error is thrown when a is evaluated in the comptime block. Here, the eval quota is 1001 because that was what it was set to in the comptime block.
The second error is thrown when a is being analyzed. Here, the eval quota is 1000.

This seems like a design flaw. There is no way to extend the eval quota in a, as that would cause a compiler error.

comptime {
    @setEvalBranchQuota(1020);
    a();
}

fn a() void {
    @setEvalBranchQuota(1020);
    inline for ([]void{{}} ** 1004) |_| {}
}
test.zig:7:5: error: @setEvalBranchQuota must be called from the top of the comptime stack
    @setEvalBranchQuota(1020);
    ^
test.zig:3:6: note: called from here
    a();
     ^
test.zig:1:10: note: called from here
comptime {

@andrewrk andrewrk added this to the 0.4.0 milestone Feb 12, 2019

@andrewrk andrewrk added bug and removed bug labels Feb 12, 2019

@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Feb 12, 2019

I see what's happening here. I agree this is a design flaw. a is getting analyzed as a runtime function and as a comptime function call. The current design wants to have the branch quota for the comptime call to a happen in the comptime block, which is good, but then when we analyze a, that is the top of the comptime stack. So there's the design flaw. (I think I just restated exactly what you said)

@andrewrk andrewrk added the breaking label Feb 12, 2019

@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Feb 12, 2019

This is related to another issue, which I'm not sure is filed on GitHub or not, which is that Zig really should not analyze a function and produce runtime code, if it is only called at comptime. It's actually pretty tricky to implement, and I believe might involve a comptime garbage collector to do correctly. However this would not solve this design flaw because you can still have a function called from both contexts and have this issue.

@Hejsil

This comment has been minimized.

Copy link
Member Author

Hejsil commented Feb 14, 2019

This is a blocking issue for me. Do we have any idea as to how to solve this? If not, then I'll just have a local branch that extends the default eval quota as a workaround.

@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Feb 14, 2019

I'll prioritize a fix, thanks for letting me know 👍

@andrewrk andrewrk closed this in c58b802 Feb 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.