Skip to content

Commit 03d7fed

Browse files
committed
review comments
1 parent 0a6b553 commit 03d7fed

File tree

2 files changed

+81
-76
lines changed

2 files changed

+81
-76
lines changed

src/librustc_resolve/diagnostics.rs

+73-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use rustc::bug;
55
use rustc::session::Session;
66
use rustc::ty::{self, DefIdTree};
77
use rustc_data_structures::fx::FxHashSet;
8-
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
8+
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
99
use rustc_feature::BUILTIN_ATTRIBUTES;
10+
use rustc_hir as hir;
1011
use rustc_hir::def::Namespace::{self, *};
1112
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind};
1213
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
@@ -1447,3 +1448,74 @@ crate fn show_candidates(
14471448
}
14481449
}
14491450
}
1451+
1452+
crate fn report_missing_lifetime_specifiers(
1453+
sess: &Session,
1454+
span: Span,
1455+
count: usize,
1456+
) -> DiagnosticBuilder<'_> {
1457+
struct_span_err!(sess, span, E0106, "missing lifetime specifier{}", pluralize!(count))
1458+
}
1459+
1460+
crate fn add_missing_lifetime_specifiers_label(
1461+
err: &mut DiagnosticBuilder<'_>,
1462+
span: Span,
1463+
count: usize,
1464+
lifetime_names: &FxHashSet<ast::Ident>,
1465+
snippet: Option<&str>,
1466+
missing_named_lifetime_spots: &[&hir::Generics<'_>],
1467+
) {
1468+
if count > 1 {
1469+
err.span_label(span, format!("expected {} lifetime parameters", count));
1470+
} else {
1471+
let suggest_existing = |err: &mut DiagnosticBuilder<'_>, sugg| {
1472+
err.span_suggestion(
1473+
span,
1474+
"consider using the named lifetime",
1475+
sugg,
1476+
Applicability::MaybeIncorrect,
1477+
);
1478+
};
1479+
let suggest_new = |err: &mut DiagnosticBuilder<'_>, sugg| {
1480+
err.span_label(span, "expected named lifetime parameter");
1481+
1482+
if let Some(generics) = missing_named_lifetime_spots.iter().last() {
1483+
let mut introduce_suggestion = vec![];
1484+
introduce_suggestion.push(match &generics.params {
1485+
[] => (generics.span, "<'lifetime>".to_string()),
1486+
[param, ..] => (param.span.shrink_to_lo(), "'lifetime, ".to_string()),
1487+
});
1488+
introduce_suggestion.push((span, sugg));
1489+
err.multipart_suggestion(
1490+
"consider introducing a named lifetime parameter",
1491+
introduce_suggestion,
1492+
Applicability::MaybeIncorrect,
1493+
);
1494+
}
1495+
};
1496+
1497+
match (lifetime_names.len(), lifetime_names.iter().next(), snippet) {
1498+
(1, Some(name), Some("&")) => {
1499+
suggest_existing(err, format!("&{} ", name));
1500+
}
1501+
(1, Some(name), Some("'_")) => {
1502+
suggest_existing(err, name.to_string());
1503+
}
1504+
(1, Some(name), Some(snippet)) if !snippet.ends_with(">") => {
1505+
suggest_existing(err, format!("{}<{}>", snippet, name));
1506+
}
1507+
(0, _, Some("&")) => {
1508+
suggest_new(err, "&'lifetime ".to_string());
1509+
}
1510+
(0, _, Some("'_")) => {
1511+
suggest_new(err, "'lifetime".to_string());
1512+
}
1513+
(0, _, Some(snippet)) if !snippet.ends_with(">") => {
1514+
suggest_new(err, format!("{}<'lifetime>", snippet));
1515+
}
1516+
_ => {
1517+
err.span_label(span, "expected lifetime parameter");
1518+
}
1519+
}
1520+
}
1521+
}

src/librustc_resolve/lifetimes.rs

