Skip to content

Commit 69ccd5a

Browse files
committed
rdr: fix SpanRef resolution and optimize metadata encoding
- Fix decoder to use span_file_data source map for -Zstable-crate-hash crates - Add preload_all_source_files for deterministic BytePos ordering - Enable -Zstable-crate-hash for library builds and copy .spans to sysroot - Encode fn_arg_idents with SpanRef - Add transform_span_ref_for_export to convert local stable_ids to exported stable_ids when encoding predicate queries to metadata Refactor span conversion into SpanConversionState and add lazy_array_with and lazy_array_with_mut helpers that scope borrows per-item. This avoids intermediate Vec allocations and HashMap cloning when encoding arrays that need span transformation, while avoiding borrow conflicts with encode(&mut self).
1 parent 770d5fe commit 69ccd5a

File tree

8 files changed

+288
-209
lines changed

8 files changed

+288
-209
lines changed

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_middle::ty::{
88
use rustc_middle::{bug, span_bug};
99
use rustc_span::def_id::{DefId, LocalDefId};
1010
use rustc_span::{Span, SpanRef};
11-
use rustc_span::def_id::{DefId, LocalDefId};
1211

1312
fn convert_to_span_ref<'tcx>(
1413
tcx: TyCtxt<'tcx>,

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredic
4343
let inferred_outlives = tcx.inferred_outlives_of(def_id);
4444
if !inferred_outlives.is_empty() {
4545
debug!("predicates_of: inferred_outlives_of({:?}) = {:?}", def_id, inferred_outlives,);
46-
let inferred_outlives_iter =
47-
inferred_outlives.iter().map(|(clause, span)| ((*clause).upcast(tcx), tcx.resolve_span_ref(*span)));
46+
let inferred_outlives_iter = inferred_outlives
47+
.iter()
48+
.map(|(clause, span)| ((*clause).upcast(tcx), tcx.resolve_span_ref(*span)));
4849
if result.predicates.is_empty() {
4950
result.predicates = tcx.arena.alloc_from_iter(inferred_outlives_iter);
5051
} else {

compiler/rustc_metadata/src/creader.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ impl CStore {
658658
.map_err(|err| CrateError::MissingSpanFile(crate_name, err))?;
659659
}
660660

661+
crate_metadata.preload_all_source_files(tcx, self);
662+
661663
self.set_crate_data(cnum, crate_metadata);
662664

663665
Ok(cnum)

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,8 +2126,11 @@ impl<'a> CrateMetadataRef<'a> {
21262126
tcx: TyCtxt<'_>,
21272127
target_stable_id: rustc_span::StableSourceFileId,
21282128
) -> Option<ImportedSourceFile> {
2129-
// Iterate through all source files in this crate's metadata
2130-
let num_files = self.root.source_map.size();
2129+
let num_files = if let Some(span_data) = self.cdata.span_file_data() {
2130+
span_data.source_map.size()
2131+
} else {
2132+
self.root.source_map.size()
2133+
};
21312134
debug!(
21322135
?target_stable_id,
21332136
crate_name = ?self.cdata.root.header.name,
@@ -2266,6 +2269,25 @@ impl CrateMetadata {
22662269
Ok(())
22672270
}
22682271

2272+
/// Pre-imports all source files to ensure deterministic `BytePos` values for
2273+
/// diagnostic output ordering.
2274+
pub(crate) fn preload_all_source_files(&self, tcx: TyCtxt<'_>, cstore: &CStore) {
2275+
let cref = CrateMetadataRef { cdata: self, cstore };
2276+
let num_files = if let Some(span_data) = self.span_file_data() {
2277+
span_data.source_map.size()
2278+
} else {
2279+
self.root.source_map.size()
2280+
};
2281+
debug!(
2282+
crate_name = ?self.root.header.name,
2283+
num_files,
2284+
"preload_all_source_files: importing all source files"
2285+
);
2286+
for file_index in 0..num_files {
2287+
let _ = cref.imported_source_file(tcx, file_index as u32);
2288+
}
2289+
}
2290+
22692291
/// Lazily loads the span file data if a span file path is configured.
22702292
/// Returns None if no span file exists or if loading fails.
22712293
fn span_file_data(&self) -> Option<&SpanFileData> {

0 commit comments

Comments
 (0)