Skip to content

std: Ensure AssertRecoverSafe indeed is more often #30513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/libstd/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,19 @@ use thread::Result;
across a recover boundary"]
pub trait RecoverSafe {}

/// A marker trait representing types which do not contain an `UnsafeCell` by
/// value internally.
/// A marker trait representing types where a shared reference is considered
/// recover safe.
///
/// This trait is namely not implemented by `UnsafeCell`, the root of all
/// interior mutability.
///
/// This is a "helper marker trait" used to provide impl blocks for the
/// `RecoverSafe` trait, for more information see that documentation.
#[unstable(feature = "recover", reason = "awaiting feedback", issue = "27719")]
#[rustc_on_unimplemented = "the type {Self} contains interior mutability \
and a reference may not be safely transferrable \
across a recover boundary"]
pub trait NoUnsafeCell {}
pub trait RefRecoverSafe {}

/// A simple wrapper around a type to assert that it is panic safe.
///
Expand Down Expand Up @@ -157,27 +160,28 @@ pub struct AssertRecoverSafe<T>(T);
// * Our custom AssertRecoverSafe wrapper is indeed recover safe
impl RecoverSafe for .. {}
impl<'a, T: ?Sized> !RecoverSafe for &'a mut T {}
impl<'a, T: NoUnsafeCell + ?Sized> RecoverSafe for &'a T {}
impl<T: NoUnsafeCell + ?Sized> RecoverSafe for *const T {}
impl<T: NoUnsafeCell + ?Sized> RecoverSafe for *mut T {}
impl<'a, T: RefRecoverSafe + ?Sized> RecoverSafe for &'a T {}
impl<T: RefRecoverSafe + ?Sized> RecoverSafe for *const T {}
impl<T: RefRecoverSafe + ?Sized> RecoverSafe for *mut T {}
impl<T: RecoverSafe> RecoverSafe for Unique<T> {}
impl<T: NoUnsafeCell + ?Sized> RecoverSafe for Shared<T> {}
impl<T: RefRecoverSafe + ?Sized> RecoverSafe for Shared<T> {}
impl<T: ?Sized> RecoverSafe for Mutex<T> {}
impl<T: ?Sized> RecoverSafe for RwLock<T> {}
impl<T> RecoverSafe for AssertRecoverSafe<T> {}

// not covered via the Shared impl above b/c the inner contents use
// Cell/AtomicUsize, but the usage here is recover safe so we can lift the
// impl up one level to Arc/Rc itself
impl<T: NoUnsafeCell + ?Sized> RecoverSafe for Rc<T> {}
impl<T: NoUnsafeCell + ?Sized> RecoverSafe for Arc<T> {}
impl<T: RefRecoverSafe + ?Sized> RecoverSafe for Rc<T> {}
impl<T: RefRecoverSafe + ?Sized> RecoverSafe for Arc<T> {}

// Pretty simple implementations for the `NoUnsafeCell` marker trait, basically
// just saying that this is a marker trait and `UnsafeCell` is the only thing
// which doesn't implement it (which then transitively applies to everything
// else.
impl NoUnsafeCell for .. {}
impl<T: ?Sized> !NoUnsafeCell for UnsafeCell<T> {}
// Pretty simple implementations for the `RefRecoverSafe` marker trait,
// basically just saying that this is a marker trait and `UnsafeCell` is the
// only thing which doesn't implement it (which then transitively applies to
// everything else.
impl RefRecoverSafe for .. {}
impl<T: ?Sized> !RefRecoverSafe for UnsafeCell<T> {}
impl<T> RefRecoverSafe for AssertRecoverSafe<T> {}

impl<T> AssertRecoverSafe<T> {
/// Creates a new `AssertRecoverSafe` wrapper around the provided type.
Expand Down
6 changes: 5 additions & 1 deletion src/test/run-pass/panic-safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![allow(dead_code)]
#![feature(recover)]

use std::panic::RecoverSafe;
use std::panic::{RecoverSafe, AssertRecoverSafe};
use std::cell::RefCell;
use std::sync::{Mutex, RwLock, Arc};
use std::rc::Rc;
Expand Down Expand Up @@ -47,5 +47,9 @@ fn main() {
assert::<Box<T>>();
assert::<Vec<T>>();
assert::<RefCell<T>>();
assert::<AssertRecoverSafe<T>>();
assert::<&AssertRecoverSafe<T>>();
assert::<Rc<AssertRecoverSafe<T>>>();
assert::<Arc<AssertRecoverSafe<T>>>();
}
}