-
Notifications
You must be signed in to change notification settings - Fork 13.3k
RFC: Give anonymous lifetimes names based on their parameter #12072
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
Comments
This (or a variation of it) was recently proposed on rust-dev as well. I see the appeal, but I am wary of introducing a kind of "second system" of lifetime parameters. I am particularly wary of the ambiguity around shadowing that you raise at the end. |
@nikomatsakis I would love to do some sort of survey of the existing codebase to see a) how many existing functions could benefit from this, and b) how many would run afoul of the shadowing issue. My expectation right now is that a) is "a fair number", and b) is "almost none". |
(That said, I do think there is room for improvement in our current scheme.) |
I crave data, yes. |
+1 on this proposal from me, obviously. Though in I think that Rust is very much in need of reduction of verbosity around usage of lifetimes, so I'll settle for a little magic. |
@vadimcn |
@kballard: Yes, but it's a bit subtle. The natural inclination would be to say " |
This has been partially addressed by lifetime elision but if such a change is still desired it should be proposed as an RFC. Can we close this? |
Closing. |
fix rust-lang#11973 associated type is unresolved
When a function has parameters that are of type
&T
, they are given an anonymous lifetime. I propose that they should be given a lifetime named the same as the parameter, except in such cases where this shadows an already-declared lifetime.This allows for less typing in common cases. For example,
can be simplified to
I also propose that lifetimes be allowed to use keywords as lifetime names again, so that
'self
is valid, as this allowsMy only concern with this proposal is potential confusion if a defaulted lifetime name would have shadowed a declared name (and therefore the lifetime remains anonymous). I think this can be clarified with an appropriate note on the error. If I have something like
and I get a lifetime error, the compile could put a note telling me that the parameter
a
has an anonymous lifetime because the lifetime'a
was already in-scope. This note would only show up when a lifetime error occurs relating to a declared lifetime that is blocking a defaulted lifetime name.The text was updated successfully, but these errors were encountered: