]> git.lizzy.rs Git - rust.git/commitdiff
Use smaller span for suggestion restricting lifetime
authorEsteban Küber <esteban@kuber.com.ar>
Sun, 5 Apr 2020 23:16:55 +0000 (16:16 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Sun, 5 Apr 2020 23:16:55 +0000 (16:16 -0700)
src/librustc_mir/borrow_check/diagnostics/explain_borrow.rs
src/test/ui/impl-trait/does-not-live-long-enough.stderr

index 7340e88f19b3783ea01f613a90a190fa9e405eda..7645182ad1fb33a39809bd08cef6ea354343e83e 100644 (file)
@@ -208,45 +208,34 @@ pub(in crate::borrow_check) fn add_explanation_to_diagnostic<'tcx>(
                     );
                 };
 
-                self.add_lifetime_bound_suggestion_to_diagnostic(
-                    tcx,
-                    err,
-                    &category,
-                    span,
-                    region_name,
-                );
+                self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
             }
             _ => {}
         }
     }
-    pub(in crate::borrow_check) fn add_lifetime_bound_suggestion_to_diagnostic<'tcx>(
+    pub(in crate::borrow_check) fn add_lifetime_bound_suggestion_to_diagnostic(
         &self,
-        tcx: TyCtxt<'tcx>,
         err: &mut DiagnosticBuilder<'_>,
         category: &ConstraintCategory,
         span: Span,
         region_name: &RegionName,
     ) {
         if let ConstraintCategory::OpaqueType = category {
-            if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(span) {
-                let suggestable_name = if region_name.was_named() {
-                    region_name.to_string()
-                } else {
-                    "'_".to_string()
-                };
+            let suggestable_name =
+                if region_name.was_named() { region_name.to_string() } else { "'_".to_string() };
 
-                err.span_suggestion(
-                    span,
-                    &format!(
-                        "you can add a bound to the {}to make it last less than \
-                             `'static` and match `{}`",
-                        category.description(),
-                        region_name,
-                    ),
-                    format!("{} + {}", snippet, suggestable_name),
-                    Applicability::Unspecified,
-                );
-            }
+            let msg = format!(
+                "you can add a bound to the {}to make it last less than `'static` and match `{}`",
+                category.description(),
+                region_name,
+            );
+
+            err.span_suggestion_verbose(
+                span.shrink_to_hi(),
+                &msg,
+                format!(" + {}", suggestable_name),
+                Applicability::Unspecified,
+            );
         }
     }
 }
index 83d0f87015bf9478756573f4f34ba33a5b1870dd..9cff4bcd8b579df62d664c6f3b3f9cdc17d0833c 100644 (file)
@@ -14,7 +14,7 @@ LL |     }
 help: you can add a bound to the opaque type to make it last less than `'static` and match `'a`
    |
 LL |     fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> + 'a {
-   |                                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |                                                                                   ^^^^
 
 error: aborting due to previous error