zig version: 0.11.0-dev.407+34fa6a1e0
Ideally this code would produce a compile error: comptime execution exceeded 1000 branches. However,
const assert = @import("std").debug.assert;
fn fibonacci(index: i32) i32 {
//if (index < 2) return index;
return fibonacci(index - 1) + fibonacci(index - 2);
}
test "fibonacci" {
comptime {
try assert(fibonacci(7) == 13);
}
}
This causes a segfault in many cases due to stack overflow. It cannot be solved by asking for more stack space for these reasons:
- Some systems or environments do not support this, or otherwise make it awkward.
- There can always be adversarial code that causes a stack overflow instead of a proper error message simply by using
@setEvalBranchQuota.
Until this is fixed I am unfortunately going to have to edit the corresponding code snippet from the language reference:
|
{#code_begin|test_err|evaluation exceeded 1000 backwards branches#} |
|
{#backend_stage1#} |
|
const assert = @import("std").debug.assert; |
|
|
|
fn fibonacci(index: i32) i32 { |
|
//if (index < 2) return index; |
|
return fibonacci(index - 1) + fibonacci(index - 2); |
|
} |
|
|
|
test "fibonacci" { |
|
comptime { |
|
try assert(fibonacci(7) == 13); |
|
} |
|
} |
|
{#code_end#} |
|
<p> |
|
The compiler noticed that evaluating this function at compile-time took a long time, |
|
and thus emitted a compile error and gave up. If the programmer wants to increase |
|
the budget for compile-time computation, they can use a built-in function called |
|
{#link|@setEvalBranchQuota#} to change the default number 1000 to something else. |
|
</p> |
zig version:
0.11.0-dev.407+34fa6a1e0Ideally this code would produce a compile error: comptime execution exceeded 1000 branches. However,
This causes a segfault in many cases due to stack overflow. It cannot be solved by asking for more stack space for these reasons:
@setEvalBranchQuota.Until this is fixed I am unfortunately going to have to edit the corresponding code snippet from the language reference:
zig/doc/langref.html.in
Lines 7032 to 7052 in a943422