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
returnin 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
matchscrutinee, 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) -> Tcalled asid(.Llvm)—Tis stillTwhen 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 enum2. 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 // ok3. 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 type4. 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 callSee also¶
TE137— the enum was inferred, but it has no such variant.TE133— the same unknown-variant failure for the qualified form and for.Variantpatterns.- /docs/data-structures#enums — enum declarations and variant construction.