{"id":"3179b9c62eab6e97","repo":"rust-lang/rust","slug":"unexpected-code","errorCode":null,"errorMessage":"Unexpected {} code: {:?}","messagePattern":"Unexpected (.+?) code: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_metadata/src/rmeta/table.rs","lineNumber":89,"sourceCode":"    fn write_to_bytes(self, b: &mut [u8; 8]) {\n        *b = self.to_le_bytes();\n    }\n}\n\nmacro_rules! fixed_size_enum {\n    ($ty:ty { $(($($pat:tt)*))* } $( unreachable { $(($($upat:tt)*))+ } )?) => {\n        impl FixedSizeEncoding for Option<$ty> {\n            type ByteArray = [u8;1];\n\n            #[inline]\n            fn from_bytes(b: &[u8;1]) -> Self {\n                use $ty::*;\n                if b[0] == 0 {\n                    return None;\n                }\n                match b[0] - 1 {\n                    $(${index()} => Some($($pat)*),)*\n                    _ => panic!(\"Unexpected {} code: {:?}\", stringify!($ty), b[0]),\n                }\n            }\n\n            #[inline]\n            fn write_to_bytes(self, b: &mut [u8;1]) {\n                use $ty::*;\n                b[0] = match self {\n                    None => unreachable!(),\n                    $(Some($($pat)*) => 1 + ${index()},)*\n                    $(Some($($($upat)*)|+) => unreachable!(),)?\n                }\n            }\n        }\n    }\n}\n\nmacro_rules! defaulted_enum {\n    ($ty:ty { $(($($pat:tt)*))* } $( unreachable { $(($($upat:tt)*))+ } )?) => {","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_metadata/src/rmeta/table.rs#L71-L107","documentation":"Generated by the `fixed_size_enum!` macro in table.rs:89 inside `Option<T>::from_bytes`. rmeta stores fixed-size enums (e.g. Option<DefKind>, Option<CoroutineKind>, Option<MacroKind>) as single bytes where 0 = None and n = Some(variant n-1). A decoded byte that doesn't map to any known variant triggers this panic, proving the bytes are inconsistent with the decoder's enum definition.","triggerScenarios":"Decoding an .rmeta/.rlib whose encoded enum discriminant has no match in the current compiler's variant list — produced by a rustc that added/removed/reordered enum variants, by a corrupted metadata blob, or by an internal encoder bug that wrote an out-of-range discriminant.","commonSituations":"Mixing artifacts compiled with different rustc versions (notably across nightlies that extend DefKind/CoroutineKind/MacroKind), pointing Cargo at a toolchain different from the one that built a dependency, or a stale incremental cache carrying incompatible metadata.","solutions":["Run `cargo clean` and rebuild the whole dependency tree with one consistent toolchain (`rustup show`, pin via `rust-toolchain.toml`).","Delete stale incremental artifacts (`rm -rf target/debug/incremental` or the relevant profile dir) and rebuild.","If it reproduces on a clean build with a single toolchain, capture a minimal repro and file a rustc issue — likely an encoder/decoder table out of sync."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Decode against an expected/known code range before handing the\n// table to the consumer that would otherwise `unwrap`/`bug!`.\nfn validate_table_code(kind: TableKind, code: u32) -> Result<(), String> {\n    let max = kind.known_max_code();\n    if code as usize <= max {\n        Ok(())\n    } else {\n        Err(format!(\"unexpected {} code: {:?} (max known={})\", kind, code, max))\n    }\n}","typeGuard":"fn is_known_table_code(kind: TableKind, code: u32) -> bool {\n    kind.known_codes().contains(&code)\n}","tryCatchPattern":"// Rust panics are not exceptions; isolate untrusted metadata decoding\n// behind catch_unwind at a process/worker boundary.\nlet result = std::panic::catch_unwind(|| decode_rmeta_table(&bytes));\nmatch result {\n    Ok(table) => use_table(table),\n    Err(payload) => report_corrupt_metadata(payload),\n}","preventionTips":["Do not consume .rmeta/.rlib files from a different/unstable rustc version; rmeta table layout is unstable across commits.","Pin toolchain versions in CI (rust-toolchain.toml) so generated metadata and consumer are always the same build.","Validate checksums / hashes of cached metadata before reuse; partial writes commonly surface as unexpected codes.","If you write a custom rmeta consumer, snapshot-test against real compiler output and fail loudly on schema drift."],"tags":["rustc","metadata","decoder","version-mismatch"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}