Skip to content

Commit 87e6b33

Browse files
authored
Rollup merge of #148962 - weihanglo:checksum, r=jieyouxu
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` Fixes #148934
2 parents a528a58 + cf57b9b commit 87e6b33

File tree

8 files changed

+62
-27
lines changed

8 files changed

+62
-27
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.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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,8 @@ impl<'a> CrateMetadataRef<'a> {
17441744
src_hash,
17451745
checksum_hash,
17461746
start_pos: original_start_pos,
1747-
source_len,
1747+
normalized_source_len,
1748+
unnormalized_source_len,
17481749
lines,
17491750
multibyte_chars,
17501751
normalized_pos,
@@ -1804,7 +1805,8 @@ impl<'a> CrateMetadataRef<'a> {
18041805
src_hash,
18051806
checksum_hash,
18061807
stable_id,
1807-
source_len.to_u32(),
1808+
normalized_source_len.to_u32(),
1809+
unnormalized_source_len,
18081810
self.cnum,
18091811
lines,
18101812
multibyte_chars,
@@ -1817,9 +1819,9 @@ impl<'a> CrateMetadataRef<'a> {
18171819
translated (start_pos {:?} source_len {:?})",
18181820
local_version.name,
18191821
original_start_pos,
1820-
source_len,
1822+
normalized_source_len,
18211823
local_version.start_pos,
1822-
local_version.source_len
1824+
local_version.normalized_source_len
18231825
);
18241826

18251827
ImportedSourceFile {

compiler/rustc_query_system/src/ich/impls_syntax.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
5454
checksum_hash: _,
5555
external_src: _,
5656
start_pos: _,
57-
source_len: _,
57+
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: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,8 +1723,10 @@ pub struct SourceFile {
17231723
pub external_src: FreezeLock<ExternalSource>,
17241724
/// The start position of this source in the `SourceMap`.
17251725
pub start_pos: BytePos,
1726-
/// The byte length of this source.
1727-
pub source_len: RelativeBytePos,
1726+
/// The byte length of this source after normalization.
1727+
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.
@@ -1748,7 +1750,8 @@ impl Clone for SourceFile {
17481750
checksum_hash: self.checksum_hash,
17491751
external_src: self.external_src.clone(),
17501752
start_pos: self.start_pos,
1751-
source_len: self.source_len,
1753+
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(),
@@ -1764,7 +1767,8 @@ impl<S: SpanEncoder> Encodable<S> for SourceFile {
17641767
self.src_hash.encode(s);
17651768
self.checksum_hash.encode(s);
17661769
// Do not encode `start_pos` as it's global state for this session.
1767-
self.source_len.encode(s);
1770+
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());
@@ -1837,7 +1841,8 @@ impl<D: SpanDecoder> Decodable<D> for SourceFile {
18371841
let name: FileName = Decodable::decode(d);
18381842
let src_hash: SourceFileHash = Decodable::decode(d);
18391843
let checksum_hash: Option<SourceFileHash> = Decodable::decode(d);
1840-
let source_len: RelativeBytePos = Decodable::decode(d);
1844+
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 {
@@ -1859,7 +1864,8 @@ impl<D: SpanDecoder> Decodable<D> for SourceFile {
18591864
SourceFile {
18601865
name,
18611866
start_pos: BytePos::from_u32(0),
1862-
source_len,
1867+
normalized_source_len,
1868+
unnormalized_source_len,
18631869
src: None,
18641870
src_hash,
18651871
checksum_hash,
@@ -1959,12 +1965,17 @@ 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);
1965-
let source_len = src.len();
1966-
let source_len = u32::try_from(source_len).map_err(|_| OffsetOverflowError)?;
1967-
if source_len > Self::MAX_FILE_SIZE {
1977+
let normalized_source_len = u32::try_from(src.len()).map_err(|_| OffsetOverflowError)?;
1978+
if normalized_source_len > Self::MAX_FILE_SIZE {
19681979
return Err(OffsetOverflowError);
19691980
}
19701981

@@ -1977,7 +1988,8 @@ impl SourceFile {
19771988
checksum_hash,
19781989
external_src: FreezeLock::frozen(ExternalSource::Unneeded),
19791990
start_pos: BytePos::from_u32(0),
1980-
source_len: RelativeBytePos::from_u32(source_len),
1991+
normalized_source_len: RelativeBytePos::from_u32(normalized_source_len),
1992+
unnormalized_source_len,
19811993
lines: FreezeLock::frozen(SourceFileLines::Lines(lines)),
19821994
multibyte_chars,
19831995
normalized_pos,
@@ -2161,7 +2173,7 @@ impl SourceFile {
21612173

21622174
#[inline]
21632175
pub fn end_position(&self) -> BytePos {
2164-
self.absolute_position(self.source_len)
2176+
self.absolute_position(self.normalized_source_len)
21652177
}
21662178

21672179
/// Finds the line containing the given position. The return value is the
@@ -2197,7 +2209,7 @@ impl SourceFile {
21972209

21982210
#[inline]
21992211
pub fn is_empty(&self) -> bool {
2200-
self.source_len.to_u32() == 0
2212+
self.normalized_source_len.to_u32() == 0
22012213
}
22022214

22032215
/// Calculates the original byte position relative to the start of the file

compiler/rustc_span/src/source_map.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl SourceMap {
262262
bytes,
263263
Span::new(
264264
file.start_pos,
265-
BytePos(file.start_pos.0 + file.source_len.0),
265+
BytePos(file.start_pos.0 + file.normalized_source_len.0),
266266
SyntaxContext::root(),
267267
None,
268268
),
@@ -353,14 +353,15 @@ impl SourceMap {
353353
src_hash: SourceFileHash,
354354
checksum_hash: Option<SourceFileHash>,
355355
stable_id: StableSourceFileId,
356-
source_len: u32,
356+
normalized_source_len: u32,
357+
unnormalized_source_len: u32,
357358
cnum: CrateNum,
358359
file_local_lines: FreezeLock<SourceFileLines>,
359360
multibyte_chars: Vec<MultiByteChar>,
360361
normalized_pos: Vec<NormalizedPos>,
361362
metadata_index: u32,
362363
) -> Arc<SourceFile> {
363-
let source_len = RelativeBytePos::from_u32(source_len);
364+
let normalized_source_len = RelativeBytePos::from_u32(normalized_source_len);
364365

365366
let source_file = SourceFile {
366367
name: filename,
@@ -372,7 +373,8 @@ impl SourceMap {
372373
metadata_index,
373374
}),
374375
start_pos: BytePos(0),
375-
source_len,
376+
normalized_source_len,
377+
unnormalized_source_len,
376378
lines: file_local_lines,
377379
multibyte_chars,
378380
normalized_pos,
@@ -566,7 +568,7 @@ impl SourceMap {
566568

567569
let start_index = local_begin.pos.to_usize();
568570
let end_index = local_end.pos.to_usize();
569-
let source_len = local_begin.sf.source_len.to_usize();
571+
let source_len = local_begin.sf.normalized_source_len.to_usize();
570572

571573
if start_index > end_index || end_index > source_len {
572574
return Err(SpanSnippetError::MalformedForSourcemap(MalformedSourceMapPositions {
@@ -997,7 +999,7 @@ impl SourceMap {
997999
return 1;
9981000
}
9991001

1000-
let source_len = local_begin.sf.source_len.to_usize();
1002+
let source_len = local_begin.sf.normalized_source_len.to_usize();
10011003
debug!("source_len=`{:?}`", source_len);
10021004
// Ensure indexes are also not malformed.
10031005
if start_index > end_index || end_index > source_len - 1 {

compiler/rustc_span/src/source_map/tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ fn t10() {
230230
name,
231231
src_hash,
232232
checksum_hash,
233-
source_len,
233+
normalized_source_len,
234+
unnormalized_source_len,
234235
lines,
235236
multibyte_chars,
236237
normalized_pos,
@@ -243,7 +244,8 @@ fn t10() {
243244
src_hash,
244245
checksum_hash,
245246
stable_id,
246-
source_len.to_u32(),
247+
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+
}

src/tools/clippy/clippy_config/src/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ struct ConfError {
108108

109109
impl ConfError {
110110
fn from_toml(file: &SourceFile, error: &toml::de::Error) -> Self {
111-
let span = error.span().unwrap_or(0..file.source_len.0 as usize);
111+
let span = error.span().unwrap_or(0..file.normalized_source_len.0 as usize);
112112
Self::spanned(file, error.message(), None, span)
113113
}
114114

0 commit comments

Comments
 (0)