Skip to content

Commit

Permalink
io::Write::write_fmt: panic if the formatter fails when the stream do…
Browse files Browse the repository at this point in the history
…es not fail
  • Loading branch information
RalfJung committed May 11, 2024
1 parent ef15976 commit 962459e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion library/alloc/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,9 @@ pub fn format(args: Arguments<'_>) -> string::String {
fn format_inner(args: Arguments<'_>) -> string::String {
let capacity = args.estimated_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
}

Expand Down
6 changes: 5 additions & 1 deletion library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,11 @@ pub trait Write {
if output.error.is_err() {
output.error
} 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"
);
}
}
}
Expand Down

0 comments on commit 962459e

Please sign in to comment.