Skip to content

Enums ​

An enum defines a nominal, unparameterized logic type with named variants mapped to explicit non-negative integer constants. For sum types whose variants carry typed payloads, see Unions & Option.

Syntax ​

neo
pub enum State {
  Idle = 0,
  Busy = 1,
  Done = 2,
}

Width & Bit Representation ​

  • The bit width of an enum is max(1, bit_length(max_variant_value)).
  • A 1-value enum (e.g., enum Single { Only = 0 }) has width 1, never 0.
  • Query at compile time via State.width.

Validation Rules ​

  • Enums are nominally typed: an enum value can only be assigned to or compared with (==, !=) values of the same enum type.
  • Variant accessors (State.Idle or inferred .Idle) produce Constant-timed LogicVals of type State carrying their compile-time bit pattern.
  • case expressions on an enum are checked for exhaustiveness (non_exhaustive_case), duplicate variant arms (duplicate_case_arm warning), unreachable _ when all variants are already covered (unreachable_wildcard warning), and non-trailing _ (non_trailing_wildcard warning).
  • Enum discriminants must fit in 64 bits (<= u64::MAX, else enum_value_out_of_range); empty enums (empty_enum) and duplicate values (duplicate_enum_value) are rejected.
  • .castTo(T) is supported whenever Z3 proves State.width == T.width.

Elaboration & Code Generation ​

In neo-ir, SystemVerilog, and FIRRTL, enum signals lower to packed bit vectors of width State.width, and variant references lower to sized constant bit literals.