Skip to content
TE136 · Type · error

Cannot infer the enum for a `.Variant` shorthand

The `.Variant` shorthand recovers its enum from the type the surrounding position expects, and this position supplies nothing usable — no expected type at all (an un-annotated `let`, an argument to a callee with no known signature), an un-pinned generic type parameter, or a type that is not an enum. Annotate the binding or write the qualified `Enum::Variant` form.

Why this fires

The inferred enum shorthand .Variant deliberately omits the enum name — the compiler recovers it from the type the surrounding position expects. TE136 says that position expects nothing usable: it supplies no type at all, or a type that isn't an enum.

enum Target { Native, Llvm, Wasm }

fn f() {
    let t = .Llvm
    //      ^^^^^ error[TE136]: cannot infer the enum for `.Llvm` here —
    //            this position has no expected type; write the qualified
    //            form (`Enum::Llvm`) or annotate the binding
}

The shorthand resolves wherever an expected type reaches it:

  • an annotated let/const (let t: Target = .Llvm), including through ? (let t: Target? = .Llvm)
  • an argument whose parameter type is known (b.target(.Llvm), configure(.Wasm))
  • a return in a function with a declared return type (fn d() -> Target { return .Llvm })
  • a struct/record literal field, an array/map literal element with a declared element type, and a lambda body checked against an expected return type
  • a match scrutinee, for the pattern form (match t { .Native => … })

It does not resolve when:

  • the position pins nothing at all — an un-annotated let, a bare expression statement, or an argument to a callee whose signature the compiler cannot see;
  • the expected type is a generic type parameter that this call did not pin (fn id<T>(x: T) -> T called as id(.Llvm)T is still T when the argument is checked);
  • the expected type resolves to something that is not an enum (a struct, a primitive, a type alias to a non-enum).

There is no "guess the enum from the variant name" fallback: two enums may share a variant name, and silently picking one would compile to the wrong constructor.

Fix it

1. Annotate the binding

enum Target { Native, Llvm, Wasm }

let t: Target = .Llvm        // ok — the annotation supplies the enum

2. Write the qualified form

Always available, and the right answer wherever no expected type exists (or where you want the enum spelled out for a reader):

let t = Target::Llvm         // ok

3. Declare the function's parameter or return type

enum Target { Native, Llvm, Wasm }

fn build(t: Target) {}
fn default_target() -> Target { return .Llvm }   // ok — declared return type

build(.Wasm)                                     // ok — declared parameter type

4. Pin the generic

A shorthand cannot pin a type parameter by itself — the enum has to arrive from somewhere else:

enum Target { Native, Llvm }

fn id<T>(x: T) -> T { x }

let t: Target = id(Target::Llvm)   // ok — qualified form inside the generic call

See also

  • TE137 — the enum was inferred, but it has no such variant.
  • TE133 — the same unknown-variant failure for the qualified form and for .Variant patterns.
  • /docs/data-structures#enums — enum declarations and variant construction.

See also

Search Zolo

9 results

en