Skip to content
TE137 · Type · error

Unknown variant in a `.Variant` shorthand

The `.Variant` shorthand resolved its enum from the position's expected type, but that enum has no variant by that name. A "did you mean?" suggestion is included when a close match exists. The pattern form of the same mistake reports `TE133` instead.

Why this fires

The inferred enum shorthand .Variant resolved its enum from the surrounding position's expected type — and that enum has no variant by that name. This is almost always a typo (a "did you mean?" suggestion is attached when a close match exists).

enum Target { Native, Llvm, Wasm }

fn f() {
    let t: Target = .Nativ
    //              ^^^^^^ error[TE137]: `.Nativ` is not a variant of enum
    //                     'Target'. Did you mean 'Native'?
}

TE137 only fires once the enum itself is known. When the position supplies no usable expected type, the compiler cannot even name an enum to check against and reports TE136 instead — so these two codes are mutually exclusive at any one site.

The pattern form of the shorthand reports the same failure under TE133, not TE137:

enum Target { Native, Llvm }

fn n(t: Target) -> int {
    match t {
        .Nope => 1,
        //^^^^ error[TE133]: enum 'Target' has no variant 'Nope'
        _ => 0,
    }
}

That is deliberate: a pattern that names an unknown variant is the same user-facing mistake whether it is written .Nope or Target::Nope, and TE133 already owns it.

Fix it

1. Fix the spelling

Trust the "did you mean?" suggestion when it appears — it is computed against the enum's real declared variant names.

enum Target { Native, Llvm, Wasm }
let t: Target = .Native   // fixed

2. Check that the expected type is the enum you meant

If the suggestion looks unrelated, the annotation (or the parameter, or the declared return type) may name a different enum than you had in mind. Spelling the qualified form out makes the mismatch visible immediately:

enum Target { Native, Llvm }
enum Mode { Debug, Release }

let m: Mode = Mode::Debug    // qualified — the compiler checks the name against `Mode`

3. Declare the variant if it is genuinely new

enum Target { Native, Llvm, Wasm, Interp }   // add it to the declaration
let t: Target = .Interp

See also

  • TE136 — the enum could not be inferred for this position at all.
  • TE133 — the same unknown-variant check for EnumName::variant and for .Variant patterns.
  • TE123 — the equivalent "did you mean?" check for unknown struct fields.
  • /docs/data-structures#enums — enum declarations, unit/tuple/struct variants.

See also

Search Zolo

9 results

en