about summary refs log tree commit diff
path: root/compiler/rustc_span/src/source_map.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-02-08 20:52:54 +0000
committerbors <bors@rust-lang.org>2021-02-08 20:52:54 +0000
commit0fc6756b42e0556cc2e18079f5fc6b4d58f4e81a (patch)
treeb772cfec476469c0839bdcc49ef177f0a5229ec5 /compiler/rustc_span/src/source_map.rs
parent921ec4b3fca17cc777766c240038d7d50ba98e0d (diff)
parent9d1e8fe045d8512309fa3303d188ccbf06b9542f (diff)
downloadrust-0fc6756b42e0556cc2e18079f5fc6b4d58f4e81a.tar.gz
rust-0fc6756b42e0556cc2e18079f5fc6b4d58f4e81a.zip
Auto merge of #81889 - m-ou-se:rollup-k63log3, r=m-ou-se
Rollup of 9 pull requests

Successful merges:

 - #71531 (Move treat err as bug tests to ui)
 - #81356 (libtest: allow multiple filters)
 - #81735 (faster few span methods)
 - #81779 (improve error message for disallowed ptr-to-int casts in const eval)
 - #81817 (Add option to emit compiler stderr per bitwidth.)
 - #81828 (parse_format: treat r" as a literal)
 - #81840 (fix formatting of std::iter::Map)
 - #81861 (Show MIR bytes separately in -Zmeta-stats output)
 - #81865 (Clean up weird Option mapping)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_span/src/source_map.rs')
-rw-r--r--compiler/rustc_span/src/source_map.rs17
1 files changed, 9 insertions, 8 deletions
diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs
index 2b429372dcf..298c995b166 100644
--- a/compiler/rustc_span/src/source_map.rs
+++ b/compiler/rustc_span/src/source_map.rs
@@ -539,7 +539,7 @@ impl SourceMap {
 
     pub fn is_line_before_span_empty(&self, sp: Span) -> bool {
         match self.span_to_prev_source(sp) {
-            Ok(s) => s.split('\n').last().map_or(false, |l| l.trim_start().is_empty()),
+            Ok(s) => s.rsplit_once('\n').unwrap_or(("", &s)).1.trim_start().is_empty(),
             Err(_) => false,
         }
     }
@@ -632,10 +632,11 @@ impl SourceMap {
     pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
         match self.span_to_prev_source(sp) {
             Err(_) => None,
-            Ok(source) => source
-                .split('\n')
-                .last()
-                .map(|last_line| last_line.len() - last_line.trim_start().len()),
+            Ok(source) => {
+                let last_line = source.rsplit_once('\n').unwrap_or(("", &source)).1;
+
+                Some(last_line.len() - last_line.trim_start().len())
+            }
         }
     }
 
@@ -651,7 +652,7 @@ impl SourceMap {
     pub fn span_extend_to_prev_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
         if let Ok(prev_source) = self.span_to_prev_source(sp) {
             let prev_source = prev_source.rsplit(c).next().unwrap_or("");
-            if !prev_source.is_empty() && (!prev_source.contains('\n') || accept_newlines) {
+            if !prev_source.is_empty() && (accept_newlines || !prev_source.contains('\n')) {
                 return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
             }
         }
@@ -673,7 +674,7 @@ impl SourceMap {
                 let prev_source = prev_source.rsplit(&pat).next().unwrap_or("").trim_start();
                 if prev_source.is_empty() && sp.lo().0 != 0 {
                     return sp.with_lo(BytePos(sp.lo().0 - 1));
-                } else if !prev_source.contains('\n') || accept_newlines {
+                } else if accept_newlines || !prev_source.contains('\n') {
                     return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
                 }
             }
@@ -693,7 +694,7 @@ impl SourceMap {
     pub fn span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
         if let Ok(next_source) = self.span_to_next_source(sp) {
             let next_source = next_source.split(c).next().unwrap_or("");
-            if !next_source.is_empty() && (!next_source.contains('\n') || accept_newlines) {
+            if !next_source.is_empty() && (accept_newlines || !next_source.contains('\n')) {
                 return sp.with_hi(BytePos(sp.hi().0 + next_source.len() as u32));
             }
         }