Skip to content

Add new temporary lifetime extension rule #1813

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 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/destructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,10 @@ expression which is one of the following:
expression], [braced struct][struct expression], or [tuple][tuple expression]
expression.
* The final expression of any extending [block expression].
* The arguments to [tuple struct][tuple struct] and [tuple variant][tuple variant] constructors.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* The arguments to [tuple struct][tuple struct] and [tuple variant][tuple variant] constructors.
* The arguments to [tuple struct] and [tuple variant] constructors.

Copy link
Contributor

Choose a reason for hiding this comment

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

We were uncertain about two things here:

  • What is an "extending ... expression"? Does that mean it is a subexpression of something else that is extending?
  • Related to that, do these need the adjective "extending"? That is, "extending tuple struct" and "extending tuple variant"? If so, would it make sense to merge this with the third bullet point, or at least to move this bullet point to be next to it?

Copy link
Contributor

@ehuss ehuss May 8, 2025

Choose a reason for hiding this comment

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

I think to answer my own question, I do believe the "extending" part is relevant. My understanding is that:

  • Some{0: &temp()} — ok
  • (Some{0: &temp()},) — ok
  • foo(Some{0: &temp()},) — ERROR

because in function call argument position it is not an "extending expression". The same seems to be true for tuple constructors in rust-lang/rust#140593:

  • Some(&temp()) — ok
  • (Some(&temp()),) — ok
  • foo(Some(&temp()),) — ERROR


So the borrow expressions in `&mut 0`, `(&1, &mut 2)`, and `Some { 0: &mut 3 }`
are all extending expressions. The borrows in `&0 + &1` and `Some(&mut 0)` are
not: the latter is syntactically a function call expression.
So the borrow expressions in `&mut 0`, `(&1, &mut 2)`, and `Some(&mut 3)`
are all extending expressions. The borrows in `&0 + &1` and `f(&mut 0)` are not.

The operand of any extending borrow expression has its temporary scope
extended.
Expand Down Expand Up @@ -467,6 +467,8 @@ There is one additional case to be aware of: when a panic reaches a [non-unwindi
[struct pattern]: patterns.md#struct-patterns
[tuple pattern]: patterns.md#tuple-patterns
[tuple struct pattern]: patterns.md#tuple-struct-patterns
[tuple struct]: types/struct.md#r-type.struct.tuple
[tuple variant]: types/enum.md#r-type.enum.declaration
Comment on lines +470 to +471
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's switch these out for the r identifiers.


[array expression]: expressions/array-expr.md#array-expressions
[block expression]: expressions/block-expr.md
Expand Down