Skip to content

Commit 48efd30

Browse files
authored
Rollup merge of rust-lang#99689 - dtolnay:write, r=Mark-Simulacrum
Revert `write!` and `writeln!` to late drop temporaries Closes (on master, but not on beta) rust-lang#99684 by reverting the `write!` and `writeln!` parts of rust-lang#96455. argument position | before<br>rust-lang#94868 | after<br>rust-lang#94868 | after<br>rust-lang#96455 | after<br>this PR | desired<br>(unimplementable) --- |:---:|:---:|:---:|:---:|:---: `write!($tmp, "…", …)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | **⸺late** `write!(…, "…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | *early⸺* `writeln!($tmp, "…", …)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | **⸺late** `writeln!(…, "…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | **⸺late** | *early⸺* `print!("…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | *early⸺* | *early⸺* `println!("…", $tmp)` | *early⸺* | **⸺late** | *early⸺* | *early⸺* | *early⸺* `eprint!("…", $tmp)` | **⸺late** | **⸺late** | *early⸺* | *early⸺* | *early⸺* `eprintln!("…", $tmp)` | *early⸺* | **⸺late**| *early⸺* | *early⸺* | *early⸺* `panic!("…", $tmp)` | *early⸺* | *early⸺* | *early⸺* | *early⸺* | *early⸺* "Late drop" refers to dropping temporaries at the nearest semicolon **outside** of the macro invocation. "Early drop" refers to dropping temporaries inside of the macro invocation.
2 parents a479cab + ffab6bf commit 48efd30

File tree

5 files changed

+136
-24
lines changed

5 files changed

+136
-24
lines changed

library/core/src/macros/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,9 @@ 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-
let result = $dst.write_fmt($crate::format_args!($($arg)*));
501-
result
502-
}};
499+
($dst:expr, $($arg:tt)*) => {
500+
$dst.write_fmt($crate::format_args!($($arg)*))
501+
};
503502
}
504503

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

563561
/// Indicates unreachable code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// check-pass
2+
// edition:2021
3+
4+
use std::fmt::{self, Display};
5+
use std::future::Future;
6+
use std::io;
7+
use std::pin::Pin;
8+
use std::task::{Context, Poll};
9+
10+
struct AsyncStdout;
11+
12+
impl AsyncStdout {
13+
fn write_fmt<'a>(&'a mut self, _args: fmt::Arguments) -> WriteFmtFuture<'a, Self>
14+
where
15+
Self: Unpin,
16+
{
17+
WriteFmtFuture(self)
18+
}
19+
}
20+
21+
struct WriteFmtFuture<'a, T>(&'a mut T);
22+
23+
impl<'a, T> Future for WriteFmtFuture<'a, T> {
24+
type Output = io::Result<()>;
25+
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
26+
unimplemented!()
27+
}
28+
}
29+
30+
async fn async_main() {
31+
let _write = write!(&mut AsyncStdout, "...").await;
32+
let _writeln = writeln!(&mut AsyncStdout, "...").await;
33+
}
34+
35+
fn main() {
36+
let _ = async_main;
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// check-fail
2+
3+
use std::fmt::{self, Display};
4+
5+
struct Mutex;
6+
7+
impl Mutex {
8+
fn lock(&self) -> MutexGuard {
9+
MutexGuard(self)
10+
}
11+
}
12+
13+
struct MutexGuard<'a>(&'a Mutex);
14+
15+
impl<'a> Drop for MutexGuard<'a> {
16+
fn drop(&mut self) {
17+
// Empty but this is a necessary part of the repro. Otherwise borrow
18+
// checker is fine with 'a dangling at the time that MutexGuard goes out
19+
// of scope.
20+
}
21+
}
22+
23+
struct Out;
24+
25+
impl Out {
26+
fn write_fmt(&self, _args: fmt::Arguments) {}
27+
}
28+
29+
impl<'a> Display for MutexGuard<'a> {
30+
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
31+
Ok(())
32+
}
33+
}
34+
35+
fn main() {
36+
// FIXME(dtolnay): We actually want both of these to work. I think it's
37+
// sadly unimplementable today though.
38+
39+
let _write = {
40+
let mutex = Mutex;
41+
write!(Out, "{}", mutex.lock()) /* no semicolon */
42+
//~^ ERROR `mutex` does not live long enough
43+
};
44+
45+
let _writeln = {
46+
let mutex = Mutex;
47+
writeln!(Out, "{}", mutex.lock()) /* no semicolon */
48+
//~^ ERROR `mutex` does not live long enough
49+
};
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0597]: `mutex` does not live long enough
2+
--> $DIR/format-args-temporaries-in-write.rs:41:27
3+
|
4+
LL | write!(Out, "{}", mutex.lock()) /* no semicolon */
5+
| ^^^^^^^^^^^^
6+
| |
7+
| borrowed value does not live long enough
8+
| a temporary with access to the borrow is created here ...
9+
LL |
10+
LL | };
11+
| -- ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard`
12+
| |
13+
| `mutex` dropped here while still borrowed
14+
|
15+
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
16+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
17+
|
18+
LL | $dst.write_fmt($crate::format_args!($($arg)*));
19+
| +
20+
21+
error[E0597]: `mutex` does not live long enough
22+
--> $DIR/format-args-temporaries-in-write.rs:47:29
23+
|
24+
LL | writeln!(Out, "{}", mutex.lock()) /* no semicolon */
25+
| ^^^^^^^^^^^^
26+
| |
27+
| borrowed value does not live long enough
28+
| a temporary with access to the borrow is created here ...
29+
LL |
30+
LL | };
31+
| -- ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard`
32+
| |
33+
| `mutex` dropped here while still borrowed
34+
|
35+
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
36+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
37+
|
38+
LL | $dst.write_fmt($crate::format_args_nl!($($arg)*));
39+
| +
40+
41+
error: aborting due to 2 previous errors
42+
43+
For more information about this error, try `rustc --explain E0597`.

src/test/ui/macros/format-args-temporaries.rs

-16
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,13 @@ impl<'a> Drop for MutexGuard<'a> {
2020
}
2121
}
2222

23-
impl<'a> MutexGuard<'a> {
24-
fn write_fmt(&self, _args: fmt::Arguments) {}
25-
}
26-
2723
impl<'a> Display for MutexGuard<'a> {
2824
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
2925
Ok(())
3026
}
3127
}
3228

3329
fn main() {
34-
let _write = {
35-
let out = Mutex;
36-
let mutex = Mutex;
37-
write!(out.lock(), "{}", mutex.lock()) /* no semicolon */
38-
};
39-
40-
let _writeln = {
41-
let out = Mutex;
42-
let mutex = Mutex;
43-
writeln!(out.lock(), "{}", mutex.lock()) /* no semicolon */
44-
};
45-
4630
let _print = {
4731
let mutex = Mutex;
4832
print!("{}", mutex.lock()) /* no semicolon */

0 commit comments

Comments
 (0)