{"record":{"id":"c9eb0780d1d8b8c1","repo":"rust-lang/rust","slug":"already-encoded-sourcemap","errorCode":null,"errorMessage":"Already encoded SourceMap!","messagePattern":"Already encoded SourceMap!","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_metadata/src/rmeta/encoder.rs","lineNumber":327,"sourceCode":"            // CrateNum we write into the metadata. This allows `imported_source_files` to binary\n            // search through the 'foreign' crate's source map information, using the\n            // deserialized 'lo' and 'hi' values directly.\n            //\n            // All of this logic ensures that the final result of deserialization is a 'normal'\n            // Span that can be used without any additional trouble.\n            let metadata_index = {\n                // Introduce a new scope so that we drop the 'read()' temporary\n                match &*source_file.external_src.read() {\n                    ExternalSource::Foreign { metadata_index, .. } => *metadata_index,\n                    src => panic!(\"Unexpected external source {src:?}\"),\n                }\n            };\n\n            (SpanKind::Foreign, metadata_index)\n        } else {\n            // Record the fact that we need to encode the data for this `SourceFile`\n            let source_files =\n                s.required_source_files.as_mut().expect(\"Already encoded SourceMap!\");\n            let (metadata_index, _) = source_files.insert_full(source_file_index);\n            let metadata_index: u32 =\n                metadata_index.try_into().expect(\"cannot export more than U32_MAX files\");\n\n            (SpanKind::Local, metadata_index)\n        };\n\n        // Encode the start position relative to the file start, so we profit more from the\n        // variable-length integer encoding.\n        let lo = self.lo - source_file.start_pos;\n\n        // Encode length which is usually less than span.hi and profits more\n        // from the variable-length integer encoding that we use.\n        let len = self.hi - self.lo;\n\n        let tag = SpanTag::new(kind, ctxt, len.0 as usize);\n        tag.encode(s);\n        if tag.context().is_none() {","sourceCodeStart":309,"sourceCodeEnd":345,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_metadata/src/rmeta/encoder.rs#L309-L345","documentation":"Fires in the rmeta encoder (encoder.rs:327). While encoding a span, the encoder tries to record the span's source file in `s.required_source_files`; if that field is already `None` (consumed by an earlier `encode_source_map` pass), `.expect(\"Already encoded SourceMap!\")` panics. It guards against encoding spans after the source-map table has been finalized.","triggerScenarios":"A code path encodes a `Span` after `encode_source_map` has already drained `required_source_files` into the finalized table; an encoder re-entrancy or ordering bug where late-serialized items reference source files post-finalization.","commonSituations":"Internal rustc bug in serialization ordering; almost never triggered by user code, but can surface with unusual proc-macro / def-site span combinations or nightly encoder changes.","solutions":["`cargo clean` and rebuild (the bad ordering may be a one-off from incremental state).","Disable incremental compilation (`CARGO_INCREMENTAL=0`) to rule out incremental-driven span reuse.","Try a different rustc version (the ordering bug may be fixed upstream).","Reproduces on a clean, incremental-off build -> file an ICE against the encoder with the span-producing item."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"# Rule out incremental-driven span reuse first\nexport CARGO_INCREMENTAL=0\ncargo clean && cargo build","typeGuard":null,"tryCatchPattern":"CARGO_INCREMENTAL=0 cargo build || { cargo clean && CARGO_INCREMENTAL=0 cargo build; }","preventionTips":["Disable incremental compilation when you see encoder-ordering panics.","`cargo clean` after nightly updates (encoder internals change frequently).","Try a different nightly if the panic reproduces; encoder ordering bugs are often fixed quickly."],"tags":["metadata","rmeta","encoder","source-map","ice"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}