Skip to content

Commit e00f27b

Browse files
committed
io::Write::write_fmt: panic if the formatter fails when the stream does not fail
1 parent ef15976 commit e00f27b

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

library/alloc/src/fmt.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,9 @@ pub fn format(args: Arguments<'_>) -> string::String {
630630
fn format_inner(args: Arguments<'_>) -> string::String {
631631
let capacity = args.estimated_capacity();
632632
let mut output = string::String::with_capacity(capacity);
633-
output.write_fmt(args).expect("a formatting trait implementation returned an error");
633+
output
634+
.write_fmt(args)
635+
.expect("a formatting trait implementation returned an error when the underlying stream did not");
634636
output
635637
}
636638

library/std/src/io/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,11 @@ pub trait Write {
18391839
if output.error.is_err() {
18401840
output.error
18411841
} else {
1842-
Err(error::const_io_error!(ErrorKind::Uncategorized, "formatter error"))
1842+
// This shouldn't happen: the underlying stream did not error, but somehow
1843+
// the formatter still errored?
1844+
panic!(
1845+
"a formatting trait implementation returned an error when the underlying stream did not"
1846+
);
18431847
}
18441848
}
18451849
}

tests/ui/write-fmt-errors.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//@ run-pass
2+
//@ needs-unwind
23

34
#![feature(io_error_uncategorized)]
45

56
use std::fmt;
67
use std::io::{self, Error, Write, sink};
8+
use std::panic::catch_unwind;
79

810
struct ErrorDisplay;
911

@@ -15,7 +17,6 @@ impl fmt::Display for ErrorDisplay {
1517

1618
struct ErrorWriter;
1719

18-
const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Uncategorized;
1920
const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
2021

2122
impl Write for ErrorWriter {
@@ -27,22 +28,28 @@ impl Write for ErrorWriter {
2728
}
2829

2930
fn main() {
30-
// Test that the error from the formatter is propagated.
31-
let res = write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar");
32-
assert!(res.is_err(), "formatter error did not propagate");
33-
assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
34-
3531
// Test that an underlying error is propagated
3632
let res = write!(ErrorWriter, "abc");
3733
assert!(res.is_err(), "writer error did not propagate");
3834

39-
// Writer error
35+
// Test that the error from the formatter is detected.
36+
let res = catch_unwind(|| write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar"));
37+
let err = res.expect_err("formatter error did not lead to panic").downcast::<&str>().unwrap();
38+
assert!(
39+
err.contains("formatting trait implementation returned an error"),
40+
"unexpected panic: {}", err
41+
);
42+
43+
// Writer error when there's some string before the first `{}`
4044
let res = write!(ErrorWriter, "abc {}", ErrorDisplay);
4145
assert!(res.is_err(), "writer error did not propagate");
4246
assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);
4347

44-
// Formatter error
45-
let res = write!(ErrorWriter, "{} abc", ErrorDisplay);
46-
assert!(res.is_err(), "formatter error did not propagate");
47-
assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
48+
// Formatter error when the `{}` comes first
49+
let res = catch_unwind(|| write!(ErrorWriter, "{} abc", ErrorDisplay));
50+
let err = res.expect_err("formatter error did not lead to panic").downcast::<&str>().unwrap();
51+
assert!(
52+
err.contains("formatting trait implementation returned an error"),
53+
"unexpected panic: {}", err
54+
);
4855
}

0 commit comments

Comments
 (0)