Skip to content

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented May 2, 2025

This makes temporary lifetime extension work for tuple struct and tuple variant constructors, such as Some().

Before:

let a = &temp(); // Extended
let a = Some(&temp()); // Not extended :(
let a = Some { 0: &temp() }; // Extended

After:

let a = &temp(); // Extended
let a = Some(&temp()); // Extended
let a = Some { 0: &temp() }; // Extended

So, with this change, this works:

let a = Some(&String::from("hello")); // New: String lifetime now extended!

println!("{a:?}");

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:

let a = some_function(&String::from("hello")); // String not extended!

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:

let x = Person {
    name: "Ferris",
    job: Some(&Job { // `Job` now extended!
        title: "Chief Rustacean",
        organisation: "Acme Ltd.",
    }),
};

dbg!(x);
let file = if use_stdout {
    None
} else {
    Some(&File::create("asdf")?) // `File` now extended!
};

set_logger(file);
use std::path::Component;

let c = Component::Normal(&OsString::from(format!("test-{num}"))); // OsString now extended!

assert_eq!(path.components.first().unwrap(), c);

@m-ou-se m-ou-se added T-lang Relevant to the language team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. I-lang-nominated Nominated for discussion during a lang team meeting. labels May 2, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 2, 2025

r? @Nadrieril

rustbot has assigned @Nadrieril.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 2, 2025
@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels May 2, 2025
@m-ou-se m-ou-se removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-tidy Area: The tidy tool labels May 2, 2025
@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels May 2, 2025
@m-ou-se m-ou-se removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-tidy Area: The tidy tool labels May 2, 2025
@m-ou-se
Copy link
Member Author

m-ou-se commented May 2, 2025

Does this count as 'I-lang-easy-decision'? I think it might be an easy decision.

@m-ou-se m-ou-se added the I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination label May 2, 2025
Copy link
Contributor

@lcnr lcnr left a 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);

@m-ou-se m-ou-se force-pushed the some-temp branch 2 times, most recently from 53924ca to a9544f2 Compare May 3, 2025 07:02
@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels May 3, 2025
@m-ou-se m-ou-se removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 3, 2025
@m-ou-se m-ou-se added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed A-tidy Area: The tidy tool labels May 3, 2025
@joshtriplett
Copy link
Member

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

@rfcbot
Copy link
Collaborator

rfcbot commented May 3, 2025

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.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels May 3, 2025
@traviscross
Copy link
Contributor

@rfcbot reviewed

This sounds right to me. We earlier decided #138458 partly on the basis that we see Some(x) as simple syntactic sugar for Some { 0: x }1, and this I think goes along well with that.

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 new constructors that mostly just pass through their inputs into the output ADT really want this same behavior for the same reasons we want it for Some(..). But we can take that up another day.

Footnotes

  1. Though there, it was in pattern position.

@m-ou-se
Copy link
Member Author

m-ou-se commented May 3, 2025

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.

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 super let at all for pin!() and let f = format_args!(..);.

Happy to implement a demo implementation of that so we can play with it and try out what the consequences are.

@traviscross
Copy link
Contributor

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.

@RalfJung
Copy link
Member

RalfJung commented May 3, 2025 via email

@lcnr
Copy link
Contributor

lcnr commented May 3, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination I-lang-nominated Nominated for discussion during a lang team meeting. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants