-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Temporary lifetime extension through tuple struct and tuple variant constructors #140593
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
base: master
Are you sure you want to change the base?
Conversation
r? @Nadrieril rustbot has assigned @Nadrieril. Use |
Does this count as 'I-lang-easy-decision'? I think it might be an easy decision. |
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.
please add an explicit test for
let indir = Some;
let x = indir(&temp);
53924ca
to
a9544f2
Compare
This seems great to me. As you said, we already do this for struct constructors in general, except tuple struct constructors. This is a consistency improvement and a usability improvement. @rfcbot merge |
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
@rfcbot reviewed This sounds right to me. We earlier decided #138458 partly on the basis that we see It does make me think, though, that maybe we also should have an attribute that authors can apply to function parameters to opt-in the corresponding arguments to temporary lifetime extension. I'm thinking here about how many Footnotes
|
Yup, I've suggested exactly that a few times, but @nikomatsakis had some arguments against that. (Mostly that you won't be able to tell at the call site, I think.) If we had that, and also a way to do an unsafe call without an unsafe block, I believe we wouldn't need Happy to implement a demo implementation of that so we can play with it and try out what the consequences are. |
We have more unlikely things than this as experiments. I'd say do it, and let's see how it feels. |
had some arguments against that. (Mostly that you won't be able to tell at the call site, I think.)
That argument already applies to this proposal though: you can't tell if it's a tuple struct constructor or a function call. So I feel we're crossing that line already with the proposal at hand.
|
In theory, in practice convention + lints mean that it is not ambiguous: constructors are upper case, functions are not. |
This makes temporary lifetime extension work for tuple struct and tuple variant constructors, such as
Some()
.Before:
After:
So, with this change, this works:
Until now, we did not extend through tuple struct/variant constructors (like
Some
), because they are function calls syntactically, and we do not want to extend the String lifetime in:However, it turns out to be very easy to distinguish between regular functions and constructors at the point where we do lifetime extension.
In practice, constructors nearly always use UpperCamelCase while regular functions use lower_snake_case, so it should still be easy to for a human programmer at the call site to see whether something qualifies for lifetime extension or not.
This needs a lang fcp.
More examples of what will work after this change: