Summary

Allow

許可する、可能にする
the use of loop, while and while let during constant
定数
evaluation.
評価
for loops are technically allowed,
許可する、可能にする
too, but can't be used in practice because each iteration
反復、繰り返し
calls
呼び出し
iterator.next(), which is not a const fn and thus
それゆえに、従って、
can't be called
呼び出し
within constants.
定数
Future RFCs (like https://github.com/rust-lang/rfcs/pull/2237) might lift that restriction.
制限

Motivation

Any iteration

反復、繰り返し
is expressible with recursion. Since we already allow
許可する、可能にする
recursion via const fn and termination
終端
of said recursion via if or match, all code enabled by const recursion is already legal
(文法的に)適格
now. Some algorithms are better expressed as imperative loops and a lot of Rust code uses loops instead of recursion. Allowing
許可する、可能にする
loops in constants
定数
will allow
許可する、可能にする
more functions to become const fn without requiring any changes.

Guide-level explanation

If you previously had to write functional code inside constants,

定数
you can now change it to imperative code. For example if you wrote a fibonacci like

#![allow(unused)] fn main() { const fn fib(n: u128) -> u128 { match n { 0 => 1, 1 => 1, n => fib(n - 1) + fib(n + 1) } } }

which takes

とる
exponential time to compute a fibonacci number, you could have changed it to the functional loop

#![allow(unused)] fn main() { const fn fib(n: u128) -> u128 { const fn helper(n: u128, a: u128, b: u128, i: u128) -> u128 { if i <= n { helper(n, b, a + b, i + 1) } else { b } } helper(n, 1, 1, 2) } }

but now you can just write it as an imperative loop, which also finishes in linear time.

#![allow(unused)] fn main() { const fn fib(n: u128) -> u128 { let mut a = 1; let mut b = 1; let mut i = 2; while i <= n { let tmp = a + b; a = b; b = tmp; i += 1; } b } }

Reference-level explanation

A loop in MIR is a cyclic

循環的
graph of BasicBlocks. Evaluating
評価する(される)
such a loop is no different from evaluating
評価する(される)
a linear sequence
連なり、並び
of BasicBlocks, except that termination
終端
is not guaranteed.
保証する
To ensure
保証する
that the compiler never hangs indefinitely, we count the number of terminators
終端子
processed and whenever we reach a fixed limit, we report a lint mentioning that we cannot guarantee
保証する
that the evaluation
評価
will terminate
終了させる
and reset the counter to zero. This lint should recur in a non-annoying amount of time (e.g. at least 30 seconds between occurrences). This means that there's an internal deterministic counter (for the terminators) and a timestamp of the last (if any) loop warning emission. Both the counter needs to reach its limit and 30 seconds have to have passed since the last warning emission in order
順序
for a new warning to be emitted.

Drawbacks

  • Infinite loops will cause
    起こす
    the compiler to never finish if the lint is not denied

Rationale and alternatives
代わりのもの、選択肢

  • Do nothing, users can keep using recursion

Unresolved questions

  • Should we add a true recursion check that hashes the interpreter state and detects if it has reached the same state again?
    • This will slow down const evaluation
      評価
      enormously and for complex
      複素数、複文の
      iterations
      反復、繰り返し
      is essentially useless because it'll take
      とる
      forever (e.g. counting from 0 to u64::max_value())