rust-lang/rust · critical

Already encoded SourceMap!

Error message

Already encoded SourceMap!

What it means

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.

Solutions

  1. `cargo clean` and rebuild (the bad ordering may be a one-off from incremental state).
  2. Disable incremental compilation (`CARGO_INCREMENTAL=0`) to rule out incremental-driven span reuse.
  3. Try a different rustc version (the ordering bug may be fixed upstream).
  4. Reproduces on a clean, incremental-off build -> file an ICE against the encoder with the span-producing item.
Defensive patterns

Strategy: retry

Validate before calling

# Rule out incremental-driven span reuse first
export CARGO_INCREMENTAL=0
cargo clean && cargo build

Try / catch

CARGO_INCREMENTAL=0 cargo build || { cargo clean && CARGO_INCREMENTAL=0 cargo build; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/c9eb0780d1d8b8c1. Report an issue: GitHub.

Appendix: source

Thrown at compiler/rustc_metadata/src/rmeta/encoder.rs:327

            // CrateNum we write into the metadata. This allows `imported_source_files` to binary
            // search through the 'foreign' crate's source map information, using the
            // deserialized 'lo' and 'hi' values directly.
            //
            // All of this logic ensures that the final result of deserialization is a 'normal'
            // Span that can be used without any additional trouble.
            let metadata_index = {
                // Introduce a new scope so that we drop the 'read()' temporary
                match &*source_file.external_src.read() {
                    ExternalSource::Foreign { metadata_index, .. } => *metadata_index,
                    src => panic!("Unexpected external source {src:?}"),
                }
            };

            (SpanKind::Foreign, metadata_index)
        } else {
            // Record the fact that we need to encode the data for this `SourceFile`
            let source_files =
                s.required_source_files.as_mut().expect("Already encoded SourceMap!");
            let (metadata_index, _) = source_files.insert_full(source_file_index);
            let metadata_index: u32 =
                metadata_index.try_into().expect("cannot export more than U32_MAX files");

            (SpanKind::Local, metadata_index)
        };

        // Encode the start position relative to the file start, so we profit more from the
        // variable-length integer encoding.
        let lo = self.lo - source_file.start_pos;

        // Encode length which is usually less than span.hi and profits more
        // from the variable-length integer encoding that we use.
        let len = self.hi - self.lo;

        let tag = SpanTag::new(kind, ctxt, len.0 as usize);
        tag.encode(s);
        if tag.context().is_none() {

View on GitHub (pinned to 7088e4b63a)