From 14add46e94aa57d6f659ccfb5401e00ffacddc0b Mon Sep 17 00:00:00 2001 From: Esteban Kuber Date: Thu, 12 Aug 2021 18:54:29 +0000 Subject: Use more accurate spans when proposing adding lifetime to item --- compiler/rustc_resolve/src/late/diagnostics.rs | 39 +++++++++++++++++++++----- 1 file changed, 32 insertions(+), 7 deletions(-) (limited to 'compiler/rustc_resolve/src') diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 7c97a17b454..ac77ec1be78 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -2076,16 +2076,40 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { for param in params { if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) { if snippet.starts_with('&') && !snippet.starts_with("&'") { - introduce_suggestion - .push((param.span, format!("&'a {}", &snippet[1..]))); - } else if let Some(stripped) = snippet.strip_prefix("&'_ ") { - introduce_suggestion.push((param.span, format!("&'a {}", &stripped))); + let lo = param.span.lo() + BytePos(1); + let span = param.span.with_lo(lo).with_hi(lo); + introduce_suggestion.push((span, "'a ".to_string())); + } else if let Some(_) = snippet.strip_prefix("&'_ ") { + let lo = param.span.lo() + BytePos(1); + let hi = lo + BytePos(2); + let span = param.span.with_lo(lo).with_hi(hi); + introduce_suggestion.push((span, "'a".to_string())); } } } for ((span, _), sugg) in spans_with_counts.iter().copied().zip(suggs.iter()) { - if let Some(sugg) = sugg { - introduce_suggestion.push((span, sugg.to_string())); + match (sugg, self.tcx.sess.source_map().span_to_snippet(span)) { + (Some(sugg), Ok(snippet)) + if snippet.starts_with('&') + && !snippet.starts_with("&'") + && sugg.starts_with("&") => + { + let lo = span.lo() + BytePos(1); + let span = span.with_lo(lo).with_hi(lo); + introduce_suggestion.push((span, sugg[1..].to_string())); + } + (Some(sugg), Ok(snippet)) + if snippet.starts_with("&'_ ") && sugg.starts_with("&") => + { + let lo = span.lo() + BytePos(1); + let hi = lo + BytePos(2); + let span = span.with_lo(lo).with_hi(hi); + introduce_suggestion.push((span, sugg[1..].to_string())); + } + (Some(sugg), _) => { + introduce_suggestion.push((span, sugg.to_string())); + } + _ => {} } } err.multipart_suggestion_with_style( @@ -2159,7 +2183,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { for ((span, _), snippet) in spans_with_counts.iter().copied().zip(snippets.iter()) { match snippet.as_deref() { Some("") => spans_suggs.push((span, "'lifetime, ".to_string())), - Some("&") => spans_suggs.push((span, "&'lifetime ".to_string())), + Some("&") => spans_suggs + .push((span.with_lo(span.lo() + BytePos(1)), "'lifetime ".to_string())), _ => {} } } -- cgit 1.4.1-3-g733a5