Home / Docs / Diagnostics

U3027: Code that may run forever where it is not allowed to

Release v0.4.0

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:

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

src/demo.unadiff
  module demo

  pub count() -> Int
+   diverges
  =
    let edit n = 0
    for ever
      stop when n > 9
      n = n + 1
    n

Failing example

ita.unauna
ita demo
  version 0.1.0
  edition 0.4
ita.lockuna
lock 1

solver
  encoding "una-smt-1"
  name "z3"
  version "5.1.0"
  logic "QF_UFLIA"
  steps 5000000
  seed 0
src/demo.unauna
module demo

pub count() -> Int
=
  let edit n = 0
  for ever
    stop when n > 9
    n = n + 1
  n

Fixed example

ita.unauna
ita demo
  version 0.1.0
  edition 0.4
ita.lockuna
lock 1

solver
  encoding "una-smt-1"
  name "z3"
  version "5.1.0"
  logic "QF_UFLIA"
  steps 5000000
  seed 0
src/demo.unauna
module demo

pub count() -> Int
  diverges
=
  let edit n = 0
  for ever
    stop when n > 9
    n = n + 1
  n