rust-lang/rust · critical

variants are not encoded for an enum

Error message

variants are not encoded for an enum

What it means

Fires in `get_adt_def` (decoder.rs:1177) when decoding an enum: the decoder expects `module_children_non_reexports` to hold the enum's variants, and `.expect("variants are not encoded for an enum")` panics when that table slot is empty/default. For enums the variants are encoded as the enum's module children, so a missing entry means the metadata for that enum was written without its variant list (or is being read with a mismatched schema).

Source

Thrown at compiler/rustc_metadata/src/rmeta/decoder.rs:1177

    fn get_adt_def<'tcx>(&self, tcx: TyCtxt<'tcx>, item_id: DefIndex) -> ty::AdtDef<'tcx> {
        let kind = self.def_kind(item_id);
        let did = self.local_def_id(item_id);

        let adt_kind = match kind {
            DefKind::Enum => ty::AdtKind::Enum,
            DefKind::Struct => ty::AdtKind::Struct,
            DefKind::Union => ty::AdtKind::Union,
            _ => bug!("get_adt_def called on a non-ADT {:?}", did),
        };
        let repr = self.root.tables.repr_options.get(self, item_id).unwrap().decode((self, tcx));

        let mut variants: Vec<_> = if let ty::AdtKind::Enum = adt_kind {
            self.root
                .tables
                .module_children_non_reexports
                .get(self, item_id)
                .expect("variants are not encoded for an enum")
                .decode((self, tcx))
                .filter_map(|index| {
                    let kind = self.def_kind(index);
                    match kind {
                        DefKind::Ctor(..) => None,
                        _ => Some(self.get_variant(tcx, kind, index, did)),
                    }
                })
                .collect()
        } else {
            std::iter::once(self.get_variant(tcx, kind, item_id, did)).collect()
        };

        variants.sort_by_key(|(idx, _)| *idx);

        tcx.mk_adt_def(
            did,
            adt_kind,

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. `cargo clean` and rebuild so the enum's metadata is re-encoded by the current compiler.
  2. Confirm the dependency and the root crate use the same rustc (`rustc --version --verbose`); run `rustup update` to a consistent toolchain.
  3. If the enum comes from a prebuilt sysroot or vendored rlib, rebuild that artifact from source with the active compiler.
  4. On a clean, single-toolchain build that still panics, file an ICE: the encoder omitted variants for a valid enum.
Defensive patterns

Strategy: retry

Validate before calling

# Ensure enum metadata is regenerated by the current compiler
cargo clean
cargo build

Try / catch

cargo build || { cargo clean && cargo build; }

Prevention

When it happens

Trigger: Querying the definition of an enum whose rmeta was stripped of variant info (e.g. a crate compiled without the variant-encoding path, or a fieldless enum whose constructors were encoded differently than the reader expects); schema drift between encoding and decoding rustc versions for enum layout.

Common situations: Mixing artifacts from two rustc versions where enum-encoding changed; a dependency rebuilt mid-upgrade; nightly-only enum features (e.g. `#[repr(...)]` changes) read by a decoder that predates them.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/9e1b5e593876028b. Report an issue: GitHub.