+8-75
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
//! used between functions, and they operate in a purely top-down
66
//! way. Therefore, we break lifetime name resolution into a separate pass.
77
8+
use crate::diagnostics::{
9+
add_missing_lifetime_specifiers_label, report_missing_lifetime_specifiers,
10+
};
811
use rustc::hir::map::Map;
912
use rustc::lint;
1013
use rustc::middle::resolve_lifetime::*;
11-
use rustc::session::Session;
1214
use rustc::ty::{self, DefIdTree, GenericParamDefKind, TyCtxt};
1315
use rustc::{bug, span_bug};
1416
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
15-
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
17+
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
1618
use rustc_hir as hir;
1719
use rustc_hir::def::{DefKind, Res};
1820
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
@@ -1320,9 +1322,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13201322
where
13211323
F: for<'b> FnOnce(ScopeRef<'_>, &mut LifetimeContext<'b, 'tcx>),
13221324
{
1323-
let LifetimeContext { tcx, map, lifetime_uses, missing_named_lifetime_spots, .. } = self;
1325+
let LifetimeContext { tcx, map, lifetime_uses, .. } = self;
13241326
let labels_in_fn = take(&mut self.labels_in_fn);
13251327
let xcrate_object_lifetime_defaults = take(&mut self.xcrate_object_lifetime_defaults);
1328+
let missing_named_lifetime_spots = take(&mut self.missing_named_lifetime_spots);
13261329
let mut this = LifetimeContext {
13271330
tcx: *tcx,
13281331
map: map,
@@ -1332,14 +1335,15 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13321335
labels_in_fn,
13331336
xcrate_object_lifetime_defaults,
13341337
lifetime_uses,
1335-
missing_named_lifetime_spots: missing_named_lifetime_spots.to_vec(),
1338+
missing_named_lifetime_spots,
13361339
};
13371340
debug!("entering scope {:?}", this.scope);
13381341
f(self.scope, &mut this);
13391342
this.check_uses_for_lifetimes_defined_by_scope();
13401343
debug!("exiting scope {:?}", this.scope);
13411344
self.labels_in_fn = this.labels_in_fn;
13421345
self.xcrate_object_lifetime_defaults = this.xcrate_object_lifetime_defaults;
1346+
self.missing_named_lifetime_spots = this.missing_named_lifetime_spots;
13431347
}
13441348

13451349
/// helper method to determine the span to remove when suggesting the
@@ -2894,74 +2898,3 @@ fn insert_late_bound_lifetimes(
28942898
}
28952899
}
28962900
}
2897-
2898-
fn report_missing_lifetime_specifiers(
2899-
sess: &Session,
2900-
span: Span,
2901-
count: usize,
2902-
) -> DiagnosticBuilder<'_> {
2903-
struct_span_err!(sess, span, E0106, "missing lifetime specifier{}", pluralize!(count))
2904-
}
2905-
2906-
fn add_missing_lifetime_specifiers_label(
2907-
err: &mut DiagnosticBuilder<'_>,
2908-
span: Span,
2909-
count: usize,
2910-
lifetime_names: &FxHashSet<ast::Ident>,
2911-
snippet: Option<&str>,
2912-
missing_named_lifetime_spots: &[&hir::Generics<'_>],
2913-
) {
2914-
if count > 1 {
2915-
err.span_label(span, format!("expected {} lifetime parameters", count));
2916-
} else {
2917-
let suggest_existing = |err: &mut DiagnosticBuilder<'_>, sugg| {
2918-
err.span_suggestion(
2919-
span,
2920-
"consider using the named lifetime",
2921-
sugg,
2922-
Applicability::MaybeIncorrect,
2923-
);
2924-
};
2925-
let suggest_new = |err: &mut DiagnosticBuilder<'_>, sugg| {
2926-
err.span_label(span, "expected named lifetime parameter");
2927-
2928-
if let Some(generics) = missing_named_lifetime_spots.iter().last() {
2929-
let mut introduce_suggestion = vec![];
2930-
introduce_suggestion.push(match &generics.params {
2931-
[] => (generics.span, "<'lifetime>".to_string()),
2932-
[param, ..] => (param.span.shrink_to_lo(), "'lifetime, ".to_string()),
2933-
});
2934-
introduce_suggestion.push((span, sugg));
2935-
err.multipart_suggestion(
2936-
"consider introducing a named lifetime parameter",
2937-
introduce_suggestion,
2938-
Applicability::MaybeIncorrect,
2939-
);
2940-
}
2941-
};
2942-
2943-
match (lifetime_names.len(), lifetime_names.iter().next(), snippet) {
2944-
(1, Some(name), Some("&")) => {
2945-
suggest_existing(err, format!("&{} ", name));
2946-
}
2947-
(1, Some(name), Some("'_")) => {
2948-
suggest_existing(err, name.to_string());
2949-
}
2950-
(1, Some(name), Some(snippet)) if !snippet.ends_with(">") => {
2951-
suggest_existing(err, format!("{}<{}>", snippet, name));
2952-
}
2953-
(0, _, Some("&")) => {
2954-
suggest_new(err, "&'lifetime ".to_string());
2955-
}
2956-
(0, _, Some("'_")) => {
2957-
suggest_new(err, "'lifetime".to_string());
2958-
}
2959-
(0, _, Some(snippet)) if !snippet.ends_with(">") => {
2960-
suggest_new(err, format!("{}<'lifetime>", snippet));
2961-
}
2962-
_ => {
2963-
err.span_label(span, "expected lifetime parameter");
2964-
}
2965-
}
2966-
}
2967-
}

0 commit comments

Comments
 (0)