Skip to content

Commit 00dcb1d

Browse files
committed
fix(span): track original source len for dep-info
Add `original_source_len` field to track the byte length of source files before normalization. For imported/decoded source files where the original content is unavailable, the normalized length is used as a fallback. `original_source_len` is for writing the correct file length to dep-info for `-Zchecksum-hash-algorithm`
1 parent b6d7ff3 commit 00dcb1d

File tree

7 files changed

+35
-1
lines changed

7 files changed

+35
-1
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
596596
.map(|fmap| {
597597
(
598598
escape_dep_filename(&fmap.name.prefer_local().to_string()),
599-
fmap.source_len.0 as u64,
599+
fmap.original_source_len as u64,
600600
fmap.checksum_hash,
601601
)
602602
})

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,7 @@ impl<'a> CrateMetadataRef<'a> {
17451745
checksum_hash,
17461746
start_pos: original_start_pos,
17471747
source_len,
1748+
original_source_len,
17481749
lines,
17491750
multibyte_chars,
17501751
normalized_pos,
@@ -1805,6 +1806,7 @@ impl<'a> CrateMetadataRef<'a> {
18051806
checksum_hash,
18061807
stable_id,
18071808
source_len.to_u32(),
1809+
original_source_len,
18081810
self.cnum,
18091811
lines,
18101812
multibyte_chars,

compiler/rustc_query_system/src/ich/impls_syntax.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
5555
external_src: _,
5656
start_pos: _,
5757
source_len: _,
58+
original_source_len: _,
5859
lines: _,
5960
ref multibyte_chars,
6061
ref normalized_pos,

compiler/rustc_span/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,8 @@ pub struct SourceFile {
17251725
pub start_pos: BytePos,
17261726
/// The byte length of this source.
17271727
pub source_len: RelativeBytePos,
1728+
/// The byte length of this source before normalization.
1729+
pub original_source_len: u32,
17281730
/// Locations of lines beginnings in the source code.
17291731
pub lines: FreezeLock<SourceFileLines>,
17301732
/// Locations of multi-byte characters in the source code.
@@ -1749,6 +1751,7 @@ impl Clone for SourceFile {
17491751
external_src: self.external_src.clone(),
17501752
start_pos: self.start_pos,
17511753
source_len: self.source_len,
1754+
original_source_len: self.original_source_len,
17521755
lines: self.lines.clone(),
17531756
multibyte_chars: self.multibyte_chars.clone(),
17541757
normalized_pos: self.normalized_pos.clone(),
@@ -1765,6 +1768,7 @@ impl<S: SpanEncoder> Encodable<S> for SourceFile {
17651768
self.checksum_hash.encode(s);
17661769
// Do not encode `start_pos` as it's global state for this session.
17671770
self.source_len.encode(s);
1771+
self.original_source_len.encode(s);
17681772

17691773
// We are always in `Lines` form by the time we reach here.
17701774
assert!(self.lines.read().is_lines());
@@ -1838,6 +1842,7 @@ impl<D: SpanDecoder> Decodable<D> for SourceFile {
18381842
let src_hash: SourceFileHash = Decodable::decode(d);
18391843
let checksum_hash: Option<SourceFileHash> = Decodable::decode(d);
18401844
let source_len: RelativeBytePos = Decodable::decode(d);
1845+
let original_source_len: u32 = Decodable::decode(d);
18411846
let lines = {
18421847
let num_lines: u32 = Decodable::decode(d);
18431848
if num_lines > 0 {
@@ -1860,6 +1865,7 @@ impl<D: SpanDecoder> Decodable<D> for SourceFile {
18601865
name,
18611866
start_pos: BytePos::from_u32(0),
18621867
source_len,
1868+
original_source_len,
18631869
src: None,
18641870
src_hash,
18651871
checksum_hash,
@@ -1959,6 +1965,12 @@ impl SourceFile {
19591965
SourceFileHash::new_in_memory(checksum_hash_kind, src.as_bytes())
19601966
}
19611967
});
1968+
// Capture the original source length before normalization.
1969+
let original_source_len = u32::try_from(src.len()).map_err(|_| OffsetOverflowError)?;
1970+
if original_source_len > Self::MAX_FILE_SIZE {
1971+
return Err(OffsetOverflowError);
1972+
}
1973+
19621974
let normalized_pos = normalize_src(&mut src);
19631975

19641976
let stable_id = StableSourceFileId::from_filename_in_current_crate(&name);
@@ -1978,6 +1990,7 @@ impl SourceFile {
19781990
external_src: FreezeLock::frozen(ExternalSource::Unneeded),
19791991
start_pos: BytePos::from_u32(0),
19801992
source_len: RelativeBytePos::from_u32(source_len),
1993+
original_source_len,
19811994
lines: FreezeLock::frozen(SourceFileLines::Lines(lines)),
19821995
multibyte_chars,
19831996
normalized_pos,

compiler/rustc_span/src/source_map.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ impl SourceMap {
354354
checksum_hash: Option<SourceFileHash>,
355355
stable_id: StableSourceFileId,
356356
source_len: u32,
357+
original_source_len: u32,
357358
cnum: CrateNum,
358359
file_local_lines: FreezeLock<SourceFileLines>,
359360
multibyte_chars: Vec<MultiByteChar>,
@@ -373,6 +374,7 @@ impl SourceMap {
373374
}),
374375
start_pos: BytePos(0),
375376
source_len,
377+
original_source_len,
376378
lines: file_local_lines,
377379
multibyte_chars,
378380
normalized_pos,

compiler/rustc_span/src/source_map/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ fn t10() {
231231
src_hash,
232232
checksum_hash,
233233
source_len,
234+
original_source_len,
234235
lines,
235236
multibyte_chars,
236237
normalized_pos,
@@ -244,6 +245,7 @@ fn t10() {
244245
checksum_hash,
245246
stable_id,
246247
source_len.to_u32(),
248+
original_source_len,
247249
CrateNum::ZERO,
248250
FreezeLock::new(lines.read().clone()),
249251
multibyte_chars,

compiler/rustc_span/src/tests.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,17 @@ fn test_trim() {
103103

104104
assert_eq!(span(well_before, before).trim_start(other), None);
105105
}
106+
107+
#[test]
108+
fn test_original_source_length() {
109+
let source = "\u{feff}hello\r\nferries\r\n".to_owned();
110+
let sf = SourceFile::new(
111+
FileName::Anon(Hash64::ZERO),
112+
source,
113+
SourceFileHashAlgorithm::Sha256,
114+
Some(SourceFileHashAlgorithm::Sha256),
115+
)
116+
.unwrap();
117+
assert_eq!(sf.original_source_len, 19);
118+
assert_eq!(sf.source_len.0, 14);
119+
}

0 commit comments

Comments
 (0)