Home / Docs / Diagnostics
U3009: Failure variant not in the declared error kind
Release v0.4.0
A failure names a variant that is not a variant of the function's error kind.
Why Una rejects it
fail V ends the function with a failure, and V must be a variant of the function's error kind (spec/06 §2). A function without fails cannot fail (spec/04 §2 rule 4).
The same holds for:
failafterif errorandif none;checked;- handler operations, which fail only with their operation's error kind (spec/05 §5);
- a function passed as an argument, which fails only as its expected function type allows (spec/10 §2).
A private function without fails has the error kind of its first failure in source order inferred, a raised variant or a passed failure (spec/04 §5 rule 3, ADR 0099), so a variant of another kind is reported where it is raised.
The repair at a glance
diff
module demo
kind StoreError = Timeout | Corrupted
kind LookupError = Missing | Unavailable
pub check(key: Int) -> Int
fails LookupError
=
- if key < 0 then fail Timeout else key
+ if key < 0 then fail Missing else keyFailing example
una
module demo
kind StoreError = Timeout | Corrupted
kind LookupError = Missing | Unavailable
pub check(key: Int) -> Int
fails LookupError
=
if key < 0 then fail Timeout else keyFixed example
una
module demo
kind StoreError = Timeout | Corrupted
kind LookupError = Missing | Unavailable
pub check(key: Int) -> Int
fails LookupError
=
if key < 0 then fail Missing else key