Skip to content
TE118 · Type · error

Discriminant must be an integer constant

A variant's `= expr` discriminant either didn't evaluate to an integer at compile time, or couldn't be evaluated as a compile-time constant at all.

Why this fires

A variant's explicit discriminant expression (= expr) either didn't evaluate to an integer at compile time, or couldn't be evaluated as a compile-time constant at all.

enum E: u8 { X = "zero" }
//               ^^^^^^ error[TE118]: discriminant for `E::X` must be an integer constant
fn compute() -> int { return 1 }

enum F: u8 { Y = compute() }
//               ^^^^^^^^^ error[TE118]: discriminant for `F::Y` is not a compile-time constant

Fix it

Use a literal integer, or an expression the comptime evaluator can fold (arithmetic on literals, references to consts):

enum E: u8 { X = 0 }             // ok

const BASE = 10
enum F: u8 { Y = BASE + 1 }      // ok — comptime-foldable

See also

  • TE114 — numeric discriminants require a non-generic, all-unit enum.
  • TE115 — discriminant value doesn't fit the backing type.
  • /docs/data-structures#enums — numeric enum discriminants.

See also

Search Zolo

9 results

en