-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Description
I tried this code (using cargo test
):
use std::fmt::{Display, Formatter};
use std::fmt;
pub struct A(Vec<u32>);
impl Display for A {
fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result {
self.0[0];
Ok(())
}
}
#[test]
fn main() {
let a = A(vec![]);
eprintln!("{}", a);
}
I expected to see this happen:
The thread should've panicked with 'Index out of bounds: the len is 0 but the index is 0'
Instead, this happened:
Thread crashes with SIGILL (illegal instruction)
Meta
This bug works in stable, beta and nightly.
It also does the same in rust playground.
Here is my cpu info (I figured that it might be useful given the nature of the bug).
The bug doesn't appear if run from main, nor if run directly (without passing from the Display impl).
rustc --version --verbose
:
rustc 1.41.1 (f3e1a954d 2020-02-24)
binary: rustc
commit-hash: f3e1a954d2ead4e2fc197c7da7d71e6c61bad196
commit-date: 2020-02-24
host: x86_64-unknown-linux-gnu
release: 1.41.1
LLVM version: 9.0
Backtrace
running 1 test
thread panicked while processing panic. aborting.
error: test failed, to rerun pass '--lib'
Caused by:
process didn't exit successfully: /path/to/proj/target/debug/deps/proj-296b7cb53c6210d6
(signal: 4, SIGILL: illegal instruction)
Activity
jonas-schievink commentedon Feb 28, 2020
This seems to be due to the
eprintln!
. Usingformat!
instead works fine.Mark-Simulacrum commentedon Feb 28, 2020
I can reproduce. This seems to happen if LOCAL_STDERR is initialized (e.g., with set_panic) which leads us to this code. Note that the RefCell there is borrowed so that we can write to it, but then when we go to panic we try to temporarily take the value out. This borrows the RefCell mutably, again, leading to a panic. Since this happens within an existing panic, we abort the process.
Smaller example:
std: Don't abort process when printing panics in tests
Rollup merge of rust-lang#69959 - alexcrichton:fix-panic-in-print, r=…
std: Don't abort process when printing panics in tests
Rollup merge of rust-lang#69959 - alexcrichton:fix-panic-in-print, r=…
std: Don't abort process when printing panics in tests
Rollup merge of rust-lang#69959 - alexcrichton:fix-panic-in-print, r=…
Rollup merge of rust-lang#69959 - alexcrichton:fix-panic-in-print, r=…