about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src
diff options
context:
space:
mode:
authorEsteban Kuber <esteban@kuber.com.ar>2021-08-12 18:54:29 +0000
committerEsteban Kuber <esteban@kuber.com.ar>2021-08-18 10:25:15 +0000
commit14add46e94aa57d6f659ccfb5401e00ffacddc0b (patch)
tree8d8c90631bd663e4661bffc7934d6c72de81f078 /compiler/rustc_resolve/src
parent679dea4cc3fbddc6717cc9ee2b5f906f1e538df6 (diff)
downloadrust-14add46e94aa57d6f659ccfb5401e00ffacddc0b.tar.gz
rust-14add46e94aa57d6f659ccfb5401e00ffacddc0b.zip
Use more accurate spans when proposing adding lifetime to item
Diffstat (limited to 'compiler/rustc_resolve/src')
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs39
1 files changed, 32 insertions, 7 deletions
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())),
                         _ => {}
                     }
                 }