]> git.proxmox.com Git - rustc.git/blame - src/test/ui/write-fmt-errors.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / src / test / ui / write-fmt-errors.rs
CommitLineData
416331ca
XL
1// run-pass
2
136023e0
XL
3#![feature(io_error_uncategorized)]
4
7453a54e
SL
5use std::fmt;
6use std::io::{self, Error, Write, sink};
7
8struct ErrorDisplay;
9
10impl fmt::Display for ErrorDisplay {
11 fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
12 Err(fmt::Error)
13 }
14}
15
16struct ErrorWriter;
17
136023e0 18const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Uncategorized;
7453a54e
SL
19const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
20
21impl Write for ErrorWriter {
22 fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
23 Err(Error::new(WRITER_ERROR, "not connected"))
24 }
25
26 fn flush(&mut self) -> io::Result<()> { Ok(()) }
27}
28
29fn 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
35 // Test that an underlying error is propagated
36 let res = write!(ErrorWriter, "abc");
37 assert!(res.is_err(), "writer error did not propagate");
38
39 // Writer error
40 let res = write!(ErrorWriter, "abc {}", ErrorDisplay);
41 assert!(res.is_err(), "writer error did not propagate");
42 assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);
43
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}