Skip to content

Commit

Permalink
Handle allow(elided_lifetimes_in_paths).
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Nov 30, 2021
1 parent c07a6d2 commit 72dc29c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
59 changes: 28 additions & 31 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1951,38 +1951,35 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
}

crate fn report_elided_lifetime_in_ty(&self, lifetime_refs: &[&hir::Lifetime]) {
let missing_lifetimes = lifetime_refs
.iter()
.filter(|a| matches!(a, hir::Lifetime { name: hir::LifetimeName::ImplicitMissing, .. }))
.count();

if missing_lifetimes > 0 {
let mut spans: Vec<_> = lifetime_refs.iter().map(|lt| lt.span).collect();
spans.sort();
let mut spans_dedup = spans.clone();
spans_dedup.dedup();
let spans_with_counts: Vec<_> = spans_dedup
.into_iter()
.map(|sp| (sp, spans.iter().filter(|nsp| *nsp == &sp).count()))
.collect();
let Some(missing_lifetime) = lifetime_refs.iter().find(|lt| {
lt.name == hir::LifetimeName::ImplicitMissing
}) else { return };

let mut spans: Vec<_> = lifetime_refs.iter().map(|lt| lt.span).collect();
spans.sort();
let mut spans_dedup = spans.clone();
spans_dedup.dedup();
let spans_with_counts: Vec<_> = spans_dedup
.into_iter()
.map(|sp| (sp, spans.iter().filter(|nsp| *nsp == &sp).count()))
.collect();

self.tcx.struct_span_lint_hir(
rustc_session::lint::builtin::ELIDED_LIFETIMES_IN_PATHS,
hir::CRATE_HIR_ID,
spans,
|lint| {
let mut db = lint.build("hidden lifetime parameters in types are deprecated");
self.add_missing_lifetime_specifiers_label(
&mut db,
spans_with_counts,
&FxHashSet::from_iter([kw::UnderscoreLifetime]),
Vec::new(),
&[],
);
db.emit()
},
);
}
self.tcx.struct_span_lint_hir(
rustc_session::lint::builtin::ELIDED_LIFETIMES_IN_PATHS,
missing_lifetime.hir_id,
spans,
|lint| {
let mut db = lint.build("hidden lifetime parameters in types are deprecated");
self.add_missing_lifetime_specifiers_label(
&mut db,
spans_with_counts,
&FxHashSet::from_iter([kw::UnderscoreLifetime]),
Vec::new(),
&[],
);
db.emit()
},
);
}

// FIXME(const_generics): This patches over an ICE caused by non-'static lifetimes in const
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/in-band-lifetimes/elided-lifetimes.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ macro_rules! anytuple_ref_ty {
}
}

#[allow(elided_lifetimes_in_paths)]
mod blah {
struct Thing<'a>(&'a i32);
struct Bar<T>(T);

fn foo(b: Bar<Thing>) {}
}

fn main() {
let honesty = RefCell::new((4, 'e'));
let loyalty: Ref<'_, (u32, char)> = honesty.borrow();
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/in-band-lifetimes/elided-lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ macro_rules! anytuple_ref_ty {
}
}

#[allow(elided_lifetimes_in_paths)]
mod blah {
struct Thing<'a>(&'a i32);
struct Bar<T>(T);

fn foo(b: Bar<Thing>) {}
}

fn main() {
let honesty = RefCell::new((4, 'e'));
let loyalty: Ref<(u32, char)> = honesty.borrow();
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/in-band-lifetimes/elided-lifetimes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ LL | fn $fn_name(gift: &str) -> $type_name<'_> {
| ~~~~~~~~~~~~~~

error: hidden lifetime parameters in types are deprecated
--> $DIR/elided-lifetimes.rs:101:22
--> $DIR/elided-lifetimes.rs:109:22
|
LL | let loyalty: Ref<(u32, char)> = honesty.borrow();
| ^ expected named lifetime parameter
Expand Down

0 comments on commit 72dc29c

Please sign in to comment.