about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-05-12 08:34:32 +0000
committerbors <bors@rust-lang.org>2024-05-12 08:34:32 +0000
commit4fd98a4b1b100f5329c6efae18031791f64372d2 (patch)
tree197a37c7a58ade9e6ec7004cb8e3ee41bfa9a169
parent645bc609d994b6556c9aebbdd810f1d75c3e2206 (diff)
parente00f27b7be9084e548f7197325c2f343e8ad27b9 (diff)
downloadrust-4fd98a4b1b100f5329c6efae18031791f64372d2.tar.gz
rust-4fd98a4b1b100f5329c6efae18031791f64372d2.zip
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
-rw-r--r--library/alloc/src/fmt.rs4
-rw-r--r--library/std/src/io/mod.rs6
-rw-r--r--tests/ui/write-fmt-errors.rs29
3 files changed, 26 insertions, 13 deletions
diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs
index d7dc6ae6694..ae44cab8131 100644
--- a/library/alloc/src/fmt.rs
+++ b/library/alloc/src/fmt.rs
@@ -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
     }
 
diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs
index af055152cbe..5c6e7b7bd50 100644
--- a/library/std/src/io/mod.rs
+++ b/library/std/src/io/mod.rs
@@ -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"
+                    );
                 }
             }
         }
diff --git a/tests/ui/write-fmt-errors.rs b/tests/ui/write-fmt-errors.rs
index f194e25b556..1dafb9a784b 100644
--- a/tests/ui/write-fmt-errors.rs
+++ b/tests/ui/write-fmt-errors.rs
@@ -1,9 +1,11 @@
 //@ run-pass
+//@ needs-unwind
 
 #![feature(io_error_uncategorized)]
 
 use std::fmt;
 use std::io::{self, Error, Write, sink};
+use std::panic::catch_unwind;
 
 struct ErrorDisplay;
 
@@ -15,7 +17,6 @@ impl fmt::Display for ErrorDisplay {
 
 struct ErrorWriter;
 
-const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Uncategorized;
 const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
 
 impl Write for ErrorWriter {
@@ -27,22 +28,28 @@ impl Write for ErrorWriter {
 }
 
 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
     let res = write!(ErrorWriter, "abc");
     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);
     assert!(res.is_err(), "writer error did not propagate");
     assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);
 
-    // Formatter error
-    let res = write!(ErrorWriter, "{} abc", ErrorDisplay);
-    assert!(res.is_err(), "formatter error did not propagate");
-    assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
+    // Formatter error when the `{}` comes first
+    let res = catch_unwind(|| write!(ErrorWriter, "{} abc", ErrorDisplay));
+    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
+    );
 }