c186f7c079
Make write/print macros eagerly drop temporaries This PR fixes the 2 regressions in #96434 (`println` and `eprintln`) and changes all the other similar macros (`write`, `writeln`, `print`, `eprint`) to match the old pre-#94868 behavior of `println` and `eprintln`. argument position | before #94868 | after #94868 | after this PR --- |:---:|:---:|:---: `write!($tmp, "…", …)` | 😡 | 😡 | 😺 `write!(…, "…", $tmp)` | 😡 | 😡 | 😺 `writeln!($tmp, "…", …)` | 😡 | 😡 | 😺 `writeln!(…, "…", $tmp)` | 😡 | 😡 | 😺 `print!("…", $tmp)` | 😡 | 😡 | 😺 `println!("…", $tmp)` | 😺 | 😡 | 😺 `eprint!("…", $tmp)` | 😡 | 😡 | 😺 `eprintln!("…", $tmp)` | 😺 | 😡 | 😺 `panic!("…", $tmp)` | 😺 | 😺 | 😺 Example of code that is affected by this change: ```rust use std::sync::Mutex; fn main() { let mutex = Mutex::new(0); print!("{}", mutex.lock().unwrap()) /* no semicolon */ } ``` You can see several real-world examples like this in the Crater links at the top of #96434. This code failed to compile prior to this PR as follows, but works after this PR. ```console error[E0597]: `mutex` does not live long enough --> src/main.rs:5:18 | 5 | print!("{}", mutex.lock().unwrap()) /* no semicolon */ | ^^^^^^^^^^^^--------- | | | borrowed value does not live long enough | a temporary with access to the borrow is created here ... 6 | } | - | | | `mutex` dropped here while still borrowed | ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard` ``` |
||
---|---|---|
.. | ||
alloc | ||
backtrace@b02ed04a7e | ||
core | ||
panic_abort | ||
panic_unwind | ||
portable-simd | ||
proc_macro | ||
profiler_builtins | ||
rtstartup | ||
rustc-std-workspace-alloc | ||
rustc-std-workspace-core | ||
rustc-std-workspace-std | ||
std | ||
stdarch@28335054b1 | ||
test | ||
unwind |