Skip to content

Commit b55a79a

Browse files
committed
fix(span): track unnormalized source len for dep-info
Add `unnormalized_source_len` field to track the byte length of source files before normalization (the original length). `unnormalized_source_len` is for writing the correct file length to dep-info for `-Zchecksum-hash-algorithm`
1 parent e9f9f47 commit b55a79a

File tree

7 files changed

+37
-1
lines changed

7 files changed

+37
-1
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,9 @@ 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.normalized_source_len.0 as u64,
599+
// This needs to be unnormalized,
600+
// as external tools wouldn't know how rustc normalizes them
601+
fmap.unnormalized_source_len as u64,
600602
fmap.checksum_hash,
601603
)
602604
})

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
normalized_source_len,
1748+
unnormalized_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
normalized_source_len.to_u32(),
1809+
unnormalized_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
normalized_source_len: _,
58+
unnormalized_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 after normalization.
17271727
pub normalized_source_len: RelativeBytePos,
1728+
/// The byte length of this source before normalization.
1729+
pub unnormalized_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
normalized_source_len: self.normalized_source_len,
1754+
unnormalized_source_len: self.unnormalized_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.normalized_source_len.encode(s);
1771+
self.unnormalized_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 normalized_source_len: RelativeBytePos = Decodable::decode(d);
1845+
let unnormalized_source_len = 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
normalized_source_len,
1868+
unnormalized_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 unnormalized_source_len = u32::try_from(src.len()).map_err(|_| OffsetOverflowError)?;
1970+
if unnormalized_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);
@@ -1977,6 +1989,7 @@ impl SourceFile {
19771989
external_src: FreezeLock::frozen(ExternalSource::Unneeded),
19781990
start_pos: BytePos::from_u32(0),
19791991
normalized_source_len: RelativeBytePos::from_u32(normalized_source_len),
1992+
unnormalized_source_len,
19801993
lines: FreezeLock::frozen(SourceFileLines::Lines(lines)),
19811994
multibyte_chars,
19821995
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
normalized_source_len: u32,
357+
unnormalized_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
normalized_source_len,
377+
unnormalized_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
normalized_source_len,
234+
unnormalized_source_len,
234235
lines,
235236
multibyte_chars,
236237
normalized_pos,
@@ -244,6 +245,7 @@ fn t10() {
244245
checksum_hash,
245246
stable_id,
246247
normalized_source_len.to_u32(),
248+
unnormalized_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_unnormalized_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.unnormalized_source_len, 19);
118+
assert_eq!(sf.normalized_source_len.0, 14);
119+
}

0 commit comments

Comments
 (0)