From 49f9bf897b93349464c2046047991b3e77ca0f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 27 Jan 2020 17:33:13 -0800 Subject: [PATCH] review comments --- src/librustc_resolve/diagnostics.rs | 21 +++------------ src/librustc_resolve/lifetimes.rs | 42 +++++++++++++++-------------- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index c254207cc3e7c..be7a9de4317c5 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -19,7 +19,7 @@ use syntax::ast::{self, Ident, Path}; use syntax::util::lev_distance::find_best_match_for_name; use crate::imports::{ImportDirective, ImportDirectiveSubclass, ImportResolver}; -use crate::lifetimes::{ElisionFailureInfo, ForLifetimeSpanType, MissingLifetimeSpot}; +use crate::lifetimes::{ElisionFailureInfo, MissingLifetimeSpot}; use crate::path_names_to_string; use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind}; use crate::{BindingError, CrateLint, HasGenericParams, LegacyScope, Module, ModuleOrUniformRoot}; @@ -1502,30 +1502,17 @@ crate fn add_missing_lifetime_specifiers_label( [param, ..] => (param.span.shrink_to_lo(), "'a, ".to_string()), } } - MissingLifetimeSpot::HRLT { span, span_type } => { + MissingLifetimeSpot::HigherRanked { span, span_type } => { msg = format!( "consider making the {} lifetime-generic with a new `'a` lifetime", - match span_type { - ForLifetimeSpanType::BoundEmpty - | ForLifetimeSpanType::BoundTail => "bound", - ForLifetimeSpanType::TypeEmpty | ForLifetimeSpanType::TypeTail => - "type", - } + span_type.descr(), ); should_break = false; err.note( "for more information on higher-ranked polymorphism, visit \ https://doc.rust-lang.org/nomicon/hrtb.html", ); - let suggestion = match span_type { - ForLifetimeSpanType::BoundEmpty | ForLifetimeSpanType::TypeEmpty => { - "for<'a> " - } - ForLifetimeSpanType::BoundTail | ForLifetimeSpanType::TypeTail => { - ", 'a" - } - }; - (*span, suggestion.to_string()) + (*span, span_type.suggestion("'a")) } }); for param in params { diff --git a/src/librustc_resolve/lifetimes.rs b/src/librustc_resolve/lifetimes.rs index b69eea6634b5a..811b36022a5b9 100644 --- a/src/librustc_resolve/lifetimes.rs +++ b/src/librustc_resolve/lifetimes.rs @@ -155,7 +155,7 @@ struct NamedRegionMap { crate enum MissingLifetimeSpot<'tcx> { Generics(&'tcx hir::Generics<'tcx>), - HRLT { span: Span, span_type: ForLifetimeSpanType }, + HigherRanked { span: Span, span_type: ForLifetimeSpanType }, } crate enum ForLifetimeSpanType { @@ -165,6 +165,22 @@ crate enum ForLifetimeSpanType { TypeTail, } +impl ForLifetimeSpanType { + crate fn descr(&self) -> &'static str { + match self { + Self::BoundEmpty | Self::BoundTail => "bound", + Self::TypeEmpty | Self::TypeTail => "type", + } + } + + crate fn suggestion(&self, sugg: &str) -> String { + match self { + Self::BoundEmpty | Self::TypeEmpty => format!("for<{}> ", sugg), + Self::BoundTail | Self::TypeTail => format!(", {}", sugg), + } + } +} + impl<'tcx> Into> for &'tcx hir::Generics<'tcx> { fn into(self) -> MissingLifetimeSpot<'tcx> { MissingLifetimeSpot::Generics(self) @@ -525,7 +541,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { (ty.span.shrink_to_lo(), ForLifetimeSpanType::TypeEmpty) }; self.missing_named_lifetime_spots - .push(MissingLifetimeSpot::HRLT { span, span_type }); + .push(MissingLifetimeSpot::HigherRanked { span, span_type }); let scope = Scope::Binder { lifetimes: c .generic_params @@ -1887,29 +1903,15 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { Applicability::MaybeIncorrect, ); } - MissingLifetimeSpot::HRLT { span, span_type } => { + MissingLifetimeSpot::HigherRanked { span, span_type } => { err.span_suggestion( *span, &format!( "consider making the {} lifetime-generic with a new `{}` lifetime", - match span_type { - ForLifetimeSpanType::BoundEmpty - | ForLifetimeSpanType::BoundTail => "bound", - ForLifetimeSpanType::TypeEmpty - | ForLifetimeSpanType::TypeTail => "type", - }, + span_type.descr(), lifetime_ref ), - match span_type { - ForLifetimeSpanType::TypeEmpty - | ForLifetimeSpanType::BoundEmpty => { - format!("for<{}> ", lifetime_ref) - } - ForLifetimeSpanType::TypeTail | ForLifetimeSpanType::BoundTail => { - format!(", {}", lifetime_ref) - } - } - .to_string(), + span_type.suggestion(&lifetime_ref.to_string()), Applicability::MaybeIncorrect, ); err.note( @@ -2840,7 +2842,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { [.., bound] => (bound.span.shrink_to_hi(), ForLifetimeSpanType::BoundTail), }; self.missing_named_lifetime_spots - .push(MissingLifetimeSpot::HRLT { span, span_type }); + .push(MissingLifetimeSpot::HigherRanked { span, span_type }); return true; } };