-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Suggest replacing named lifetime with 'static
#51513
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
``` warning: unnecessary lifetime parameter `'a` --> file.rs:11:14 | 11 | fn foo<'c, 'a: 'static, 'b, 'd>(_x: &'a str) -> &'a str { | ^^^^^^^^^^^ help: you can use the `'static` lifetime directly, in place of `'a` | 11 | fn foo<'c, 'b, 'd>(_x: &'static str) -> &'static str { | -- ^^^^^^^ ^^^^^^^ warning: unnecessary lifetime parameter `'a` --> file.rs:14:9 | 14 | fn bar<'a: 'static>(_x: &'a str) -> &'a str { | ^^^^^^^^^^^ help: you can use the `'static` lifetime directly, in place of `'a` | 14 | fn bar(_x: &'static str) -> &'static str { | -- ^^^^^^^ ^^^^^^^ ```
'static
[wip]'static
This comment has been minimized.
This comment has been minimized.
Ping from triage @nikomatsakis! This PR needs your review. |
Ping. |
Overall, this seems like a bit more code than I would have expected; it feels like there should be a lighterweight way to detect this condition. |
Well, I guess that making the edit is part of the hard part... |
This code can probably be cleaned quite a bit, but wanted to get some feedback on wether this is worthwhile to begin with. This doesn't fix #36179, but by providing a suggestion for lifetimes the risk of someone writing |
@@ -1662,6 +1669,137 @@ pub struct Ty { | |||
pub hir_id: HirId, | |||
} | |||
|
|||
impl Ty { | |||
pub fn lifetimes(&self) -> Vec<Lifetime> { |
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.
Can't we use a visitor for this?
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.
Also, what is this used for? It seems like it doesn't handle shadowing (which, I guess, we disallow at present, so maybe that's ok).
(I was thinking of types like for<'a> fn(&'a u32)
, which mention 'a
, but not the same 'a
as might be in scope outside... but as I said at present we do disallow such shadowing)
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.
It's only used to gather all the lifetime spans in the argument and return tys that match the name being checked.
I see. I think the output looks good. I'm not sure if there is a more light-weight way to get it. |
I made one suggestion that would help to remove a lot of the boilerplate (using a visitor) |
CC #44506 (same mechanism can be used to suggest the proposed output). |
☔ The latest upstream changes (presumably #48149) made this pull request unmergeable. Please resolve the merge conflicts. |
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.
OK, @estebank -- now that I understand better what you were going for, I think this looks pretty nice. It seems like we should (A) rewrite to use a visitor, (B) make the vector construction lazy, and (C) add a helper fn, but otherwise this seems like a nice POC. (We could/should fix shadowing too but that is less urgent since it is currently illegal anyway)
@@ -1441,6 +1446,18 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { | |||
.collect(); | |||
|
|||
let next_early_index = index + generics.ty_params().count() as u32; | |||
let mut arg_lifetimes = vec![]; | |||
for input in &decl.inputs { |
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.
This vector is only used in the case of lint/error, right? I'm not sure how I feel about building it eagerly -- maybe we can convert this into a closure or a trait or something?
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.
Makes sense, I'll see what I can do about it.
self.resolve_lifetime_ref(bound); | ||
); | ||
|
||
// all the use spans for this lifetime in arguments to be replaced |
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.
we should extract this into a helper fn, but the basic idea seems pretty good 💯
As an example where shadowing could be relevant though: fn foo<'a: 'static>(x: for<'a> fn(&'a u32)) { } |
The output for
is
|
hmm, that seems suboptimal, do you agree? I think it'd be pretty easy to fix -- in the |
Agree with your comments. I tried rebasing to see how hard it would be to include the feedback and the codebase diverged significantly. I'll get back to this later. |
@estebank should we close the PR in the mean time? |
Closing until I get back to this. |
cc #36179