Skip to content

println panic suppressed by test harness stdio capture #107118

Open
@SimonSapin

Description

@SimonSapin

This code prints something then panics, which seems fine:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1f799e1599b2e8b7ecac0963864b8020

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.)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2382727df2a6289b4108cdb959cd14af

#[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

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryC-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions