Open
Description
This code prints something
then panics, which seems fine:
use std::fmt;
struct FailingDisplay;
impl fmt::Display for FailingDisplay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "something")?;
Err(fmt::Error)
}
}
fn main() {
eprintln!("{}", FailingDisplay)
}
Running `target/debug/playground`
something
thread 'main' panicked at 'failed printing to stderr: formatter error', library/std/src/io/stdio.rs:1009:9
Let’s add a test for this behavior. (In reality, this is a non-regression test that will stop panicking when I fix a bug that make the Display
impl return an error.)
#[test]
#[should_panic]
fn test() {
eprintln!("{}", FailingDisplay)
}
I expected to see this happen: eprintln!
panics in the test, same as in main
Instead, this happened: the test prints something
but does not panic unless it is run with --nocapture
. I did not expect output capturing to affect whether something panics.
running 1 test
test test - should panic ... FAILED
failures:
---- test stdout ----
something
note: test did not panic as expected
failures:
test
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Meta
Identical results with three Rust versions:
- On playground: Build using the Stable version: 1.66.1
- On playground: Build using the Nightly version: 1.68.0-nightly (2023-01-19 4c83bd0)
- In my larger project:
rustc --version --verbose
:
rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: aarch64-apple-darwin
release: 1.65.0
LLVM version: 15.0.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
No status