about summary refs log tree commit diff
path: root/compiler/rustc_span/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-11-20 10:28:05 +0000
committerbors <bors@rust-lang.org>2021-11-20 10:28:05 +0000
commit3d789740b09002e3d2be3ab1cf53fdca3995034c (patch)
tree760446b1d10e5a8b280fa4e791609da5ac3739d0 /compiler/rustc_span/src
parent6d48ee90f51dd5793b425c6593581fd108ead398 (diff)
parent3379721a30d87c396df69efa15b1307389d408df (diff)
downloadrust-3d789740b09002e3d2be3ab1cf53fdca3995034c.tar.gz
rust-3d789740b09002e3d2be3ab1cf53fdca3995034c.zip
Auto merge of #91080 - matthiaskrgr:rollup-znh88cy, r=matthiaskrgr
Rollup of 5 pull requests

Successful merges:

 - #90575 (Improve suggestions for compatible variants on type mismatch.)
 - #90628 (Clarify error messages caused by re-exporting `pub(crate)` visibility to outside)
 - #90930 (Fix `non-constant value` ICE (#90878))
 - #90983 (Make scrollbar in the sidebar always visible for visual consistency)
 - #91021 (Elaborate `Future::Output` when printing opaque `impl Future` type)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_span/src')
-rw-r--r--compiler/rustc_span/src/lib.rs1
-rw-r--r--compiler/rustc_span/src/source_map.rs19
2 files changed, 13 insertions, 7 deletions
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index dfc64f37e4c..1445c59710c 100644
--- a/compiler/rustc_span/src/lib.rs
+++ b/compiler/rustc_span/src/lib.rs
@@ -1935,6 +1935,7 @@ pub struct Loc {
 #[derive(Debug)]
 pub struct SourceFileAndLine {
     pub sf: Lrc<SourceFile>,
+    /// Index of line, starting from 0.
     pub line: usize,
 }
 #[derive(Debug)]
diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs
index 74958c49849..7414d201f51 100644
--- a/compiler/rustc_span/src/source_map.rs
+++ b/compiler/rustc_span/src/source_map.rs
@@ -593,14 +593,19 @@ impl SourceMap {
     }
 
     pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
-        match self.span_to_prev_source(sp) {
-            Err(_) => None,
-            Ok(source) => {
-                let last_line = source.rsplit_once('\n').unwrap_or(("", &source)).1;
+        Some(self.indentation_before(sp)?.len())
+    }
 
-                Some(last_line.len() - last_line.trim_start().len())
-            }
-        }
+    pub fn indentation_before(&self, sp: Span) -> Option<String> {
+        self.span_to_source(sp, |src, start_index, _| {
+            let before = &src[..start_index];
+            let last_line = before.rsplit_once('\n').map_or(before, |(_, last)| last);
+            Ok(last_line
+                .split_once(|c: char| !c.is_whitespace())
+                .map_or(last_line, |(indent, _)| indent)
+                .to_string())
+        })
+        .ok()
     }
 
     /// Returns the source snippet as `String` before the given `Span`.