std: Rename thread::catch_panic to panic::recover
This commit is an implementation of [RFC 1236] and [RFC 1323] which rename the `thread::catch_panic` function to `panic::recover` while also replacing the `Send + 'static` bounds with a new `PanicSafe` bound. [RFC 1236]: https://github.com/rust-lang/rfcs/pull/1236 [RFC 1323]: https://github.com/rust-lang/rfcs/pull/1323 cc #27719
This commit is contained in:
parent
8864f2c83a
commit
0a13f1abaf
13 changed files with 462 additions and 45 deletions
24
src/test/compile-fail/not-panic-safe-2.rs
Normal file
24
src/test/compile-fail/not-panic-safe-2.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(recover)]
|
||||
|
||||
use std::panic::RecoverSafe;
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
fn assert<T: RecoverSafe + ?Sized>() {}
|
||||
|
||||
fn main() {
|
||||
assert::<Rc<RefCell<i32>>>(); //~ ERROR: is not implemented
|
||||
//~^ ERROR: is not implemented
|
||||
}
|
||||
|
||||
23
src/test/compile-fail/not-panic-safe-3.rs
Normal file
23
src/test/compile-fail/not-panic-safe-3.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(recover)]
|
||||
|
||||
use std::panic::RecoverSafe;
|
||||
use std::sync::Arc;
|
||||
use std::cell::RefCell;
|
||||
|
||||
fn assert<T: RecoverSafe + ?Sized>() {}
|
||||
|
||||
fn main() {
|
||||
assert::<Arc<RefCell<i32>>>(); //~ ERROR: is not implemented
|
||||
//~^ ERROR: is not implemented
|
||||
}
|
||||
22
src/test/compile-fail/not-panic-safe-4.rs
Normal file
22
src/test/compile-fail/not-panic-safe-4.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(recover)]
|
||||
|
||||
use std::panic::RecoverSafe;
|
||||
use std::cell::RefCell;
|
||||
|
||||
fn assert<T: RecoverSafe + ?Sized>() {}
|
||||
|
||||
fn main() {
|
||||
assert::<&RefCell<i32>>(); //~ ERROR: is not implemented
|
||||
//~^ ERROR is not implemented
|
||||
}
|
||||
21
src/test/compile-fail/not-panic-safe-5.rs
Normal file
21
src/test/compile-fail/not-panic-safe-5.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(recover)]
|
||||
|
||||
use std::panic::RecoverSafe;
|
||||
use std::cell::UnsafeCell;
|
||||
|
||||
fn assert<T: RecoverSafe + ?Sized>() {}
|
||||
|
||||
fn main() {
|
||||
assert::<*const UnsafeCell<i32>>(); //~ ERROR: is not implemented
|
||||
}
|
||||
23
src/test/compile-fail/not-panic-safe-6.rs
Normal file
23
src/test/compile-fail/not-panic-safe-6.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(recover)]
|
||||
|
||||
use std::panic::RecoverSafe;
|
||||
use std::cell::RefCell;
|
||||
|
||||
fn assert<T: RecoverSafe + ?Sized>() {}
|
||||
|
||||
fn main() {
|
||||
assert::<*mut RefCell<i32>>(); //~ ERROR: is not implemented
|
||||
//~^ ERROR is not implemented
|
||||
}
|
||||
|
||||
20
src/test/compile-fail/not-panic-safe.rs
Normal file
20
src/test/compile-fail/not-panic-safe.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(recover)]
|
||||
|
||||
use std::panic::RecoverSafe;
|
||||
|
||||
fn assert<T: RecoverSafe + ?Sized>() {}
|
||||
|
||||
fn main() {
|
||||
assert::<&mut i32>(); //~ ERROR: RecoverSafe` is not implemented
|
||||
}
|
||||
|
|
@ -8,15 +8,13 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(std_misc, binary_heap_extras, catch_panic, rand, sync_poison)]
|
||||
#![feature(recover, rand, std_panic)]
|
||||
|
||||
use std::__rand::{thread_rng, Rng};
|
||||
use std::thread;
|
||||
use std::panic::{self, AssertRecoverSafe};
|
||||
|
||||
use std::collections::BinaryHeap;
|
||||
use std::cmp;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
||||
|
||||
static DROP_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
|
||||
|
|
@ -66,31 +64,24 @@ fn test_integrity() {
|
|||
|
||||
// heapify the sane items
|
||||
rng.shuffle(&mut panic_ords);
|
||||
let heap = Arc::new(Mutex::new(BinaryHeap::from_vec(panic_ords)));
|
||||
let mut heap = BinaryHeap::from(panic_ords);
|
||||
let inner_data;
|
||||
|
||||
{
|
||||
let heap_ref = heap.clone();
|
||||
|
||||
|
||||
// push the panicking item to the heap and catch the panic
|
||||
let thread_result = thread::catch_panic(move || {
|
||||
heap.lock().unwrap().push(panic_item);
|
||||
});
|
||||
let thread_result = {
|
||||
let mut heap_ref = AssertRecoverSafe::new(&mut heap);
|
||||
panic::recover(move || {
|
||||
heap_ref.push(panic_item);
|
||||
})
|
||||
};
|
||||
assert!(thread_result.is_err());
|
||||
|
||||
// Assert no elements were dropped
|
||||
let drops = DROP_COUNTER.load(Ordering::SeqCst);
|
||||
//assert!(drops == 0, "Must not drop items. drops={}", drops);
|
||||
|
||||
{
|
||||
// now fetch the binary heap's data vector
|
||||
let mutex_guard = match heap_ref.lock() {
|
||||
Ok(x) => x,
|
||||
Err(poison) => poison.into_inner(),
|
||||
};
|
||||
inner_data = mutex_guard.clone().into_vec();
|
||||
}
|
||||
assert!(drops == 0, "Must not drop items. drops={}", drops);
|
||||
inner_data = heap.clone().into_vec();
|
||||
drop(heap);
|
||||
}
|
||||
let drops = DROP_COUNTER.load(Ordering::SeqCst);
|
||||
assert_eq!(drops, DATASZ);
|
||||
|
|
|
|||
51
src/test/run-pass/panic-safe.rs
Normal file
51
src/test/run-pass/panic-safe.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![allow(dead_code)]
|
||||
#![feature(recover)]
|
||||
|
||||
use std::panic::RecoverSafe;
|
||||
use std::cell::RefCell;
|
||||
use std::sync::{Mutex, RwLock, Arc};
|
||||
use std::rc::Rc;
|
||||
|
||||
struct Foo { a: i32 }
|
||||
|
||||
fn assert<T: RecoverSafe + ?Sized>() {}
|
||||
|
||||
fn main() {
|
||||
assert::<i32>();
|
||||
assert::<&i32>();
|
||||
assert::<*mut i32>();
|
||||
assert::<*const i32>();
|
||||
assert::<usize>();
|
||||
assert::<str>();
|
||||
assert::<&str>();
|
||||
assert::<Foo>();
|
||||
assert::<&Foo>();
|
||||
assert::<Vec<i32>>();
|
||||
assert::<String>();
|
||||
assert::<RefCell<i32>>();
|
||||
assert::<Box<i32>>();
|
||||
assert::<Mutex<i32>>();
|
||||
assert::<RwLock<i32>>();
|
||||
assert::<Rc<i32>>();
|
||||
assert::<Arc<i32>>();
|
||||
|
||||
fn bar<T>() {
|
||||
assert::<Mutex<T>>();
|
||||
assert::<RwLock<T>>();
|
||||
}
|
||||
fn baz<T: RecoverSafe>() {
|
||||
assert::<Box<T>>();
|
||||
assert::<Vec<T>>();
|
||||
assert::<RefCell<T>>();
|
||||
}
|
||||
}
|
||||
|
|
@ -8,11 +8,11 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(catch_panic, start)]
|
||||
#![feature(std_panic, recover, start)]
|
||||
|
||||
use std::ffi::CStr;
|
||||
use std::process::{Command, Output};
|
||||
use std::thread;
|
||||
use std::panic;
|
||||
use std::str;
|
||||
|
||||
#[start]
|
||||
|
|
@ -22,8 +22,8 @@ fn start(argc: isize, argv: *const *const u8) -> isize {
|
|||
match **argv.offset(1) as char {
|
||||
'1' => {}
|
||||
'2' => println!("foo"),
|
||||
'3' => assert!(thread::catch_panic(|| {}).is_ok()),
|
||||
'4' => assert!(thread::catch_panic(|| panic!()).is_err()),
|
||||
'3' => assert!(panic::recover(|| {}).is_ok()),
|
||||
'4' => assert!(panic::recover(|| panic!()).is_err()),
|
||||
'5' => assert!(Command::new("test").spawn().is_err()),
|
||||
_ => panic!()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue