Skip to content

Commit 4401708

Browse files
committed
Auto merge of #96455 - dtolnay:writetmp, r=m-ou-se
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, "…", …)` | :rage: | :rage: | :smiley_cat: `write!(…, "…", $tmp)` | :rage: | :rage: | :smiley_cat: `writeln!($tmp, "…", …)` | :rage: | :rage: | :smiley_cat: `writeln!(…, "…", $tmp)` | :rage: | :rage: | :smiley_cat: `print!("…", $tmp)` | :rage: | :rage: | :smiley_cat: `println!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat: `eprint!("…", $tmp)` | :rage: | :rage: | :smiley_cat: `eprintln!("…", $tmp)` | :smiley_cat: | :rage: | :smiley_cat: `panic!("…", $tmp)` | :smiley_cat: | :smiley_cat: | :smiley_cat: 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` ```
2 parents 57aa640 + 3e9572a commit 4401708

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

core/src/macros/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,10 @@ macro_rules! r#try {
496496
#[stable(feature = "rust1", since = "1.0.0")]
497497
#[cfg_attr(not(test), rustc_diagnostic_item = "write_macro")]
498498
macro_rules! write {
499-
($dst:expr, $($arg:tt)*) => {
500-
$dst.write_fmt($crate::format_args!($($arg)*))
501-
};
499+
($dst:expr, $($arg:tt)*) => {{
500+
let result = $dst.write_fmt($crate::format_args!($($arg)*));
501+
result
502+
}};
502503
}
503504

504505
/// Write formatted data into a buffer, with a newline appended.
@@ -553,9 +554,10 @@ macro_rules! writeln {
553554
($dst:expr $(,)?) => {
554555
$crate::write!($dst, "\n")
555556
};
556-
($dst:expr, $($arg:tt)*) => {
557-
$dst.write_fmt($crate::format_args_nl!($($arg)*))
558-
};
557+
($dst:expr, $($arg:tt)*) => {{
558+
let result = $dst.write_fmt($crate::format_args_nl!($($arg)*));
559+
result
560+
}};
559561
}
560562

561563
/// Indicates unreachable code.

std/src/macros.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ macro_rules! panic {
6262
#[cfg_attr(not(test), rustc_diagnostic_item = "print_macro")]
6363
#[allow_internal_unstable(print_internals)]
6464
macro_rules! print {
65-
($($arg:tt)*) => {
66-
$crate::io::_print($crate::format_args!($($arg)*))
67-
};
65+
($($arg:tt)*) => {{
66+
$crate::io::_print($crate::format_args!($($arg)*));
67+
}};
6868
}
6969

7070
/// Prints to the standard output, with a newline.
@@ -133,9 +133,9 @@ macro_rules! println {
133133
#[cfg_attr(not(test), rustc_diagnostic_item = "eprint_macro")]
134134
#[allow_internal_unstable(print_internals)]
135135
macro_rules! eprint {
136-
($($arg:tt)*) => {
137-
$crate::io::_eprint($crate::format_args!($($arg)*))
138-
};
136+
($($arg:tt)*) => {{
137+
$crate::io::_eprint($crate::format_args!($($arg)*));
138+
}};
139139
}
140140

141141
/// Prints to the standard error, with a newline.

0 commit comments

Comments
 (0)