@@ -234,13 +234,17 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
234234 s. source_file_cache =
235235 ( source_map. files ( ) [ source_file_index] . clone ( ) , source_file_index) ;
236236 }
237+ let ( ref source_file, source_file_index) = s. source_file_cache ;
237238
238- if !s . source_file_cache . 0 . contains ( span. hi ) {
239+ if !source_file . contains ( span. hi ) {
239240 // Unfortunately, macro expansion still sometimes generates Spans
240241 // that malformed in this way.
241242 return TAG_PARTIAL_SPAN . encode ( s) ;
242243 }
243244
245+ // Length is independent of the span provenance.
246+ let len = span. hi - span. lo ;
247+
244248 // There are two possible cases here:
245249 // 1. This span comes from a 'foreign' crate - e.g. some crate upstream of the
246250 // crate we are writing metadata for. When the metadata for *this* crate gets
@@ -257,47 +261,44 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
257261 // if we're a proc-macro crate.
258262 // This allows us to avoid loading the dependencies of proc-macro crates: all of
259263 // the information we need to decode `Span`s is stored in the proc-macro crate.
260- let ( tag, lo, hi, metadata_index) =
261- if s. source_file_cache . 0 . is_imported ( ) && !s. is_proc_macro {
262- // To simplify deserialization, we 'rebase' this span onto the crate it originally came from
263- // (the crate that 'owns' the file it references. These rebased 'lo' and 'hi' values
264- // are relative to the source map information for the 'foreign' crate whose CrateNum
265- // we write into the metadata. This allows `imported_source_files` to binary
266- // search through the 'foreign' crate's source map information, using the
267- // deserialized 'lo' and 'hi' values directly.
268- //
269- // All of this logic ensures that the final result of deserialization is a 'normal'
270- // Span that can be used without any additional trouble.
271- let ( external_start_pos, metadata_index) = {
272- // Introduce a new scope so that we drop the 'lock()' temporary
273- match & * s. source_file_cache . 0 . external_src . lock ( ) {
274- ExternalSource :: Foreign { original_start_pos, metadata_index, .. } => {
275- ( * original_start_pos, * metadata_index)
276- }
277- src => panic ! ( "Unexpected external source {:?}" , src) ,
264+ let ( tag, lo, metadata_index) = if source_file. is_imported ( ) && !s. is_proc_macro {
265+ // To simplify deserialization, we 'rebase' this span onto the crate it originally came from
266+ // (the crate that 'owns' the file it references. These rebased 'lo' and 'hi' values
267+ // are relative to the source map information for the 'foreign' crate whose CrateNum
268+ // we write into the metadata. This allows `imported_source_files` to binary
269+ // search through the 'foreign' crate's source map information, using the
270+ // deserialized 'lo' and 'hi' values directly.
271+ //
272+ // All of this logic ensures that the final result of deserialization is a 'normal'
273+ // Span that can be used without any additional trouble.
274+ let ( external_start_pos, metadata_index) = {
275+ // Introduce a new scope so that we drop the 'lock()' temporary
276+ match & * source_file. external_src . lock ( ) {
277+ ExternalSource :: Foreign { original_start_pos, metadata_index, .. } => {
278+ ( * original_start_pos, * metadata_index)
278279 }
279- } ;
280- let lo = ( span. lo - s. source_file_cache . 0 . start_pos ) + external_start_pos;
281- let hi = ( span. hi - s. source_file_cache . 0 . start_pos ) + external_start_pos;
282-
283- ( TAG_VALID_SPAN_FOREIGN , lo, hi, metadata_index)
284- } else {
285- // Record the fact that we need to encode the data for this `SourceFile`
286- let source_files =
287- s. required_source_files . as_mut ( ) . expect ( "Already encoded SourceMap!" ) ;
288- let ( source_file_index, _) = source_files. insert_full ( s. source_file_cache . 1 ) ;
289- let source_file_index: u32 =
290- source_file_index. try_into ( ) . expect ( "cannot export more than U32_MAX files" ) ;
291-
292- ( TAG_VALID_SPAN_LOCAL , span. lo , span. hi , source_file_index)
280+ src => panic ! ( "Unexpected external source {:?}" , src) ,
281+ }
293282 } ;
283+ let lo = ( span. lo - source_file. start_pos ) + external_start_pos;
284+
285+ ( TAG_VALID_SPAN_FOREIGN , lo, metadata_index)
286+ } else {
287+ // Record the fact that we need to encode the data for this `SourceFile`
288+ let source_files =
289+ s. required_source_files . as_mut ( ) . expect ( "Already encoded SourceMap!" ) ;
290+ let ( metadata_index, _) = source_files. insert_full ( source_file_index) ;
291+ let metadata_index: u32 =
292+ metadata_index. try_into ( ) . expect ( "cannot export more than U32_MAX files" ) ;
293+
294+ ( TAG_VALID_SPAN_LOCAL , span. lo , metadata_index)
295+ } ;
294296
295297 tag. encode ( s) ;
296298 lo. encode ( s) ;
297299
298300 // Encode length which is usually less than span.hi and profits more
299301 // from the variable-length integer encoding that we use.
300- let len = hi - lo;
301302 len. encode ( s) ;
302303
303304 // Encode the index of the `SourceFile` for the span, in order to make decoding faster.
0 commit comments