]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_resolve/lifetimes.rs
Rollup merge of #68469 - ollie27:skip_count, r=sfackler
[rust.git] / src / librustc_resolve / lifetimes.rs
index 1fb35ca26d6f8d5c6a1ac9a68bbde4ebd83aee5d..6e9ed5fdc179cd1928892ff0fde44d671f6ef2c2 100644 (file)
@@ -5,14 +5,16 @@
 //! used between functions, and they operate in a purely top-down
 //! way. Therefore, we break lifetime name resolution into a separate pass.
 
+use crate::diagnostics::{
+    add_missing_lifetime_specifiers_label, report_missing_lifetime_specifiers,
+};
 use rustc::hir::map::Map;
 use rustc::lint;
 use rustc::middle::resolve_lifetime::*;
-use rustc::session::Session;
 use rustc::ty::{self, DefIdTree, GenericParamDefKind, TyCtxt};
 use rustc::{bug, span_bug};
 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
-use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
+use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
 use rustc_hir as hir;
 use rustc_hir::def::{DefKind, Res};
 use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
@@ -30,8 +32,6 @@
 
 use log::debug;
 
-use rustc_error_codes::*;
-
 // This counts the no of times a lifetime is used
 #[derive(Clone, Copy, Debug)]
 pub enum LifetimeUseSet<'tcx> {
@@ -1320,9 +1320,10 @@ fn with<F>(&mut self, wrap_scope: Scope<'_>, f: F)
     where
         F: for<'b> FnOnce(ScopeRef<'_>, &mut LifetimeContext<'b, 'tcx>),
     {
-        let LifetimeContext { tcx, map, lifetime_uses, missing_named_lifetime_spots, .. } = self;
+        let LifetimeContext { tcx, map, lifetime_uses, .. } = self;
         let labels_in_fn = take(&mut self.labels_in_fn);
         let xcrate_object_lifetime_defaults = take(&mut self.xcrate_object_lifetime_defaults);
+        let missing_named_lifetime_spots = take(&mut self.missing_named_lifetime_spots);
         let mut this = LifetimeContext {
             tcx: *tcx,
             map: map,
@@ -1332,7 +1333,7 @@ fn with<F>(&mut self, wrap_scope: Scope<'_>, f: F)
             labels_in_fn,
             xcrate_object_lifetime_defaults,
             lifetime_uses,
-            missing_named_lifetime_spots: missing_named_lifetime_spots.to_vec(),
+            missing_named_lifetime_spots,
         };
         debug!("entering scope {:?}", this.scope);
         f(self.scope, &mut this);
@@ -1340,6 +1341,7 @@ fn with<F>(&mut self, wrap_scope: Scope<'_>, f: F)
         debug!("exiting scope {:?}", this.scope);
         self.labels_in_fn = this.labels_in_fn;
         self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
+        self.missing_named_lifetime_spots = this.missing_named_lifetime_spots;
     }
 
     /// helper method to determine the span to remove when suggesting the
@@ -2894,95 +2896,3 @@ fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
         }
     }
 }
-
-fn report_missing_lifetime_specifiers(
-    sess: &Session,
-    span: Span,
-    count: usize,
-) -> DiagnosticBuilder<'_> {
-    struct_span_err!(sess, span, E0106, "missing lifetime specifier{}", pluralize!(count))
-}
-
-fn add_missing_lifetime_specifiers_label(
-    err: &mut DiagnosticBuilder<'_>,
-    span: Span,
-    count: usize,
-    lifetime_names: &FxHashSet<ast::Ident>,
-    snippet: Option<&str>,
-    missing_named_lifetime_spots: &[&hir::Generics<'_>],
-) {
-    if count > 1 {
-        err.span_label(span, format!("expected {} lifetime parameters", count));
-    } else {
-        let mut introduce_suggestion = vec![];
-        if let Some(generics) = missing_named_lifetime_spots.iter().last() {
-            introduce_suggestion.push(match &generics.params {
-                [] => (generics.span, "<'lifetime>".to_string()),
-                [param, ..] => (param.span.shrink_to_lo(), "'lifetime, ".to_string()),
-            });
-        }
-
-        match (lifetime_names.len(), lifetime_names.iter().next(), snippet) {
-            (1, Some(name), Some("&")) => {
-                err.span_suggestion(
-                    span,
-                    "consider using the named lifetime",
-                    format!("&{} ", name),
-                    Applicability::MaybeIncorrect,
-                );
-            }
-            (1, Some(name), Some("'_")) => {
-                err.span_suggestion(
-                    span,
-                    "consider using the named lifetime",
-                    name.to_string(),
-                    Applicability::MaybeIncorrect,
-                );
-            }
-            (1, Some(name), Some(snippet)) if !snippet.ends_with(">") => {
-                err.span_suggestion(
-                    span,
-                    "consider using the named lifetime",
-                    format!("{}<{}>", snippet, name),
-                    Applicability::MaybeIncorrect,
-                );
-            }
-            (0, _, Some("&")) => {
-                err.span_label(span, "expected named lifetime parameter");
-                if !introduce_suggestion.is_empty() {
-                    introduce_suggestion.push((span, "&'lifetime ".to_string()));
-                    err.multipart_suggestion(
-                        "consider introducing a named lifetime parameter",
-                        introduce_suggestion,
-                        Applicability::MaybeIncorrect,
-                    );
-                }
-            }
-            (0, _, Some("'_")) => {
-                err.span_label(span, "expected named lifetime parameter");
-                if !introduce_suggestion.is_empty() {
-                    introduce_suggestion.push((span, "'lifetime".to_string()));
-                    err.multipart_suggestion(
-                        "consider introducing a named lifetime parameter",
-                        introduce_suggestion,
-                        Applicability::MaybeIncorrect,
-                    );
-                }
-            }
-            (0, _, Some(snippet)) if !snippet.ends_with(">") => {
-                err.span_label(span, "expected named lifetime parameter");
-                if !introduce_suggestion.is_empty() {
-                    introduce_suggestion.push((span, format!("{}<'lifetime>", snippet)));
-                    err.multipart_suggestion(
-                        "consider introducing a named lifetime parameter",
-                        introduce_suggestion,
-                        Applicability::MaybeIncorrect,
-                    );
-                }
-            }
-            _ => {
-                err.span_label(span, "expected lifetime parameter");
-            }
-        }
-    }
-}