{"id":"d750855014d403a4","repo":"rust-lang/rust","slug":"variant-with-id-unknown-variant","errorCode":null,"errorMessage":"variant_with_id: unknown variant","messagePattern":"variant_with_id: unknown variant","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_middle/src/ty/adt.rs","lineNumber":598,"sourceCode":"        // This would disallow the following kind of enum from being casted into integer.\n        // ```\n        // enum Enum {\n        //    Foo() = 1,\n        //    Bar{} = 2,\n        //    Baz = 3,\n        // }\n        // ```\n        if self.variants().iter().any(|v| {\n            matches!(v.discr, VariantDiscr::Explicit(_)) && v.ctor_kind() != Some(CtorKind::Const)\n        }) {\n            return false;\n        }\n        self.variants().iter().all(|v| v.fields.is_empty())\n    }\n\n    /// Return a `VariantDef` given a variant id.\n    pub fn variant_with_id(self, vid: DefId) -> &'tcx VariantDef {\n        self.variants().iter().find(|v| v.def_id == vid).expect(\"variant_with_id: unknown variant\")\n    }\n\n    /// Return a `VariantDef` given a constructor id.\n    pub fn variant_with_ctor_id(self, cid: DefId) -> &'tcx VariantDef {\n        self.variants()\n            .iter()\n            .find(|v| v.ctor_def_id() == Some(cid))\n            .expect(\"variant_with_ctor_id: unknown variant\")\n    }\n\n    /// Return the index of `VariantDef` given a variant id.\n    #[inline]\n    pub fn variant_index_with_id(self, vid: DefId) -> VariantIdx {\n        self.variants()\n            .iter_enumerated()\n            .find(|(_, v)| v.def_id == vid)\n            .expect(\"variant_index_with_id: unknown variant\")\n            .0","sourceCodeStart":580,"sourceCodeEnd":616,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_middle/src/ty/adt.rs#L580-L616","documentation":"This `expect` in adt.rs:598 (`variant_with_id`) fires when `AdtDef::variants()` does not contain a `VariantDef` whose `def_id` equals the queried `vid`. The ADT's variant list is built once during HIR lowering/TyCtxt construction; a lookup miss indicates a stale or mismatched DefId was handed to the ADT — an internal consistency bug, normally not reachable from valid user code. Sibling lookups (`variant_with_ctor_id`, `variant_index_with_id`) share the same invariant.","triggerScenarios":"Called when the compiler resolves `Res::Def(DefKind::Variant, vid)` via `variant_of_res` (adt.rs:628) or directly via `tcx.adt_def(..).variant_with_id(did)`, but `did` is not a variant of that ADT — e.g. a DefId from a different enum passed in due to an incremental-compilation cache desync or a HIR query ordering bug.","commonSituations":"Incremental compilation (`-C incremental`) corruption after a compiler upgrade; proc-macro or macro_rules that synthesizes enum variant references; rename/refactor of an enum variant leaving a stale DefId in a cached query; enum variants generated by a derive macro whose input changed.","solutions":["Run `cargo clean` and rebuild to rule out incremental-cache desync.","Pin or upgrade the toolchain to a known-good nightly (the ICE is usually fixed quickly once reported).","Reduce the failing crate and file an ICE report at https://github.com/rust-lang/rust with the backtrace and `RUST_BACKTRACE=full` output.","If a proc-macro is involved, check that it emits fresh spans/DefIds rather than caching them across compilations."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// variant_with_id failures almost always mean stale/corrupt rmeta for an enum variant.\n// Validate the build artifacts are fresh before trusting any ADT variant index.\nfn build_artifacts_fresh(meta_stamp: std::time::SystemTime, src_stamp: std::time::SystemTime) -> bool {\n    meta_stamp > src_stamp // metadata newer than source -> safe to trust variant ids\n}","typeGuard":"// Confirm a variant still exists in the *current* metadata before indexing by id.\nfn variant_exists<E: EnumProbe>(id: u32) -> bool {\n    E::variant_count() > id\n}","tryCatchPattern":null,"preventionTips":["On first sighting, run `cargo clean && cargo build` — unknown-variant ICEs are dominated by stale incremental/rmeta caches, not real bugs.","Avoid hand-editing generated enum code without bumping the crate version; downstream variant ids go stale silently.","Disable `-Zincremental` in CI; incremental caches are the usual source of variant-id drift.","If it reproduces after a clean build, isolate the offending enum and report with `--pretty=expanded`."],"tags":["rustc-ice","adt","hir","incremental","compiler-internal"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}