Repository navigation
Description
What happens
While it compiles, vxc computes the value of a let when it can. It counts the loop iterations of these computations and stops after 1,000,000 (E8005). The count goes on from one let to the next. Only an assert or a comptime block starts a new count.
So after let values that run 1,000,000 iterations in all, a later assert that is true gets two errors:
- E8005, although the assert itself runs one loop of 5 iterations.
- E8002 "Comptime assert failed". The evaluator stops the loop early and uses the partial value as the result.
A normal compile checks all the functions of a module with one checker, and a parallel compile (-j) makes a new checker for each function. So when the let values are in other functions, only a normal compile gives the errors.
Program 1: one function (both compiles fail)
fn spin(n : i32) -> i32 {
let mut i = 0;
while i < n {
i = i + 1;
}
return i;
}
fn main() -> i32 {
assert(spin(0) == 0);
let a = spin(600000);
let b = spin(600000);
let x = spin(5);
assert(x == 5);
return a + b + x;
}
vxc one_function.vx --action emit-mlir and vxc -j 2 one_function.vx --action emit-mlir both give:
Error[E8005] at one_function.vx:14:3: compile-time evaluation ran more than 1000000 loop iterations and was stopped. A loop whose end condition is never reached is the usual cause.
Error[E8002] at one_function.vx:14:3: Comptime assert failed: Comptime assertion failed
Program 2: three functions (only a normal compile fails)
fn spin(n : i32) -> i32 {
let mut i = 0;
while i < n {
i = i + 1;
}
return i;
}
fn first() -> i32 {
let a = spin(600000);
return a;
}
fn second() -> i32 {
let b = spin(600000);
return b;
}
fn third() -> i32 {
let x = spin(1);
assert(x == 1);
return x;
}
fn main() -> i32 {
assert(spin(0) == 0);
return first() + second() + third();
}
A normal compile gives E8005 and E8002 at assert(x == 1) in third. vxc -j 2 compiles the program with no error.
The assert in main calls spin, so vxc can run spin while compiling.
Expected
Both programs compile with no error, in a normal compile and with -j. A let that runs too many iterations gets no value. It must not change the value or the errors of later code.
Where
src/hir/stmt.rs: the checker's evaluator counts iterations inconsteval.loop_steps. Alet(incheck_let_decl_stmt), an assignment and aforrange evaluate with the count that earlier code left. Only an assert and acomptimeblock insrc/hir/check/control.rsset it to 0.steps_exceededstays set after aletstops. The nextreport_steps_exceededcall reports it, at an assert or a loop that did not run too many iterations.- A loop that stops at the limit ends as if it finished (
eval_for_loop, and thewhileloop after it). The code after the loop runs with the partial values, and the function returns a wrong value, not "no value". That is where the false E8002 comes from.
The PR for #4602 gives each evaluation that the checker starts its own call count, for the same reason. The loop count needs the same change.
Found with a debug vxc built on main at ea0d9e8. The build had the commits of #4624, which do not change this code.
Activity