Home / Docs / Diagnostics

U3029: A failure that must be handled where it is not

Release v0.4.0

A failure is not handled inside code that runs where no caller can receive it.

Why Una rejects it

Some code runs where no caller can receive a failure, so each failure it meets is handled inside it, with if error use or attempt (spec/26, ADR 0116):

The error is at the statement, or at the fails clause.

The repair at a glance

src/demo.unadiff
  module demo

  pub kind Lost = | Gone

  close(name: Text) -> Unit
    fails Lost
  =
    if name == "" then fail Gone else unit

  pub run() -> Unit
-   fails Lost
    diverges
  =
    for ever
      stop
    on stop
      close("log")
+       if error use unit
    unit

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 kind Lost = | Gone

close(name: Text) -> Unit
  fails Lost
=
  if name == "" then fail Gone else unit

pub run() -> Unit
  fails Lost
  diverges
=
  for ever
    stop
  on stop
    close("log")
  unit

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 kind Lost = | Gone

close(name: Text) -> Unit
  fails Lost
=
  if name == "" then fail Gone else unit

pub run() -> Unit
  diverges
=
  for ever
    stop
  on stop
    close("log")
      if error use unit
  unit