@@ -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
0 commit comments