---
title: "U3027: Code that may run forever where it is not allowed to"
url: https://una-lang.org/en/docs/errors/U3027/
release: v0.4.0
source: https://github.com/una-lang/una/blob/v0.4.0/crates/una-explain/explain/U3027.md
---

# 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 `diverges` can call later;
- in a `together` task, since the tasks of a block run one after another in the interpreter;
- in an `on stop` block, which runs once its loop has ended and must end itself;
- in the body of a `for ever` loop, whose every iteration must end (rule 9);
- in a test, except in its `do` line when the test ends with a `stop after N` line, which stops every `for ever` once `N` of 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

`src/demo.una`

```diff
  module demo

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

## Failing example

`ita.una`

```una
ita demo
  version 0.1.0
  edition 0.4
```

`ita.lock`

```una
lock 1

solver
  encoding "una-smt-1"
  name "z3"
  version "5.1.0"
  logic "QF_UFLIA"
  steps 5000000
  seed 0
```

`src/demo.una`

```una
module demo

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

## Fixed example

`ita.una`

```una
ita demo
  version 0.1.0
  edition 0.4
```

`ita.lock`

```una
lock 1

solver
  encoding "una-smt-1"
  name "z3"
  version "5.1.0"
  logic "QF_UFLIA"
  steps 5000000
  seed 0
```

`src/demo.una`

```una
module demo

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