Skip to content

Commit 6cfafad

Browse files
committed
Make sure formatter errors are emitted by the default Write::write_fmt
Previously, if an error was returned from the formatter that did not originate in an underlying writer error, Write::write_fmt would return successfully even if the formatting did not complete (was interrupted by an `fmt::Error` return). Now we choose to emit an io::Error with kind Other for formatter errors. Since this may reveal error returns from `write!()` and similar that previously passed silently, it's a kind of a [breaking-change].
1 parent 8842e28 commit 6cfafad

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/libstd/io/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,14 @@ pub trait Write {
10551055
let mut output = Adaptor { inner: self, error: Ok(()) };
10561056
match fmt::write(&mut output, fmt) {
10571057
Ok(()) => Ok(()),
1058-
Err(..) => output.error
1058+
Err(..) => {
1059+
// check if the error came from the underlying `Write` or not
1060+
if output.error.is_err() {
1061+
output.error
1062+
} else {
1063+
Err(Error::new(ErrorKind::Other, "formatter error"))
1064+
}
1065+
}
10591066
}
10601067
}
10611068

src/test/run-pass/write-fmt-errors.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::fmt;
12+
use std::io::{self, Error, Write, sink};
13+
14+
struct ErrorDisplay;
15+
16+
impl fmt::Display for ErrorDisplay {
17+
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
18+
Err(fmt::Error)
19+
}
20+
}
21+
22+
struct ErrorWriter;
23+
24+
const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Other;
25+
const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
26+
27+
impl Write for ErrorWriter {
28+
fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
29+
Err(Error::new(WRITER_ERROR, "not connected"))
30+
}
31+
32+
fn flush(&mut self) -> io::Result<()> { Ok(()) }
33+
}
34+
35+
fn main() {
36+
// Test that the error from the formatter is propagated.
37+
let res = write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar");
38+
assert!(res.is_err(), "formatter error did not propagate");
39+
assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
40+
41+
// Test that an underlying error is propagated
42+
let res = write!(ErrorWriter, "abc");
43+
assert!(res.is_err(), "writer error did not propagate");
44+
45+
// Writer error
46+
let res = write!(ErrorWriter, "abc {}", ErrorDisplay);
47+
assert!(res.is_err(), "writer error did not propagate");
48+
assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);
49+
50+
// Formatter error
51+
let res = write!(ErrorWriter, "{} abc", ErrorDisplay);
52+
assert!(res.is_err(), "formatter error did not propagate");
53+
assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
54+
}

0 commit comments

Comments
 (0)