-
Notifications
You must be signed in to change notification settings - Fork 14k
Temporary lifetime extension for blocks #146098
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
dianne
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
dianne:extending-blocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+498
−272
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
tests/ui/borrowck/format-args-temporary-scopes.e2021.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/format-args-temporary-scopes.rs:33:64 | ||
| | | ||
| LL | println!("{:?}{:?}", (), if true { std::convert::identity(&format!("")) } else { "" }); | ||
| | ----------------------------------^^^^^^^^^^^--------------- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| | borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
| = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0716`. |
39 changes: 26 additions & 13 deletions
39
tests/ui/borrowck/format-args-temporary-scopes.e2024.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,40 @@ | ||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/format-args-temporary-scopes.rs:13:25 | ||
| --> $DIR/format-args-temporary-scopes.rs:17:48 | ||
| | | ||
| LL | println!("{:?}", { &temp() }); | ||
| | ---^^^^^--- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| LL | println!("{:?}", { std::convert::identity(&temp()) }); | ||
| | --------------------------^^^^^^--- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| | borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
|
|
||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/format-args-temporary-scopes.rs:19:29 | ||
| --> $DIR/format-args-temporary-scopes.rs:24:52 | ||
| | | ||
| LL | println!("{:?}{:?}", { &temp() }, ()); | ||
| | ---^^^^^--- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| LL | println!("{:?}{:?}", { std::convert::identity(&temp()) }, ()); | ||
| | --------------------------^^^^^^--- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| | borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
|
|
||
| error: aborting due to 2 previous errors | ||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/format-args-temporary-scopes.rs:33:64 | ||
| | | ||
| LL | println!("{:?}{:?}", (), if true { std::convert::identity(&format!("")) } else { "" }); | ||
| | ------------------------^^^^^^^^^^^- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| | borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
| = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0716`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,38 @@ | ||
| //! Test for #145784 as it relates to format arguments: arguments to macros such as `println!` | ||
| //! should obey normal temporary scoping rules. | ||
| //@ revisions: e2021 e2024 | ||
| //@ [e2021] check-pass | ||
| //@ [e2021] edition: 2021 | ||
| //@ [e2024] edition: 2024 | ||
|
|
||
| fn temp() {} | ||
|
|
||
| fn main() { | ||
| // In Rust 2024, block tail expressions are temporary scopes, so the result of `temp()` is | ||
| // dropped after evaluating `&temp()`. | ||
| // In Rust 2024, block tail expressions are temporary scopes, but temporary lifetime extension | ||
| // rules apply: `&temp()` here is an extending borrow expression, so `temp()`'s lifetime is | ||
| // extended past the block. | ||
| println!("{:?}", { &temp() }); | ||
|
|
||
| // Arguments to function calls aren't extending expressions, so `temp()` is dropped at the end | ||
| // of the block in Rust 2024. | ||
| println!("{:?}", { std::convert::identity(&temp()) }); | ||
| //[e2024]~^ ERROR: temporary value dropped while borrowed [E0716] | ||
|
|
||
| // In Rust 1.89, `format_args!` extended the lifetime of all extending expressions in its | ||
| // arguments when provided with two or more arguments. This caused the result of `temp()` to | ||
| // outlive the result of the block, making this compile. | ||
| // In Rust 1.89, `format_args!` had different lifetime extension behavior dependent on how many | ||
| // formatting arguments it had (#145880), so let's test that too. | ||
| println!("{:?}{:?}", { &temp() }, ()); | ||
|
|
||
| println!("{:?}{:?}", { std::convert::identity(&temp()) }, ()); | ||
| //[e2024]~^ ERROR: temporary value dropped while borrowed [E0716] | ||
|
|
||
| // In real-world projects, this typically appeared in `if` expressions with a `&str` in one | ||
| // branch and a reference to a `String` temporary in the other. Since the consequent and `else` | ||
| // blocks of `if` expressions are temporary scopes in all editions, this affects Rust 2021 and | ||
| // earlier as well. | ||
| println!("{:?}{:?}", (), if true { &format!("") } else { "" }); | ||
|
|
||
| println!("{:?}{:?}", (), if true { std::convert::identity(&format!("")) } else { "" }); | ||
| //~^ ERROR: temporary value dropped while borrowed [E0716] | ||
|
|
||
| // This has likewise occurred with `match`, affecting all editions. | ||
| println!("{:?}{:?}", (), match true { true => &"" as &dyn std::fmt::Debug, false => &temp() }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| //! Test that `super let` bindings in `if` expressions' blocks have the same scope as the result | ||
| //! of the block. | ||
| //@ check-pass | ||
| #![feature(super_let)] | ||
|
|
||
| fn main() { | ||
| // For `super let` in an extending `if`, the binding `temp` should live in the scope of the | ||
| // outer `let` statement. | ||
| let x = if true { | ||
| super let temp = (); | ||
| &temp | ||
| } else { | ||
| super let temp = (); | ||
| &temp | ||
| }; | ||
| x; | ||
|
|
||
| // For `super let` in non-extending `if`, the binding `temp` should live in the temporary scope | ||
| // the `if` expression is in. | ||
| std::convert::identity(if true { | ||
| super let temp = (); | ||
| &temp | ||
| } else { | ||
| super let temp = (); | ||
| &temp | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| //! Demonstrates a case where `{ super let x = temp(); &x }` =/= `&temp()`, a variant of which is | ||
| //! observable on stable Rust via the `pin!` macro: `pin!($EXPR)` and `&mut $EXPR` may use different | ||
| //! scopes for their temporaries. | ||
|
|
||
| #![feature(super_let)] | ||
|
|
||
| use std::pin::pin; | ||
|
|
||
| fn temp() {} | ||
|
|
||
| fn main() { | ||
| // This is fine, since the temporary is extended to the end of the block: | ||
| let a = &*&temp(); | ||
| a; | ||
| let b = &mut *&mut temp(); | ||
| b; | ||
|
|
||
| // The temporary is dropped at the end of the outer `let` initializer: | ||
| let c = &*{ super let x = temp(); &x }; | ||
| //~^ ERROR `x` does not live long enough | ||
| c; | ||
| let d = &mut *pin!(temp()); | ||
| //~^ ERROR temporary value dropped while borrowed | ||
| d; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| error[E0597]: `x` does not live long enough | ||
| --> $DIR/super-let-projection-extension.rs:19:39 | ||
| | | ||
| LL | let c = &*{ super let x = temp(); &x }; | ||
| | - ^^ - `x` dropped here while still borrowed | ||
| | | | | ||
| | | borrowed value does not live long enough | ||
| | binding `x` declared here | ||
| LL | | ||
| LL | c; | ||
| | - borrow later used here | ||
|
|
||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/super-let-projection-extension.rs:22:19 | ||
| | | ||
| LL | let d = &mut *pin!(temp()); | ||
| | ^^^^^^^^^^^^- temporary value is freed at the end of this statement | ||
| | | | ||
| | creates a temporary value which is freed while still in use | ||
| LL | | ||
| LL | d; | ||
| | - borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
| = note: this error originates in the macro `pin` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| Some errors have detailed explanations: E0597, E0716. | ||
| For more information about an error, try `rustc --explain E0597`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Test drop order for destructuring assignments against | ||
| // other expressions they should be consistent with. | ||
| // | ||
| // See: | ||
| // | ||
| // - https://github.com/rust-lang/rust/pull/145838 | ||
| // | ||
| // Original author: TC | ||
| // Date: 2025-08-30 | ||
| //@ edition: 2024 | ||
| //@ run-pass | ||
|
|
||
| #![allow(unused_must_use)] | ||
|
|
||
| fn main() { | ||
| assert_drop_order(1..=3, |e| { | ||
| &({ &*&e.log(2) }, drop(e.log(1))); | ||
| // &({ &raw const *&e.log(2) }, drop(e.log(1))); | ||
| drop(e.log(3)); | ||
| }); | ||
| /* | ||
| assert_drop_order(1..=3, |e| { | ||
| { let _x; _x = &({ &raw const *&e.log(2) }, drop(e.log(1))); } | ||
| drop(e.log(3)); | ||
| }); | ||
| assert_drop_order(1..=3, |e| { | ||
| _ = &({ &raw const *&e.log(2) }, drop(e.log(1))); | ||
| drop(e.log(3)); | ||
| }); | ||
| assert_drop_order(1..=3, |e| { | ||
| { let _ = &({ &raw const *&e.log(2) }, drop(e.log(1))); } | ||
| drop(e.log(3)); | ||
| }); | ||
| assert_drop_order(1..=3, |e| { | ||
| let _x; let _y; | ||
| (_x, _y) = ({ &raw const *&e.log(2) }, drop(e.log(1))); | ||
| drop(e.log(3)); | ||
| }); | ||
| */ | ||
| } | ||
|
|
||
| // # Test scaffolding... | ||
|
|
||
| use core::cell::RefCell; | ||
|
|
||
| struct DropOrder(RefCell<Vec<u64>>); | ||
| struct LogDrop<'o>(&'o DropOrder, u64); | ||
|
|
||
| impl DropOrder { | ||
| fn log(&self, n: u64) -> LogDrop<'_> { | ||
| LogDrop(self, n) | ||
| } | ||
| } | ||
|
|
||
| impl<'o> Drop for LogDrop<'o> { | ||
| fn drop(&mut self) { | ||
| self.0 .0.borrow_mut().push(self.1); | ||
| } | ||
| } | ||
|
|
||
| #[track_caller] | ||
| fn assert_drop_order( | ||
| ex: impl IntoIterator<Item = u64>, | ||
| f: impl Fn(&DropOrder), | ||
| ) { | ||
| let order = DropOrder(RefCell::new(Vec::new())); | ||
| f(&order); | ||
| let order = order.0.into_inner(); | ||
| let expected: Vec<u64> = ex.into_iter().collect(); | ||
| assert_eq!(order, expected); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This appears to be inlined from
rust/library/core/src/slice/index.rs
Lines 432 to 439 in 82ae0ee
where previously the storage for the slice pointer returned by
get_offset_len_noubcheckwas dropped before the result of the bounds check1 and now it's dropped after, since it now lives past the execution of theifexpression.Footnotes
See here and here for why that bool lives until then: individual boolean conditions have their scopes overridden so they live to the end of the
ifexpression. I have to assume this is so they can each can have a singleStorageDead, rather than one in each branch following each condition's switch terminator. ↩