Skip to content

Commit 33513e6

Browse files
committed
review comments
1 parent 8a9a5a3 commit 33513e6

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

src/librustc_resolve/diagnostics.rs

+4-17
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::print::pprust;
1919
use syntax::util::lev_distance::find_best_match_for_name;
2020

2121
use crate::imports::{ImportDirective, ImportDirectiveSubclass, ImportResolver};
22-
use crate::lifetimes::{ElisionFailureInfo, ForLifetimeSpanType, MissingLifetimeSpot};
22+
use crate::lifetimes::{ElisionFailureInfo, MissingLifetimeSpot};
2323
use crate::path_names_to_string;
2424
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind};
2525
use crate::{BindingError, CrateLint, HasGenericParams, LegacyScope, Module, ModuleOrUniformRoot};
@@ -1497,30 +1497,17 @@ crate fn add_missing_lifetime_specifiers_label(
14971497
[param, ..] => (param.span.shrink_to_lo(), "'a, ".to_string()),
14981498
}
14991499
}
1500-
MissingLifetimeSpot::HRLT { span, span_type } => {
1500+
MissingLifetimeSpot::HigherRanked { span, span_type } => {
15011501
msg = format!(
15021502
"consider making the {} lifetime-generic with a new `'a` lifetime",
1503-
match span_type {
1504-
ForLifetimeSpanType::BoundEmpty
1505-
| ForLifetimeSpanType::BoundTail => "bound",
1506-
ForLifetimeSpanType::TypeEmpty | ForLifetimeSpanType::TypeTail =>
1507-
"type",
1508-
}
1503+
span_type.descr(),
15091504
);
15101505
should_break = false;
15111506
err.note(
15121507
"for more information on higher-ranked polymorphism, visit \
15131508
https://doc.rust-lang.org/nomicon/hrtb.html",
15141509
);
1515-
let suggestion = match span_type {
1516-
ForLifetimeSpanType::BoundEmpty | ForLifetimeSpanType::TypeEmpty => {
1517-
"for<'a> "
1518-
}
1519-
ForLifetimeSpanType::BoundTail | ForLifetimeSpanType::TypeTail => {
1520-
", 'a"
1521-
}
1522-
};
1523-
(*span, suggestion.to_string())
1510+
(*span, span_type.suggestion("'a"))
15241511
}
15251512
});
15261513
for param in params {

src/librustc_resolve/lifetimes.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ struct NamedRegionMap {
155155

156156
crate enum MissingLifetimeSpot<'tcx> {
157157
Generics(&'tcx hir::Generics<'tcx>),
158-
HRLT { span: Span, span_type: ForLifetimeSpanType },
158+
HigherRanked { span: Span, span_type: ForLifetimeSpanType },
159159
}
160160

161161
crate enum ForLifetimeSpanType {
@@ -165,6 +165,22 @@ crate enum ForLifetimeSpanType {
165165
TypeTail,
166166
}
167167

168+
impl ForLifetimeSpanType {
169+
crate fn descr(&self) -> &'static str {
170+
match self {
171+
Self::BoundEmpty | Self::BoundTail => "bound",
172+
Self::TypeEmpty | Self::TypeTail => "type",
173+
}
174+
}
175+
176+
crate fn suggestion(&self, sugg: &str) -> String {
177+
match self {
178+
Self::BoundEmpty | Self::TypeEmpty => format!("for<{}> ", sugg),
179+
Self::BoundTail | Self::TypeTail => format!(", {}", sugg),
180+
}
181+
}
182+
}
183+
168184
impl<'tcx> Into<MissingLifetimeSpot<'tcx>> for &'tcx hir::Generics<'tcx> {
169185
fn into(self) -> MissingLifetimeSpot<'tcx> {
170186
MissingLifetimeSpot::Generics(self)
@@ -525,7 +541,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
525541
(ty.span.shrink_to_lo(), ForLifetimeSpanType::TypeEmpty)
526542
};
527543
self.missing_named_lifetime_spots
528-
.push(MissingLifetimeSpot::HRLT { span, span_type });
544+
.push(MissingLifetimeSpot::HigherRanked { span, span_type });
529545
let scope = Scope::Binder {
530546
lifetimes: c
531547
.generic_params
@@ -1887,29 +1903,15 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
18871903
Applicability::MaybeIncorrect,
18881904
);
18891905
}
1890-
MissingLifetimeSpot::HRLT { span, span_type } => {
1906+
MissingLifetimeSpot::HigherRanked { span, span_type } => {
18911907
err.span_suggestion(
18921908
*span,
18931909
&format!(
18941910
"consider making the {} lifetime-generic with a new `{}` lifetime",
1895-
match span_type {
1896-
ForLifetimeSpanType::BoundEmpty
1897-
| ForLifetimeSpanType::BoundTail => "bound",
1898-
ForLifetimeSpanType::TypeEmpty
1899-
| ForLifetimeSpanType::TypeTail => "type",
1900-
},
1911+
span_type.descr(),
19011912
lifetime_ref
19021913
),
1903-
match span_type {
1904-
ForLifetimeSpanType::TypeEmpty
1905-
| ForLifetimeSpanType::BoundEmpty => {
1906-
format!("for<{}> ", lifetime_ref)
1907-
}
1908-
ForLifetimeSpanType::TypeTail | ForLifetimeSpanType::BoundTail => {
1909-
format!(", {}", lifetime_ref)
1910-
}
1911-
}
1912-
.to_string(),
1914+
span_type.suggestion(&lifetime_ref.to_string()),
19131915
Applicability::MaybeIncorrect,
19141916
);
19151917
err.note(
@@ -2840,7 +2842,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
28402842
[.., bound] => (bound.span.shrink_to_hi(), ForLifetimeSpanType::BoundTail),
28412843
};
28422844
self.missing_named_lifetime_spots
2843-
.push(MissingLifetimeSpot::HRLT { span, span_type });
2845+
.push(MissingLifetimeSpot::HigherRanked { span, span_type });
28442846
return true;
28452847
}
28462848
};

0 commit comments

Comments
 (0)