Some tweaks to "type parameters from outer function" diagnostic

Follow up to #47574.
This commit is contained in:
Esteban Küber
2018-03-13 22:58:45 -07:00
parent 883e74645d
commit 16d424f147
4 changed files with 42 additions and 27 deletions

View File

@@ -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));
}
}
}