Skip to content

comptime: loop iterations of earlier let values count for a later assert, which then gets a false E8005 and E8002 #4627

Backlog

Description

@hiraditya

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 in consteval.loop_steps. A let (in check_let_decl_stmt), an assignment and a for range evaluate with the count that earlier code left. Only an assert and a comptime block in src/hir/check/control.rs set it to 0.
  • steps_exceeded stays set after a let stops. The next report_steps_exceeded call 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 the while loop 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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions