From f8410670aded628f354236b835c92f650f9d1cd2 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Wed, 4 Jul 2018 14:07:55 -0700 Subject: [PATCH] in which the elided-lifetimes-in-paths lint stops firing on `'_` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `is_elided` is true for both underscore (`'_`) and implicit (no representation in source code) lifetimes, but we don't want to fire the lint on the former, because the entire point of the lint is to suggest changing the latter to the former (see the initial issue #45992). It seems unfortunate for there to be ambiguity on whether the word "elided" includes underscore lifetimes or not—the mandate of the elided-lifetimes-in-paths lint seems to suggest it doesn't, whereas the `is_elided` method seems to suggest it does—but it's beyond us to resolve that in this commit. For now, let the message say "implicit" for definiteness. This relates to #52041. --- src/librustc/hir/mod.rs | 4 ++++ src/librustc/lint/builtin.rs | 2 +- src/librustc/middle/resolve_lifetime.rs | 22 +++++++++---------- .../ui/in-band-lifetimes/elided-lifetimes.rs | 6 ++--- .../in-band-lifetimes/elided-lifetimes.stderr | 10 ++------- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index bae443bfc5824..666f6bc3835ea 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -272,6 +272,10 @@ impl LifetimeName { } } + pub fn is_implicit(&self) -> bool { + self == &LifetimeName::Implicit + } + fn is_static(&self) -> bool { self == &LifetimeName::Static } diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index a46b312062247..f43fbaa3268d8 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -252,7 +252,7 @@ declare_lint! { declare_lint! { pub ELIDED_LIFETIMES_IN_PATHS, Allow, - "hidden lifetime parameters are deprecated, try `Foo<'_>`" + "implicit lifetime parameters are deprecated" } declare_lint! { diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 369f65c214aa1..ae70978bfddba 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -2068,24 +2068,24 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { fn resolve_elided_lifetimes(&mut self, lifetime_refs: Vec<&'tcx hir::Lifetime>, - deprecated: bool) { + deprecate_implicit: bool) { if lifetime_refs.is_empty() { return; } let span = lifetime_refs[0].span; - let id = lifetime_refs[0].id; let mut late_depth = 0; let mut scope = self.scope; - if deprecated { - self.tcx - .struct_span_lint_node( - lint::builtin::ELIDED_LIFETIMES_IN_PATHS, - id, - span, - &format!("hidden lifetime parameters are deprecated, try `Foo<'_>`"), - ) - .emit(); + if deprecate_implicit && lifetime_refs[0].name.is_implicit() { + let mut err = self.tcx.struct_span_lint_node( + lint::builtin::ELIDED_LIFETIMES_IN_PATHS, + lifetime_refs[0].id, // FIXME: HirIdify #50928 + span, + &format!("implicit lifetime parameters in types are deprecated"), + ); + // FIXME: suggest `'_` (need to take into account whether angle-bracketed + // params already exist) + err.emit(); } let error = loop { match *scope { diff --git a/src/test/ui/in-band-lifetimes/elided-lifetimes.rs b/src/test/ui/in-band-lifetimes/elided-lifetimes.rs index ab66fed5533ca..3a79b9f827427 100644 --- a/src/test/ui/in-band-lifetimes/elided-lifetimes.rs +++ b/src/test/ui/in-band-lifetimes/elided-lifetimes.rs @@ -14,11 +14,9 @@ struct Foo<'a> { x: &'a u32 } fn foo(x: &Foo) { - //~^ ERROR: hidden lifetime parameters are deprecated, try `Foo<'_>` + //~^ ERROR: implicit lifetime parameters in types are deprecated } -fn bar(x: &Foo<'_>) { - //~^ ERROR: hidden lifetime parameters are deprecated, try `Foo<'_>` -} +fn bar(x: &Foo<'_>) {} fn main() {} diff --git a/src/test/ui/in-band-lifetimes/elided-lifetimes.stderr b/src/test/ui/in-band-lifetimes/elided-lifetimes.stderr index a85880c634abf..f72b4ac513fd8 100644 --- a/src/test/ui/in-band-lifetimes/elided-lifetimes.stderr +++ b/src/test/ui/in-band-lifetimes/elided-lifetimes.stderr @@ -1,4 +1,4 @@ -error: hidden lifetime parameters are deprecated, try `Foo<'_>` +error: implicit lifetime parameters in types are deprecated --> $DIR/elided-lifetimes.rs:16:12 | LL | fn foo(x: &Foo) { @@ -10,11 +10,5 @@ note: lint level defined here LL | #![deny(elided_lifetimes_in_paths)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ -error: hidden lifetime parameters are deprecated, try `Foo<'_>` - --> $DIR/elided-lifetimes.rs:20:16 - | -LL | fn bar(x: &Foo<'_>) { - | ^^ - -error: aborting due to 2 previous errors +error: aborting due to previous error