E1045: case does not cover every value

View source

A case expression must handle every possible value of its subject (ADR-0013).

A case expression must handle every possible value of its subject (ADR-0013). For an enum, each variant needs a branch, or a _ branch must catch the rest. For a Bool subject, unguarded true and false branches cover it and no _ is needed; with one of them missing, the message names the missing value (missing: false). For an integer or String subject, a _ branch is required. A branch with a guard does not count, because the guard may fail. The message lists the missing variants. For an integer subject it names the subject type and shows _. The error points at the case.

Example

actus
enum Color {
    Red,
    Green,
    Blue,
}

verb name(abs c: Color) -> Int {
    return case c {
        Color.Red => 1,
        Color.Green => 2,
    };
}

verb main() {
}

A Bool needs both of its values:

actus
verb name(abs flag: Bool) -> Int {
    return case flag {
        true => 1,
    };
}

verb main() {
}

Fix

Add a branch for each missing variant, or a _ branch.

actus
enum Color {
    Red,
    Green,
    Blue,
}

verb name(abs c: Color) -> Int {
    return case c {
        Color.Red => 1,
        Color.Green => 2,
        Color.Blue => 3,
    };
}

verb main() {
}

A Bool is covered by true and false, without a _.

actus
verb name(abs flag: Bool) -> Int {
    return case flag {
        true => 1,
        false => 0,
    };
}

verb main() {
}

Run actus explain E1045 in your terminal to read this entry offline. Browse Ownership & types or all error codes.