Lyricdev
GitHub

LYR-SEM0111

a bare name in a pattern that spells a variant of the scrutinee's enum

error · spec 09-patterns

A pattern binds a bare name, and the scrutinee's enum has a variant of that name — the pattern would match every value, not the variant.

Explanation

A bare name in a pattern is always a binding: it matches any value and names it. Red => … over a Signal therefore takes every signal, under the name Red, and every arm after it is dead. Since that is never what is meant when the enum has a variant Red, the spelling is refused.

Write the variant as .Red or Signal.Red; to bind every value, pick a name that is no variant of the enum.

Wrong

enum Signal { Red, Yellow, Green }

fn code(s: Signal): int {
    return match (s) {
        Red => 0,
        _ => 1,
    };
}

fn main(): void {
    let _ = code(Signal.Green);
}
enum Signal { Red, Yellow, Green }

fn code(s: Signal): int {
    return match (s) {
        .Red => 0,
        _ => 1,
    };
}

fn main(): void {
    let _ = code(Signal.Green);
}