rust-lang/rust · critical

Invalid DepKind {u}

Error message

Invalid DepKind {u}

What it means

In `DepKind::from_u16` (dep_node.rs:70), the compiler reconstructs a DepKind variant from a serialized u16. DepKind is generated by `define_dep_nodes!` with a fixed `DEP_KIND_NUM_VARIANTS`; any value above `MAX` (= variants-1) is rejected. So this fires when persisted dep-graph metadata references a DepKind index the running compiler doesn't know.

Source

Thrown at compiler/rustc_middle/src/dep_graph/dep_node.rs:70

use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
use rustc_data_structures::stable_hash::{StableHasher, StableOrd};
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::DefPathHash;
use rustc_macros::{Decodable, Encodable, StableHash};
use rustc_span::Symbol;

use super::{DepNodeIndex, KeyFingerprintStyle, SerializedDepNodeIndex};
use crate::dep_graph::DepNodeKey;
use crate::mono::MonoItem;
use crate::ty::{TyCtxt, tls};

// `enum DepKind` is generated by `define_dep_nodes!` below.
impl DepKind {
    #[inline]
    pub(crate) fn from_u16(u: u16) -> Self {
        if u > Self::MAX {
            panic!("Invalid DepKind {u}");
        }
        // SAFETY: See comment on DEP_KIND_NUM_VARIANTS
        unsafe { std::mem::transmute(u) }
    }

    #[inline]
    pub(crate) const fn as_u16(&self) -> u16 {
        *self as u16
    }

    #[inline]
    pub const fn as_usize(&self) -> usize {
        *self as usize
    }

    /// This is the highest value a `DepKind` can have. It's used during encoding to
    /// pack information into the unused bits.
    pub(crate) const MAX: u16 = DEP_KIND_NUM_VARIANTS - 1;

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Remove the incremental cache (`rm -rf target/<profile>/incremental`) or `cargo clean`, then rebuild.
  2. Ensure the same rustc toolchain is used across the whole build (`rust-toolchain.toml`, CI cache keys scoped by toolchain).
  3. If it recurs on a clean build, file a rustc issue — the encoder is writing a kind index the decoder rejects.
Defensive patterns

Strategy: type-guard

Validate before calling

// DepKind is a small closed enum serialized as an integer; verify
// before constructing a DepNode from external input.
fn depkind_from_u(val: u32) -> Option<DepKind> {
    DepKind::from_u32(val)
}

Type guard

fn is_valid_dep_kind_u(val: u32) -> bool {
    val < DepKind::VARIANTS as u32
}

Prevention

When it happens

Trigger: Deserializing a previous-session dep graph whose DepKind enum had more variants than the current compiler — i.e. incremental metadata written by a newer/different rustc, or corrupted `incremental/*.rlib` dep-graph section. Also reachable via an internal bug that writes an uninitialized kind.

Common situations: Switching toolchains (nightly to older stable, or between nightlies) without clearing `target/incremental`; copying a target dir across machines/toolchains; a partial/corrupted incremental cache from a killed build.

Related errors


AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03). Data as JSON: /data/errors/96bbab6c03bb6950.json. Report an issue: GitHub.