about summary refs log tree commit diff
path: root/tests/ui/io-checks
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-07-11 07:35:19 +0200
committerGitHub <noreply@github.com>2025-07-11 07:35:19 +0200
commit66799d0b83df8ef34df210ba454b59fce53fd1e9 (patch)
tree4d5703b8d224e0d654c41b81edd9eb7fed76be7d /tests/ui/io-checks
parent3d6eb225e26448b09fdec1ba7222f3e7174c6ac2 (diff)
parent259512e3b6a920b1e1eb6034fbc738e7b9815337 (diff)
downloadrust-66799d0b83df8ef34df210ba454b59fce53fd1e9.tar.gz
rust-66799d0b83df8ef34df210ba454b59fce53fd1e9.zip
Rollup merge of #143303 - Kivooeo:tf28, r=tgross35
`tests/ui`: A New Order [28/28] FINAL PART

> [!NOTE]
>
> Intermediate commits are intended to help review, but will be squashed prior to merge.

Some `tests/ui/` housekeeping, to trim down number of tests directly under `tests/ui/`. Part of rust-lang/rust#133895.

r? ``@tgross35``
Diffstat (limited to 'tests/ui/io-checks')
-rw-r--r--tests/ui/io-checks/write-macro-error.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/ui/io-checks/write-macro-error.rs b/tests/ui/io-checks/write-macro-error.rs
new file mode 100644
index 00000000000..857ea0024e1
--- /dev/null
+++ b/tests/ui/io-checks/write-macro-error.rs
@@ -0,0 +1,62 @@
+//! Tests that errors from both the writer (`Write::write`) and formatter (`Display::fmt`)
+//! are correctly propagated: writer errors return `Err`, formatter errors cause panics.
+
+//@ run-pass
+//@ needs-unwind
+
+#![feature(io_error_uncategorized)]
+
+use std::fmt;
+use std::io::{self, Error, Write};
+use std::panic::catch_unwind;
+
+struct ErrorDisplay;
+
+impl fmt::Display for ErrorDisplay {
+    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
+        Err(fmt::Error)
+    }
+}
+
+struct ErrorWriter;
+
+const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
+
+impl Write for ErrorWriter {
+    fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
+        Err(Error::new(WRITER_ERROR, "not connected"))
+    }
+
+    fn flush(&mut self) -> io::Result<()> {
+        Ok(())
+    }
+}
+
+fn main() {
+    // Test that an underlying error is propagated
+    let res = write!(ErrorWriter, "abc");
+    assert!(res.is_err(), "writer error did not propagate");
+
+    // Test that the error from the formatter is detected.
+    let res = catch_unwind(|| write!(vec![], "{} {} {}", 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 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
+    );
+}