forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std: Don't abort process when printing panics in tests
This commit fixes an issue when using `set_print` and friends, notably used by libtest, to avoid aborting the process if printing panics. This previously panicked due to borrowing a mutable `RefCell` twice, and this is worked around by borrowing these cells for less time, instead taking out and removing contents temporarily. Closes rust-lang#69558
- Loading branch information
1 parent
23de827
commit 773c04c
Showing
3 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// run-pass | ||
// ignore-wasm no subprocess support | ||
|
||
#![feature(set_stdio)] | ||
|
||
use std::fmt; | ||
use std::fmt::{Display, Formatter}; | ||
use std::io::set_panic; | ||
|
||
pub struct A; | ||
|
||
impl Display for A { | ||
fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { | ||
panic!(); | ||
} | ||
} | ||
|
||
fn main() { | ||
set_panic(Some(Box::new(Vec::new()))); | ||
assert!(std::panic::catch_unwind(|| { | ||
eprintln!("{}", A); | ||
}) | ||
.is_err()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// compile-flags:--test | ||
// run-pass | ||
// ignore-wasm no subprocess support | ||
|
||
use std::fmt; | ||
use std::fmt::{Display, Formatter}; | ||
|
||
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 result = std::panic::catch_unwind(|| { | ||
let a = A(vec![]); | ||
eprintln!("{}", a); | ||
}); | ||
assert!(result.is_err()); | ||
} |