Auto merge of #125012 - RalfJung:format-error, r=Mark-Simulacrum,workingjubilee
io::Write::write_fmt: panic if the formatter fails when the stream does not fail Follow-up to https://github.com/rust-lang/rust/pull/124954
This commit is contained in:
commit
4fd98a4b1b
@ -630,7 +630,9 @@ pub fn format(args: Arguments<'_>) -> string::String {
|
|||||||
fn format_inner(args: Arguments<'_>) -> string::String {
|
fn format_inner(args: Arguments<'_>) -> string::String {
|
||||||
let capacity = args.estimated_capacity();
|
let capacity = args.estimated_capacity();
|
||||||
let mut output = string::String::with_capacity(capacity);
|
let mut output = string::String::with_capacity(capacity);
|
||||||
output.write_fmt(args).expect("a formatting trait implementation returned an error");
|
output
|
||||||
|
.write_fmt(args)
|
||||||
|
.expect("a formatting trait implementation returned an error when the underlying stream did not");
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1839,7 +1839,11 @@ fn write_str(&mut self, s: &str) -> fmt::Result {
|
|||||||
if output.error.is_err() {
|
if output.error.is_err() {
|
||||||
output.error
|
output.error
|
||||||
} else {
|
} else {
|
||||||
Err(error::const_io_error!(ErrorKind::Uncategorized, "formatter error"))
|
// This shouldn't happen: the underlying stream did not error, but somehow
|
||||||
|
// the formatter still errored?
|
||||||
|
panic!(
|
||||||
|
"a formatting trait implementation returned an error when the underlying stream did not"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
//@ run-pass
|
//@ run-pass
|
||||||
|
//@ needs-unwind
|
||||||
|
|
||||||
#![feature(io_error_uncategorized)]
|
#![feature(io_error_uncategorized)]
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::{self, Error, Write, sink};
|
use std::io::{self, Error, Write, sink};
|
||||||
|
use std::panic::catch_unwind;
|
||||||
|
|
||||||
struct ErrorDisplay;
|
struct ErrorDisplay;
|
||||||
|
|
||||||
@ -15,7 +17,6 @@ fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
|
|||||||
|
|
||||||
struct ErrorWriter;
|
struct ErrorWriter;
|
||||||
|
|
||||||
const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Uncategorized;
|
|
||||||
const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
|
const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
|
||||||
|
|
||||||
impl Write for ErrorWriter {
|
impl Write for ErrorWriter {
|
||||||
@ -27,22 +28,28 @@ fn flush(&mut self) -> io::Result<()> { Ok(()) }
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Test that the error from the formatter is propagated.
|
|
||||||
let res = write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar");
|
|
||||||
assert!(res.is_err(), "formatter error did not propagate");
|
|
||||||
assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
|
|
||||||
|
|
||||||
// Test that an underlying error is propagated
|
// Test that an underlying error is propagated
|
||||||
let res = write!(ErrorWriter, "abc");
|
let res = write!(ErrorWriter, "abc");
|
||||||
assert!(res.is_err(), "writer error did not propagate");
|
assert!(res.is_err(), "writer error did not propagate");
|
||||||
|
|
||||||
// Writer error
|
// Test that the error from the formatter is detected.
|
||||||
|
let res = catch_unwind(|| write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar"));
|
||||||
|
let err = res.expect_err("formatter error did not lead to panic").downcast::<&str>().unwrap();
|
||||||
|
assert!(
|
||||||
|
err.contains("formatting trait implementation returned an error"),
|
||||||
|
"unexpected panic: {}", err
|
||||||
|
);
|
||||||
|
|
||||||
|
// Writer error when there's some string before the first `{}`
|
||||||
let res = write!(ErrorWriter, "abc {}", ErrorDisplay);
|
let res = write!(ErrorWriter, "abc {}", ErrorDisplay);
|
||||||
assert!(res.is_err(), "writer error did not propagate");
|
assert!(res.is_err(), "writer error did not propagate");
|
||||||
assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);
|
assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);
|
||||||
|
|
||||||
// Formatter error
|
// Formatter error when the `{}` comes first
|
||||||
let res = write!(ErrorWriter, "{} abc", ErrorDisplay);
|
let res = catch_unwind(|| write!(ErrorWriter, "{} abc", ErrorDisplay));
|
||||||
assert!(res.is_err(), "formatter error did not propagate");
|
let err = res.expect_err("formatter error did not lead to panic").downcast::<&str>().unwrap();
|
||||||
assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
|
assert!(
|
||||||
|
err.contains("formatting trait implementation returned an error"),
|
||||||
|
"unexpected panic: {}", err
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user