Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Feb 5, 2020
1 parent 30d9278 commit 49f9bf8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
21 changes: 4 additions & 17 deletions src/librustc_resolve/diagnostics.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 22 additions & 20 deletions src/librustc_resolve/lifetimes.rs
Expand Up @@ -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 {
Expand All @@ -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<MissingLifetimeSpot<'tcx>> for &'tcx hir::Generics<'tcx> {
fn into(self) -> MissingLifetimeSpot<'tcx> {
MissingLifetimeSpot::Generics(self)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
};
Expand Down

0 comments on commit 49f9bf8

Please sign in to comment.