Home / Docs / Diagnostics
U3027: Code that may run forever where it is not allowed to
A for ever, or a call of a function that says diverges, is where it is not legal, or a function the host calls in a loop of its own says diverges.
Why Una rejects it
In edition 0.4, a function that may run forever says so in its header with diverges (spec/26 §5, ADR 0116 rule 8). for ever, and a call of a function that says diverges, are legal only in the body of such a function, and not:
- in a lambda, which is an ordinary value that code without
divergescan call later; - in a
togethertask, since the tasks of a block run one after another in the interpreter; - in an
on stopblock, which runs once its loop has ended and must end itself; - in the body of a
for everloop, whose every iteration must end (rule 9); - in a test, except in its
doline when the test ends with astop after Nline, which stops everyfor everonceNof their iterations have started (spec/26 §8, rule 19).
diverges is never inferred, so every header that may run forever says it. The error is at the for ever or the call.
The functions the host calls in a loop of its own may not say diverges themselves: init, step and on_stop of a driven module (ADR 0116 rule 8), and handle and on_stop of the module of a serve section, so that every request and the stop end after finitely many steps (spec/31 §7, ADR 0123 rule 16). The error is then at the diverges clause.
The repair at a glance
module demo
pub count() -> Int
+ diverges
=
let edit n = 0
for ever
stop when n > 9
n = n + 1
nFailing example
ita demo
version 0.1.0
edition 0.4lock 1
solver
encoding "una-smt-1"
name "z3"
version "5.1.0"
logic "QF_UFLIA"
steps 5000000
seed 0module demo
pub count() -> Int
=
let edit n = 0
for ever
stop when n > 9
n = n + 1
nFixed example
ita demo
version 0.1.0
edition 0.4lock 1
solver
encoding "una-smt-1"
name "z3"
version "5.1.0"
logic "QF_UFLIA"
steps 5000000
seed 0module demo
pub count() -> Int
diverges
=
let edit n = 0
for ever
stop when n > 9
n = n + 1
n