Simplify encoding a bit.

This commit is contained in:
Camille GILLOT 2022-08-06 23:13:09 +02:00
parent d74af405eb
commit 7c2d722fb0

View File

@ -234,13 +234,17 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
s.source_file_cache = s.source_file_cache =
(source_map.files()[source_file_index].clone(), source_file_index); (source_map.files()[source_file_index].clone(), source_file_index);
} }
let (ref source_file, source_file_index) = s.source_file_cache;
if !s.source_file_cache.0.contains(span.hi) { if !source_file.contains(span.hi) {
// Unfortunately, macro expansion still sometimes generates Spans // Unfortunately, macro expansion still sometimes generates Spans
// that malformed in this way. // that malformed in this way.
return TAG_PARTIAL_SPAN.encode(s); return TAG_PARTIAL_SPAN.encode(s);
} }
// Length is independent of the span provenance.
let len = span.hi - span.lo;
// There are two possible cases here: // There are two possible cases here:
// 1. This span comes from a 'foreign' crate - e.g. some crate upstream of the // 1. This span comes from a 'foreign' crate - e.g. some crate upstream of the
// crate we are writing metadata for. When the metadata for *this* crate gets // crate we are writing metadata for. When the metadata for *this* crate gets
@ -257,8 +261,7 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
// if we're a proc-macro crate. // if we're a proc-macro crate.
// This allows us to avoid loading the dependencies of proc-macro crates: all of // This allows us to avoid loading the dependencies of proc-macro crates: all of
// the information we need to decode `Span`s is stored in the proc-macro crate. // the information we need to decode `Span`s is stored in the proc-macro crate.
let (tag, lo, hi, metadata_index) = let (tag, lo, metadata_index) = if source_file.is_imported() && !s.is_proc_macro {
if s.source_file_cache.0.is_imported() && !s.is_proc_macro {
// To simplify deserialization, we 'rebase' this span onto the crate it originally came from // To simplify deserialization, we 'rebase' this span onto the crate it originally came from
// (the crate that 'owns' the file it references. These rebased 'lo' and 'hi' values // (the crate that 'owns' the file it references. These rebased 'lo' and 'hi' values
// are relative to the source map information for the 'foreign' crate whose CrateNum // are relative to the source map information for the 'foreign' crate whose CrateNum
@ -270,26 +273,25 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
// Span that can be used without any additional trouble. // Span that can be used without any additional trouble.
let (external_start_pos, metadata_index) = { let (external_start_pos, metadata_index) = {
// Introduce a new scope so that we drop the 'lock()' temporary // Introduce a new scope so that we drop the 'lock()' temporary
match &*s.source_file_cache.0.external_src.lock() { match &*source_file.external_src.lock() {
ExternalSource::Foreign { original_start_pos, metadata_index, .. } => { ExternalSource::Foreign { original_start_pos, metadata_index, .. } => {
(*original_start_pos, *metadata_index) (*original_start_pos, *metadata_index)
} }
src => panic!("Unexpected external source {:?}", src), src => panic!("Unexpected external source {:?}", src),
} }
}; };
let lo = (span.lo - s.source_file_cache.0.start_pos) + external_start_pos; let lo = (span.lo - source_file.start_pos) + external_start_pos;
let hi = (span.hi - s.source_file_cache.0.start_pos) + external_start_pos;
(TAG_VALID_SPAN_FOREIGN, lo, hi, metadata_index) (TAG_VALID_SPAN_FOREIGN, lo, metadata_index)
} else { } else {
// Record the fact that we need to encode the data for this `SourceFile` // Record the fact that we need to encode the data for this `SourceFile`
let source_files = let source_files =
s.required_source_files.as_mut().expect("Already encoded SourceMap!"); s.required_source_files.as_mut().expect("Already encoded SourceMap!");
let (source_file_index, _) = source_files.insert_full(s.source_file_cache.1); let (metadata_index, _) = source_files.insert_full(source_file_index);
let source_file_index: u32 = let metadata_index: u32 =
source_file_index.try_into().expect("cannot export more than U32_MAX files"); metadata_index.try_into().expect("cannot export more than U32_MAX files");
(TAG_VALID_SPAN_LOCAL, span.lo, span.hi, source_file_index) (TAG_VALID_SPAN_LOCAL, span.lo, metadata_index)
}; };
tag.encode(s); tag.encode(s);
@ -297,7 +299,6 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
// Encode length which is usually less than span.hi and profits more // Encode length which is usually less than span.hi and profits more
// from the variable-length integer encoding that we use. // from the variable-length integer encoding that we use.
let len = hi - lo;
len.encode(s); len.encode(s);
// Encode the index of the `SourceFile` for the span, in order to make decoding faster. // Encode the index of the `SourceFile` for the span, in order to make decoding faster.