{"record":{"id":"75f2e0bf13f977b5","repo":"rust-lang/rust","slug":"cannot-export-more-than-u32-max-files","errorCode":null,"errorMessage":"cannot export more than U32_MAX files","messagePattern":"cannot export more than U32_MAX files","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_metadata/src/rmeta/encoder.rs","lineNumber":330,"sourceCode":"            //\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() {\n            ctxt.encode(s);\n        }\n        lo.encode(s);","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_metadata/src/rmeta/encoder.rs#L312-L348","documentation":"Fires in the rmeta encoder (encoder.rs:330). After inserting a source file into `required_source_files`, the resulting index is converted to `u32` for serialization; `.expect(\"cannot export more than U32_MAX files\")` panics if the index exceeds `u32::MAX`. It is a hard capacity guard: the on-disk metadata format addresses source files with a 32-bit index.","triggerScenarios":"A compilation that registers more than 2^32 distinct `SourceFile`s in `required_source_files`. In practice unreachable for human-scale code; could conceivably arise from a pathological proc-macro / include! explosion that fabricates billions of synthetic source files.","commonSituations":"Essentially never in real projects; a guard rail. If hit, it indicates a runaway file-generation process (macro generating one source file per iteration of a huge range).","solutions":["Identify and cap the macro/procedural generation that creates a unique `SourceFile` per iteration (e.g. `include_str!` in a loop, `concat!`/`file!`-expanding macros over a huge range).","Reduce the number of distinct files fed to the crate (concatenate generated sources).","If legitimate, this is a format limitation; file an issue to widen the index or stream source files.","Audit `build.rs` / codegen steps that emit one file per entity."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"# Count distinct source files being fed to the crate; this guard only\n# fires near 2^32 files, so a hit means runaway generation.\nfind . -name '*.rs' -type f | wc -l   # sanity check the input is human-scale\n# If a macro generates files, cap its iteration range.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cap macros/codegen that emit one `SourceFile` per loop iteration.","Consolidate generated code into fewer files instead of one per entity.","Audit `build.rs` codegen for unbounded file creation.","Treat a hit here as a signal of runaway generation, not a normal build."],"tags":["metadata","rmeta","encoder","source-map","capacity"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}