-
Notifications
You must be signed in to change notification settings - Fork 13.8k
elided lifetime #46254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
elided lifetime #46254
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -737,7 +737,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { | |||||||||||||||
|
||||||||||||||||
fn visit_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime) { | ||||||||||||||||
if lifetime_ref.is_elided() { | ||||||||||||||||
self.resolve_elided_lifetimes(slice::from_ref(lifetime_ref)); | ||||||||||||||||
self.resolve_elided_lifetimes(slice::from_ref(lifetime_ref), false); | ||||||||||||||||
return; | ||||||||||||||||
} | ||||||||||||||||
if lifetime_ref.is_static() { | ||||||||||||||||
|
@@ -1444,7 +1444,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if params.lifetimes.iter().all(|l| l.is_elided()) { | ||||||||||||||||
self.resolve_elided_lifetimes(¶ms.lifetimes); | ||||||||||||||||
self.resolve_elided_lifetimes(¶ms.lifetimes, true); | ||||||||||||||||
} else { | ||||||||||||||||
for l in ¶ms.lifetimes { | ||||||||||||||||
self.visit_lifetime(l); | ||||||||||||||||
|
@@ -1803,14 +1803,24 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { | |||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
fn resolve_elided_lifetimes(&mut self, lifetime_refs: &'tcx [hir::Lifetime]) { | ||||||||||||||||
fn resolve_elided_lifetimes(&mut self, lifetime_refs: &'tcx [hir::Lifetime], deprecated: bool) { | ||||||||||||||||
|
this.tcx | |
.struct_span_lint_node(lint::builtin::SINGLE_USE_LIFETIME, | |
id, | |
span, | |
&format!("lifetime name `{}` only used once", | |
hir_lifetime.name.name())) | |
.emit(); |
I think you should be able to do something similar from this function here when deprecated
is true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Thanks
I have followed that code already was wondering if i had to create the LifetimeContext
object just like what is being done in the function. I guess i have to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don' think you have to do that, no. I am confused. You should have a LifetimeContext
available.
Basically, that function does some things in addition to creating the lint -- that is why it makes the new LifetimeContext
struct -- you are not doing those things. The only thing you should need is the call to struct_span_lint_node
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have followed that code already
What do you mean by "followed" here? I don't see (in the PR) any calls to struct_span_lint_node
, maybe you forgot to push a commit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should not have to create a LifetimeContext
, no. That function does a few other things in addition to issuing the lint -- in particular, it pushes a "subscope" -- and that is why it creates a LifetimeContext
. You can just use self
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by "followed" here? I don't see (in the PR) any calls to struct_span_lint_node, maybe you forgot to push a commit?
Ah I meant the code you had mentioned earlier.
My question is: do I have access to this.tcx directly? I thought I didn't
Edit: I have solved it but running into an issue of another lint blocking it. Will update this in a while
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
#![allow(warnings)] | ||
#![allow(unused_variables, dead_code, unused, bad_style)] | ||
#![deny(elided_lifetime_in_path)] | ||
|
||
struct Foo<'a> { x: &'a u32 } | ||
fn foo(x: &Foo) { | ||
//~^ ERROR: hidden lifetime parameters are deprecated, try `Foo<'_>` | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: hidden lifetime parameters are deprecated, try `Foo<'_>` | ||
--> $DIR/ellided-lifetimes.rs:15:12 | ||
| | ||
15 | fn foo(x: &Foo) { | ||
| ^^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/ellided-lifetimes.rs:12:9 | ||
| | ||
12 | #![deny(elided_lifetime_in_path)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change the message to:
elided lifetime parameters from structs, enums, or such types are deprecated; use
'_
instead