Rollup merge of #99689 - dtolnay:write, r=Mark-Simulacrum
Revert `write!` and `writeln!` to late drop temporaries Closes (on master, but not on beta) #99684 by reverting the `write!` and `writeln!` parts of #96455. argument position | before<br>#94868 | after<br>#94868 | after<br>#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.
This commit is contained in:
commit
48efd30c9d
5 changed files with 136 additions and 24 deletions
|
@ -496,10 +496,9 @@ macro_rules! r#try {
|
|||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[cfg_attr(not(test), rustc_diagnostic_item = "write_macro")]
|
||||
macro_rules! write {
|
||||
($dst:expr, $($arg:tt)*) => {{
|
||||
let result = $dst.write_fmt($crate::format_args!($($arg)*));
|
||||
result
|
||||
}};
|
||||
($dst:expr, $($arg:tt)*) => {
|
||||
$dst.write_fmt($crate::format_args!($($arg)*))
|
||||
};
|
||||
}
|
||||
|
||||
/// Write formatted data into a buffer, with a newline appended.
|
||||
|
@ -554,10 +553,9 @@ macro_rules! writeln {
|
|||
($dst:expr $(,)?) => {
|
||||
$crate::write!($dst, "\n")
|
||||
};
|
||||
($dst:expr, $($arg:tt)*) => {{
|
||||
let result = $dst.write_fmt($crate::format_args_nl!($($arg)*));
|
||||
result
|
||||
}};
|
||||
($dst:expr, $($arg:tt)*) => {
|
||||
$dst.write_fmt($crate::format_args_nl!($($arg)*))
|
||||
};
|
||||
}
|
||||
|
||||
/// Indicates unreachable code.
|
||||
|
|
37
src/test/ui/macros/format-args-temporaries-async.rs
Normal file
37
src/test/ui/macros/format-args-temporaries-async.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
// check-pass
|
||||
// edition:2021
|
||||
|
||||
use std::fmt::{self, Display};
|
||||
use std::future::Future;
|
||||
use std::io;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
struct AsyncStdout;
|
||||
|
||||
impl AsyncStdout {
|
||||
fn write_fmt<'a>(&'a mut self, _args: fmt::Arguments) -> WriteFmtFuture<'a, Self>
|
||||
where
|
||||
Self: Unpin,
|
||||
{
|
||||
WriteFmtFuture(self)
|
||||
}
|
||||
}
|
||||
|
||||
struct WriteFmtFuture<'a, T>(&'a mut T);
|
||||
|
||||
impl<'a, T> Future for WriteFmtFuture<'a, T> {
|
||||
type Output = io::Result<()>;
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
async fn async_main() {
|
||||
let _write = write!(&mut AsyncStdout, "...").await;
|
||||
let _writeln = writeln!(&mut AsyncStdout, "...").await;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = async_main;
|
||||
}
|
50
src/test/ui/macros/format-args-temporaries-in-write.rs
Normal file
50
src/test/ui/macros/format-args-temporaries-in-write.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
// check-fail
|
||||
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
struct Mutex;
|
||||
|
||||
impl Mutex {
|
||||
fn lock(&self) -> MutexGuard {
|
||||
MutexGuard(self)
|
||||
}
|
||||
}
|
||||
|
||||
struct MutexGuard<'a>(&'a Mutex);
|
||||
|
||||
impl<'a> Drop for MutexGuard<'a> {
|
||||
fn drop(&mut self) {
|
||||
// Empty but this is a necessary part of the repro. Otherwise borrow
|
||||
// checker is fine with 'a dangling at the time that MutexGuard goes out
|
||||
// of scope.
|
||||
}
|
||||
}
|
||||
|
||||
struct Out;
|
||||
|
||||
impl Out {
|
||||
fn write_fmt(&self, _args: fmt::Arguments) {}
|
||||
}
|
||||
|
||||
impl<'a> Display for MutexGuard<'a> {
|
||||
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// FIXME(dtolnay): We actually want both of these to work. I think it's
|
||||
// sadly unimplementable today though.
|
||||
|
||||
let _write = {
|
||||
let mutex = Mutex;
|
||||
write!(Out, "{}", mutex.lock()) /* no semicolon */
|
||||
//~^ ERROR `mutex` does not live long enough
|
||||
};
|
||||
|
||||
let _writeln = {
|
||||
let mutex = Mutex;
|
||||
writeln!(Out, "{}", mutex.lock()) /* no semicolon */
|
||||
//~^ ERROR `mutex` does not live long enough
|
||||
};
|
||||
}
|
43
src/test/ui/macros/format-args-temporaries-in-write.stderr
Normal file
43
src/test/ui/macros/format-args-temporaries-in-write.stderr
Normal file
|
@ -0,0 +1,43 @@
|
|||
error[E0597]: `mutex` does not live long enough
|
||||
--> $DIR/format-args-temporaries-in-write.rs:41:27
|
||||
|
|
||||
LL | write!(Out, "{}", mutex.lock()) /* no semicolon */
|
||||
| ^^^^^^^^^^^^
|
||||
| |
|
||||
| borrowed value does not live long enough
|
||||
| a temporary with access to the borrow is created here ...
|
||||
LL |
|
||||
LL | };
|
||||
| -- ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard`
|
||||
| |
|
||||
| `mutex` dropped here while still borrowed
|
||||
|
|
||||
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
|
||||
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
|
||||
|
|
||||
LL | $dst.write_fmt($crate::format_args!($($arg)*));
|
||||
| +
|
||||
|
||||
error[E0597]: `mutex` does not live long enough
|
||||
--> $DIR/format-args-temporaries-in-write.rs:47:29
|
||||
|
|
||||
LL | writeln!(Out, "{}", mutex.lock()) /* no semicolon */
|
||||
| ^^^^^^^^^^^^
|
||||
| |
|
||||
| borrowed value does not live long enough
|
||||
| a temporary with access to the borrow is created here ...
|
||||
LL |
|
||||
LL | };
|
||||
| -- ... and the borrow might be used here, when that temporary is dropped and runs the `Drop` code for type `MutexGuard`
|
||||
| |
|
||||
| `mutex` dropped here while still borrowed
|
||||
|
|
||||
help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped
|
||||
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
|
||||
|
|
||||
LL | $dst.write_fmt($crate::format_args_nl!($($arg)*));
|
||||
| +
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0597`.
|
|
@ -20,10 +20,6 @@ impl<'a> Drop for MutexGuard<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> MutexGuard<'a> {
|
||||
fn write_fmt(&self, _args: fmt::Arguments) {}
|
||||
}
|
||||
|
||||
impl<'a> Display for MutexGuard<'a> {
|
||||
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
Ok(())
|
||||
|
@ -31,18 +27,6 @@ impl<'a> Display for MutexGuard<'a> {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let _write = {
|
||||
let out = Mutex;
|
||||
let mutex = Mutex;
|
||||
write!(out.lock(), "{}", mutex.lock()) /* no semicolon */
|
||||
};
|
||||
|
||||
let _writeln = {
|
||||
let out = Mutex;
|
||||
let mutex = Mutex;
|
||||
writeln!(out.lock(), "{}", mutex.lock()) /* no semicolon */
|
||||
};
|
||||
|
||||
let _print = {
|
||||
let mutex = Mutex;
|
||||
print!("{}", mutex.lock()) /* no semicolon */
|
||||
|
|
Loading…
Add table
Reference in a new issue