Auto merge of #125012 - RalfJung:format-error, r=Mark-Simulacrum,work… · rust-lang/rust@4fd98a4 (original) (raw)

1

1

`//@ run-pass

`

``

2

`+

//@ needs-unwind

`

2

3

``

3

4

`#![feature(io_error_uncategorized)]

`

4

5

``

5

6

`use std::fmt;

`

6

7

`use std::io::{self, Error, Write, sink};

`

``

8

`+

use std::panic::catch_unwind;

`

7

9

``

8

10

`struct ErrorDisplay;

`

9

11

``

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

`

15

17

``

16

18

`struct ErrorWriter;

`

17

19

``

18

``

`-

const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Uncategorized;

`

19

20

`const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;

`

20

21

``

21

22

`impl Write for ErrorWriter {

`

`@@ -27,22 +28,28 @@ impl Write for ErrorWriter {

`

27

28

`}

`

28

29

``

29

30

`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

``

-

35

31

`// Test that an underlying error is propagated

`

36

32

`let res = write!(ErrorWriter, "abc");

`

37

33

`assert!(res.is_err(), "writer error did not propagate");

`

38

34

``

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 {}

``

40

44

`let res = write!(ErrorWriter, "abc {}", ErrorDisplay);

`

41

45

`assert!(res.is_err(), "writer error did not propagate");

`

42

46

`assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);

`

43

47

``

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

`+

);

`

48

55

`}

`