about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2018-03-13 22:58:45 -0700
committerEsteban Küber <esteban@kuber.com.ar>2018-03-14 12:35:25 -0700
commit16d424f1471988ed4f7601b8eca42c72a23ca38e (patch)
tree7d782be01d1f34562ae6a475e9bf6404d3b5498e /src/libsyntax
parent883e74645d350b6752cb94d48f46363f6f8789e9 (diff)
downloadrust-16d424f1471988ed4f7601b8eca42c72a23ca38e.tar.gz
rust-16d424f1471988ed4f7601b8eca42c72a23ca38e.zip
Some tweaks to "type parameters from outer function" diagnostic
Follow up to #47574.
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/codemap.rs22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs
index c340f1b8c8a..951f8a871ca 100644
--- a/src/libsyntax/codemap.rs
+++ b/src/libsyntax/codemap.rs
@@ -622,13 +622,21 @@ impl CodeMap {
         sp
     }
 
-    /// Extend the given `Span` to just after the previous occurrence of `pat`. Return the same span
-    /// if no character could be found or if an error occurred while retrieving the code snippet.
-    pub fn span_extend_to_prev_str(&self, sp: Span, pat: &str) -> Span {
-        if let Ok(prev_source) = self.span_to_prev_source(sp) {
-            let prev_source = prev_source.rsplit(pat).nth(0).unwrap_or("").trim_left();
-            if !prev_source.is_empty() && !prev_source.contains('\n') {
-                return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
+    /// Extend the given `Span` to just after the previous occurrence of `pat` when surrounded by
+    /// whitespace. Return the same span if no character could be found or if an error occurred
+    /// while retrieving the code snippet.
+    pub fn span_extend_to_prev_str(&self, sp: Span, pat: &str, accept_newlines: bool) -> Span {
+        // assure that the pattern is delimited, to avoid the following
+        //     fn my_fn()
+        //           ^^^^ returned span without the check
+        //     ---------- correct span
+        for ws in &[" ", "\t", "\n"] {
+            let pat = pat.to_owned() + ws;
+            if let Ok(prev_source) = self.span_to_prev_source(sp) {
+                let prev_source = prev_source.rsplit(&pat).nth(0).unwrap_or("").trim_left();
+                if !prev_source.is_empty() && (!prev_source.contains('\n') || accept_newlines) {
+                    return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32));
+                }
             }
         }