Skip to content

Commit 6f10c0a

Browse files
committed
Make write/print macros eagerly drop temporaries
1 parent 5765819 commit 6f10c0a

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

library/core/src/macros/mod.rs

+8-6
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.

library/std/src/macros.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ macro_rules! panic {
6060
#[cfg_attr(not(test), rustc_diagnostic_item = "print_macro")]
6161
#[allow_internal_unstable(print_internals)]
6262
macro_rules! print {
63-
($($arg:tt)*) => {
64-
$crate::io::_print($crate::format_args!($($arg)*))
65-
};
63+
($($arg:tt)*) => {{
64+
$crate::io::_print($crate::format_args!($($arg)*));
65+
}};
6666
}
6767

6868
/// Prints to the standard output, with a newline.
@@ -99,9 +99,9 @@ macro_rules! println {
9999
() => {
100100
$crate::print!("\n")
101101
};
102-
($($arg:tt)*) => {
103-
$crate::io::_print($crate::format_args_nl!($($arg)*))
104-
};
102+
($($arg:tt)*) => {{
103+
$crate::io::_print($crate::format_args_nl!($($arg)*));
104+
}};
105105
}
106106

107107
/// Prints to the standard error.
@@ -130,9 +130,9 @@ macro_rules! println {
130130
#[cfg_attr(not(test), rustc_diagnostic_item = "eprint_macro")]
131131
#[allow_internal_unstable(print_internals)]
132132
macro_rules! eprint {
133-
($($arg:tt)*) => {
134-
$crate::io::_eprint($crate::format_args!($($arg)*))
135-
};
133+
($($arg:tt)*) => {{
134+
$crate::io::_eprint($crate::format_args!($($arg)*));
135+
}};
136136
}
137137

138138
/// Prints to the standard error, with a newline.
@@ -164,9 +164,9 @@ macro_rules! eprintln {
164164
() => {
165165
$crate::eprint!("\n")
166166
};
167-
($($arg:tt)*) => {
168-
$crate::io::_eprint($crate::format_args_nl!($($arg)*))
169-
};
167+
($($arg:tt)*) => {{
168+
$crate::io::_eprint($crate::format_args_nl!($($arg)*));
169+
}};
170170
}
171171

172172
/// Prints and returns the value of a given expression for quick and dirty

0 commit comments

Comments
 (0)