E1046: pattern can never match

View source

Branches of a case are tried from top to bottom.

Branches of a case are tried from top to bottom. A branch that follows an unguarded _ branch is never reached, because _ already matches every value. The compiler rejects the dead branch. The message shows the pattern, and the error points at it.

A _ branch that follows unguarded branches naming every variant of an enum, or both true and false of a Bool, is just as dead: nothing is left for it to match. The error points at the _. A _ that covers at least one value no branch names is accepted, and a branch with a guard does not name its value.

Example

actus
enum Color {
    Red,
    Green,
}

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

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

verb main() {
}
actus
enum Color {
    Red,
    Green,
}

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

verb main() {
}

Fix

Put the _ branch last, or remove it when the branches before it cover every value.

actus check --format json offers this as a fix: remove the unreachable arm, with its trailing comma and the line break before it (a review fix, because it deletes code).

actus
enum Color {
    Red,
    Green,
}

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

verb main() {
}

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