From f20ceb1c6fd7fa29a91677be548d6f8ca6c4b172 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 6 Aug 2022 22:33:06 +0200 Subject: Encode index of SourceFile along with span. --- compiler/rustc_span/src/source_map.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'compiler/rustc_span/src/source_map.rs') diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 28381157d50..387777f7af6 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -337,6 +337,7 @@ impl SourceMap { mut file_local_normalized_pos: Vec, original_start_pos: BytePos, original_end_pos: BytePos, + metadata_index: u32, ) -> Lrc { let start_pos = self .allocate_address_space(source_len) @@ -383,6 +384,7 @@ impl SourceMap { kind: ExternalSourceKind::AbsentOk, original_start_pos, original_end_pos, + metadata_index, }), start_pos, end_pos, -- cgit 1.4.1-3-g733a5 From bacb4db48c7fe109a7d480b3b5b1cd03898db4a6 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 7 Aug 2022 12:27:38 +0200 Subject: Only encode position from start of file. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 18 ++++++++---------- compiler/rustc_metadata/src/rmeta/encoder.rs | 26 +++++++++++++------------- compiler/rustc_span/src/lib.rs | 4 ---- compiler/rustc_span/src/source_map.rs | 4 +--- 4 files changed, 22 insertions(+), 30 deletions(-) (limited to 'compiler/rustc_span/src/source_map.rs') diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index a58f6e6e1de..4b2dcc7a70b 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -581,28 +581,26 @@ impl<'a, 'tcx> Decodable> for Span { foreign_data.imported_source_file(metadata_index, sess) }; - // Make sure our binary search above is correct. + // Make sure our span is well-formed. debug_assert!( - lo >= source_file.original_start_pos && lo <= source_file.original_end_pos, - "Bad binary search: lo={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}", + lo + source_file.original_start_pos <= source_file.original_end_pos, + "Malformed encoded span: lo={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}", lo, source_file.original_start_pos, source_file.original_end_pos ); - // Make sure we correctly filtered out invalid spans during encoding + // Make sure we correctly filtered out invalid spans during encoding. debug_assert!( - hi >= source_file.original_start_pos && hi <= source_file.original_end_pos, - "Bad binary search: hi={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}", + hi + source_file.original_start_pos <= source_file.original_end_pos, + "Malformed encoded span: hi={:?} source_file.original_start_pos={:?} source_file.original_end_pos={:?}", hi, source_file.original_start_pos, source_file.original_end_pos ); - let lo = - (lo + source_file.translated_source_file.start_pos) - source_file.original_start_pos; - let hi = - (hi + source_file.translated_source_file.start_pos) - source_file.original_start_pos; + let lo = lo + source_file.translated_source_file.start_pos; + let hi = hi + source_file.translated_source_file.start_pos; // Do not try to decode parent for foreign spans. Span::new(lo, hi, ctxt, None) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 463cfece715..6b3118e7152 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -235,6 +235,7 @@ impl<'a, 'tcx> Encodable> for Span { (source_map.files()[source_file_index].clone(), source_file_index); } let (ref source_file, source_file_index) = s.source_file_cache; + debug_assert!(source_file.contains(span.lo)); if !source_file.contains(span.hi) { // Unfortunately, macro expansion still sometimes generates Spans @@ -242,9 +243,6 @@ impl<'a, 'tcx> Encodable> for Span { 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: // 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 @@ -261,7 +259,7 @@ impl<'a, 'tcx> Encodable> for Span { // if we're a proc-macro crate. // 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. - let (tag, lo, metadata_index) = if source_file.is_imported() && !s.is_proc_macro { + let (tag, metadata_index) = if source_file.is_imported() && !s.is_proc_macro { // 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 // are relative to the source map information for the 'foreign' crate whose CrateNum @@ -271,18 +269,15 @@ impl<'a, 'tcx> Encodable> for Span { // // All of this logic ensures that the final result of deserialization is a 'normal' // Span that can be used without any additional trouble. - let (external_start_pos, metadata_index) = { + let metadata_index = { // Introduce a new scope so that we drop the 'lock()' temporary match &*source_file.external_src.lock() { - ExternalSource::Foreign { original_start_pos, metadata_index, .. } => { - (*original_start_pos, *metadata_index) - } + ExternalSource::Foreign { metadata_index, .. } => *metadata_index, src => panic!("Unexpected external source {:?}", src), } }; - let lo = (span.lo - source_file.start_pos) + external_start_pos; - (TAG_VALID_SPAN_FOREIGN, lo, metadata_index) + (TAG_VALID_SPAN_FOREIGN, metadata_index) } else { // Record the fact that we need to encode the data for this `SourceFile` let source_files = @@ -291,14 +286,19 @@ impl<'a, 'tcx> Encodable> for Span { let metadata_index: u32 = metadata_index.try_into().expect("cannot export more than U32_MAX files"); - (TAG_VALID_SPAN_LOCAL, span.lo, metadata_index) + (TAG_VALID_SPAN_LOCAL, metadata_index) }; - tag.encode(s); - lo.encode(s); + // Encode the start position relative to the file start, so we profit more from the + // variable-length integer encoding. + let lo = span.lo - source_file.start_pos; // Encode length which is usually less than span.hi and profits more // from the variable-length integer encoding that we use. + let len = span.hi - span.lo; + + tag.encode(s); + lo.encode(s); len.encode(s); // Encode the index of the `SourceFile` for the span, in order to make decoding faster. diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index bae05f2f02e..8d26cd6bee3 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1094,10 +1094,6 @@ pub enum ExternalSource { Unneeded, Foreign { kind: ExternalSourceKind, - /// This SourceFile's byte-offset within the source_map of its original crate. - original_start_pos: BytePos, - /// The end of this SourceFile within the source_map of its original crate. - original_end_pos: BytePos, /// Index of the file inside metadata. metadata_index: u32, }, diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 387777f7af6..c0fbb7f2c9f 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -336,7 +336,7 @@ impl SourceMap { mut file_local_non_narrow_chars: Vec, mut file_local_normalized_pos: Vec, original_start_pos: BytePos, - original_end_pos: BytePos, + _original_end_pos: BytePos, metadata_index: u32, ) -> Lrc { let start_pos = self @@ -382,8 +382,6 @@ impl SourceMap { src_hash, external_src: Lock::new(ExternalSource::Foreign { kind: ExternalSourceKind::AbsentOk, - original_start_pos, - original_end_pos, metadata_index, }), start_pos, -- cgit 1.4.1-3-g733a5 From 0d41f9145ce4f884a53fddefe0624060eb22609b Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 8 Aug 2022 21:12:04 +0200 Subject: Remove unused parameter. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 1 - compiler/rustc_span/src/source_map.rs | 1 - compiler/rustc_span/src/source_map/tests.rs | 1 - 3 files changed, 3 deletions(-) (limited to 'compiler/rustc_span/src/source_map.rs') diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 4b2dcc7a70b..2a3693a360f 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1581,7 +1581,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { non_narrow_chars, normalized_pos, start_pos, - end_pos, source_file_index, ); debug!( diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index c0fbb7f2c9f..108b169e306 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -336,7 +336,6 @@ impl SourceMap { mut file_local_non_narrow_chars: Vec, mut file_local_normalized_pos: Vec, original_start_pos: BytePos, - _original_end_pos: BytePos, metadata_index: u32, ) -> Lrc { let start_pos = self diff --git a/compiler/rustc_span/src/source_map/tests.rs b/compiler/rustc_span/src/source_map/tests.rs index 2cada019b7f..3058ec45a64 100644 --- a/compiler/rustc_span/src/source_map/tests.rs +++ b/compiler/rustc_span/src/source_map/tests.rs @@ -251,7 +251,6 @@ fn t10() { non_narrow_chars, normalized_pos, start_pos, - end_pos, 0, ); -- cgit 1.4.1-3-g733a5 From 455a55e681c16f599bb66c9b634bcf0672f8a4c0 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Fri, 29 Jul 2022 16:34:06 +0200 Subject: Show absolute line numbers if span is outside relative span In the MIR pretty printing, it can sometimes happen that the span of the statement is outside the span of the body (for example through inlining). In this case, don't display a relative span but an absolute span. This will make the mir-opt-tests a little more prone to diffs again, but the impact should be small. --- compiler/rustc_span/src/source_map.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'compiler/rustc_span/src/source_map.rs') diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index bc76ccc1d59..5f928707ea8 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -472,7 +472,7 @@ impl SourceMap { let hi = self.lookup_char_pos(sp.hi()); let offset = self.lookup_char_pos(relative_to.lo()); - if lo.file.name != offset.file.name { + if lo.file.name != offset.file.name || !relative_to.contains(sp) { return self.span_to_embeddable_string(sp); } -- cgit 1.4.1-3-g733a5 From 6e88d738be05637824f581fc9cb59990406bf2ba Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 14 Jul 2022 17:28:34 +0200 Subject: Remove generate_fn_name_span and generate_local_type_param_snippet. --- compiler/rustc_span/src/source_map.rs | 87 ----------------------------------- 1 file changed, 87 deletions(-) (limited to 'compiler/rustc_span/src/source_map.rs') diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 5f928707ea8..a32cabab4c4 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -982,93 +982,6 @@ impl SourceMap { self.files().iter().fold(0, |a, f| a + f.count_lines()) } - pub fn generate_fn_name_span(&self, span: Span) -> Option { - let prev_span = self.span_extend_to_prev_str(span, "fn", true, true)?; - if let Ok(snippet) = self.span_to_snippet(prev_span) { - debug!( - "generate_fn_name_span: span={:?}, prev_span={:?}, snippet={:?}", - span, prev_span, snippet - ); - - if snippet.is_empty() { - return None; - }; - - let len = snippet - .find(|c: char| !c.is_alphanumeric() && c != '_') - .expect("no label after fn"); - Some(prev_span.with_hi(BytePos(prev_span.lo().0 + len as u32))) - } else { - None - } - } - - /// Takes the span of a type parameter in a function signature and try to generate a span for - /// the function name (with generics) and a new snippet for this span with the pointed type - /// parameter as a new local type parameter. - /// - /// For instance: - /// ```rust,ignore (pseudo-Rust) - /// // Given span - /// fn my_function(param: T) - /// // ^ Original span - /// - /// // Result - /// fn my_function(param: T) - /// // ^^^^^^^^^^^ Generated span with snippet `my_function` - /// ``` - /// - /// Attention: The method used is very fragile since it essentially duplicates the work of the - /// parser. If you need to use this function or something similar, please consider updating the - /// `SourceMap` functions and this function to something more robust. - pub fn generate_local_type_param_snippet(&self, span: Span) -> Option<(Span, String)> { - // Try to extend the span to the previous "fn" keyword to retrieve the function - // signature. - if let Some(sugg_span) = self.span_extend_to_prev_str(span, "fn", false, true) { - if let Ok(snippet) = self.span_to_snippet(sugg_span) { - // Consume the function name. - let mut offset = snippet - .find(|c: char| !c.is_alphanumeric() && c != '_') - .expect("no label after fn"); - - // Consume the generics part of the function signature. - let mut bracket_counter = 0; - let mut last_char = None; - for c in snippet[offset..].chars() { - match c { - '<' => bracket_counter += 1, - '>' => bracket_counter -= 1, - '(' => { - if bracket_counter == 0 { - break; - } - } - _ => {} - } - offset += c.len_utf8(); - last_char = Some(c); - } - - // Adjust the suggestion span to encompass the function name with its generics. - let sugg_span = sugg_span.with_hi(BytePos(sugg_span.lo().0 + offset as u32)); - - // Prepare the new suggested snippet to append the type parameter that triggered - // the error in the generics of the function signature. - let mut new_snippet = if last_char == Some('>') { - format!("{}, ", &snippet[..(offset - '>'.len_utf8())]) - } else { - format!("{}<", &snippet[..offset]) - }; - new_snippet - .push_str(&self.span_to_snippet(span).unwrap_or_else(|_| "T".to_string())); - new_snippet.push('>'); - - return Some((sugg_span, new_snippet)); - } - } - - None - } pub fn ensure_source_file_source_present(&self, source_file: Lrc) -> bool { source_file.add_external_src(|| { match source_file.name { -- cgit 1.4.1-3-g733a5