Skip to content
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

Propagate temporary lifetime extension into if and match. #121346

Merged
merged 5 commits into from Apr 10, 2024

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Feb 20, 2024

This PR makes this work:

let a = if true {
    ..;
    &temp() // used to error, but now gets lifetime extended
} else {
    ..;
    &temp() // used to error, but now gets lifetime extended
};

and

let a = match () {
    _ => {
        ..;
        &temp() // used to error, but now gets lifetime extended
    }
};

to make it consistent with:

let a = {
    ..;
    &temp() // lifetime is extended
};

This is one small part of the temporary lifetimes work.

This part is backwards compatible (so doesn't need be edition-gated), because all code affected by this change previously resulted in a hard error.

@m-ou-se m-ou-se added T-lang Relevant to the language team, which will review and decide on the PR/issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. labels Feb 20, 2024
@m-ou-se m-ou-se assigned m-ou-se and nikomatsakis and unassigned m-ou-se Feb 20, 2024
Copy link
Contributor

@danielhenrymantilla danielhenrymantilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yesss!! Such an underrated improvement 🤩

tests/ui/lifetimes/temporary-lifetime-extension.rs Outdated Show resolved Hide resolved
Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
@m-ou-se m-ou-se added the I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. label Feb 20, 2024
@joshtriplett
Copy link
Member

Based on discussion in the @rust-lang/lang meeting today:

@rfcbot merge

@rfcbot
Copy link

rfcbot commented Feb 21, 2024

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 Feb 21, 2024
@nikomatsakis
Copy link
Contributor

@rfcbot reviewed

I thought at first there were backwards compatibility concerns here. But as I dug in, I realized that there isn't. Looking at this example that Mara gave:

fn main() {
    drop(if true {
        ..;
        &temp(0) // used to error, but now gets lifetime extended
    } else {
        ..;
        &temp(1) // used to error, but now gets lifetime extended
    });
}

This winds up being a guaranteed error because, when we enter into an if expression, we always free the temporaries created within on exiting from the if. I remember making this change -- the idea was that wherever we have control flow divergence we will drop temporaries before we come back together -- and I believe the reason was because of the limitations of codegeneration way back then (long before we had MIR, for example). This makes sense because to support this pattern you need dynamic drop -- i.e., there is a temporary slot that will store temp(0) but it's only created conditionally -- and we didn't have that at the time. So I think it seemed obvious that we needed this restriction, but we don't anymore.

(Same for match etc)

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Feb 29, 2024
@rfcbot
Copy link

rfcbot commented Feb 29, 2024

🔔 This is now entering its final comment period, as per the review above. 🔔

@m-ou-se m-ou-se added relnotes Marks issues that should be documented in the release notes of the next release. and removed I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. labels Feb 29, 2024
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Mar 10, 2024
@rfcbot
Copy link

rfcbot commented Mar 10, 2024

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Mar 10, 2024
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Mar 14, 2024
@rust-lang rust-lang deleted a comment Mar 20, 2024
@rustbot rustbot assigned BoxyUwU and unassigned TaKO8Ki Apr 3, 2024
@BoxyUwU
Copy link
Member

BoxyUwU commented Apr 3, 2024

cool
r? @compiler-errors idk

@rustbot rustbot assigned compiler-errors and unassigned BoxyUwU Apr 3, 2024
@compiler-errors
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Apr 9, 2024

📌 Commit f4caa83 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 9, 2024
@bors
Copy link
Contributor

bors commented Apr 10, 2024

⌛ Testing commit f4caa83 with merge dd71663...

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2024
… r=compiler-errors

Propagate temporary lifetime extension into if and match.

This PR makes this work:

```rust
let a = if true {
    ..;
    &temp() // used to error, but now gets lifetime extended
} else {
    ..;
    &temp() // used to error, but now gets lifetime extended
};
```

and

```rust
let a = match () {
    _ => {
        ..;
        &temp() // used to error, but now gets lifetime extended
    }
};
```

to make it consistent with:

```rust
let a = {
    ..;
    &temp() // lifetime is extended
};
```

This is one small part of [the temporary lifetimes work](rust-lang/lang-team#253).

This part is backwards compatible (so doesn't need be edition-gated), because all code affected by this change previously resulted in a hard error.
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-aux failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@bors
Copy link
Contributor

bors commented Apr 10, 2024

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 10, 2024
@compiler-errors
Copy link
Member

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2024
@bors
Copy link
Contributor

bors commented Apr 10, 2024

⌛ Testing commit f4caa83 with merge b3bd705...

@bors
Copy link
Contributor

bors commented Apr 10, 2024

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing b3bd705 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 10, 2024
@bors bors merged commit b3bd705 into rust-lang:master Apr 10, 2024
12 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 10, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (b3bd705): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.1% [-5.1%, -5.1%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 675.28s -> 675.727s (0.07%)
Artifact size: 318.45 MiB -> 318.44 MiB (-0.00%)

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. finished-final-comment-period The final comment period is finished for this PR / Issue. merged-by-bors This PR was explicitly merged by bors. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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.

None yet