{"id":"8e2578d915323632","repo":"rust-lang/rust","slug":"decode-error-e","errorCode":null,"errorMessage":"decode error: {e}","messagePattern":"decode error: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs","lineNumber":56,"sourceCode":"                panic!(\"DefPathHashMap::OwnedFromMetadata variant only exists for deserialization\")\n            }\n        }\n    }\n}\n\nimpl<'a> Decodable<BlobDecodeContext<'a>> for DefPathHashMapRef<'static> {\n    fn decode(d: &mut BlobDecodeContext<'a>) -> DefPathHashMapRef<'static> {\n        let len = d.read_usize();\n        let pos = d.position();\n        let o = d.blob().bytes().clone().slice(|blob| &blob[pos..pos + len]);\n\n        // Although we already have the data we need via the `OwnedSlice`, we still need\n        // to advance the `DecodeContext`'s position so it's in a valid state after\n        // the method. We use `read_raw_bytes()` for that.\n        let _ = d.read_raw_bytes(len);\n\n        let inner = odht::HashTable::from_raw_bytes(o).unwrap_or_else(|e| {\n            panic!(\"decode error: {e}\");\n        });\n        DefPathHashMapRef::OwnedFromMetadata(inner)\n    }\n}\n","sourceCodeStart":38,"sourceCodeEnd":61,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs#L38-L61","documentation":"When decoding rmeta, `DefPathHashMapRef::decode` reads a length-prefixed byte slice and reconstructs an `odht::HashTable` via `from_raw_bytes`. If reconstruction fails, it panics with \"decode error: {e}\". This means the def-path-hash map bytes in the `.rmeta`/incremental metadata are structurally invalid or were produced by an incompatible encoder version.","triggerScenarios":"Loading a crate's metadata (compiling against a dependency, or resuming incremental compilation) where `odht::HashTable::from_raw_bytes` returns `Err` — e.g. wrong magic/version header, truncated or zero-length blob, or a layout mismatch from an odht version change.","commonSituations":"Switching nightly compiler versions (or odht crate versions) without clearing `target/` incremental caches. Corrupt or partially-written `.rmeta` from a killed/interrupted build, disk-full, or NFS quirks. Mixing metadata produced by a cross-compiled toolchain with a different endianness/word-size assumption.","solutions":["Run `cargo clean` (or remove `target/` and any `incremental` dir) to discard stale/corrupt metadata and rebuild.","Rebuild all dependencies from source with the current toolchain so `.rmeta` files match the decoder.","If it persists, check for disk/NFS corruption and verify the odht/rustc_abi versions are consistent across the dependency graph; report as a rustc ICE with the backtrace if metadata is freshly produced."],"exampleFix":"// no source fix; this is a metadata-corruption / version-mismatch panic.\n// recover with:\n//   cargo clean\n//   cargo build","handlingStrategy":"try-catch","validationCode":"// This is a runtime decode error from reading an `.rmeta` file's\n// DefPathHashMap. You cannot fully prevent it (the file may be corrupt or\n// written by a mismatched compiler), but you CAN pre-validate before decoding:\nuse std::fs;\nuse std::path::Path;\n\nfn preflight_rmeta(path: &Path) -> Result<(), String> {\n    let meta = fs::metadata(path)\n        .map_err(|e| format!(\"rmeta missing: {e}\"))?;\n    if meta.len() < 8 {\n        return Err(\"rmeta truncated (< 8 bytes)\".into());\n    }\n    // Optionally check the file header magic / rustc version tag your crate\n    // embeds, so you never hand an incompatible blob to the decoder.\n    Ok(())\n}\n\n// Call preflight_rmeta() before invoking the decoder.","typeGuard":"// Narrow on a successful decode result rather than catching the panic.\nenum DecodeOutcome<T> {\n    Ok(T),\n    Corrupt(String),\n}\n\nfn safe_decode<T: Decode>(bytes: &[u8]) -> DecodeOutcome<T> {\n    match Decoder::read(bytes) {\n        Ok(v)  => DecodeOutcome::Ok(v),\n        Err(e) => DecodeOutcome::Corrupt(format!(\"decode error: {e}\")),\n    }\n}","tryCatchPattern":"// The decoder returns Result; never unwrap it on untrusted .rmeta.\nmatch DefPathHashMap::decode(&mut decoder, &tcx) {\n    Ok(map) => use_map(map),\n    Err(e) => {\n        // Treat the crate as unreadable: drop it from the dependency graph,\n        // log the file path + compiler version that produced it, and surface\n        // a user-actionable message ('rebuild dependency X').\n        tracing::error!(\n            rmeta = %path.display(),\n            error = %e,\n            \"def_path_hash_map decode failed; crate is corrupt or version-mismatched\"\n        );\n        mark_crate_unusable(&path);\n    }\n }","preventionTips":["Treat every `.rmeta` from outside your build as untrusted — validate length and header before decoding.","Never `.unwrap()` a decode Result; map the error to 'crate unusable' and continue the build without it.","Clean (`cargo clean`) and rebuild a dependency whose rmeta triggers this error — stale artifacts from a different toolchain are the usual cause.","Pin your toolchain per workspace so rmeta producers and consumers agree."],"tags":["rustc-metadata","rmeta","decoding","corruption","odht"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}