2015-08-31 10:51:53 -05:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Panic support in the standard library
|
|
|
|
|
2016-04-07 12:42:53 -05:00
|
|
|
#![stable(feature = "std_panic", since = "1.9.0")]
|
2015-08-31 10:51:53 -05:00
|
|
|
|
2015-12-25 13:00:40 -06:00
|
|
|
use any::Any;
|
|
|
|
use boxed::Box;
|
2015-08-31 10:51:53 -05:00
|
|
|
use cell::UnsafeCell;
|
|
|
|
use ops::{Deref, DerefMut};
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 18:18:40 -05:00
|
|
|
use panicking;
|
2015-08-31 10:51:53 -05:00
|
|
|
use ptr::{Unique, Shared};
|
|
|
|
use rc::Rc;
|
|
|
|
use sync::{Arc, Mutex, RwLock};
|
|
|
|
use thread::Result;
|
|
|
|
|
std: Stabilize APIs for the 1.10 release
This commit applies the FCP decisions made by the libs team for the 1.10 cycle,
including both new stabilizations and deprecations. Specifically, the list of
APIs is:
Stabilized:
* `os::windows::fs::OpenOptionsExt::access_mode`
* `os::windows::fs::OpenOptionsExt::share_mode`
* `os::windows::fs::OpenOptionsExt::custom_flags`
* `os::windows::fs::OpenOptionsExt::attributes`
* `os::windows::fs::OpenOptionsExt::security_qos_flags`
* `os::unix::fs::OpenOptionsExt::custom_flags`
* `sync::Weak::new`
* `Default for sync::Weak`
* `panic::set_hook`
* `panic::take_hook`
* `panic::PanicInfo`
* `panic::PanicInfo::payload`
* `panic::PanicInfo::location`
* `panic::Location`
* `panic::Location::file`
* `panic::Location::line`
* `ffi::CStr::from_bytes_with_nul`
* `ffi::CStr::from_bytes_with_nul_unchecked`
* `ffi::FromBytesWithNulError`
* `fs::Metadata::modified`
* `fs::Metadata::accessed`
* `fs::Metadata::created`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange`
* `sync::atomic::Atomic{Usize,Isize,Bool,Ptr}::compare_exchange_weak`
* `collections::{btree,hash}_map::{Occupied,Vacant,}Entry::key`
* `os::unix::net::{UnixStream, UnixListener, UnixDatagram, SocketAddr}`
* `SocketAddr::is_unnamed`
* `SocketAddr::as_pathname`
* `UnixStream::connect`
* `UnixStream::pair`
* `UnixStream::try_clone`
* `UnixStream::local_addr`
* `UnixStream::peer_addr`
* `UnixStream::set_read_timeout`
* `UnixStream::set_write_timeout`
* `UnixStream::read_timeout`
* `UnixStream::write_Timeout`
* `UnixStream::set_nonblocking`
* `UnixStream::take_error`
* `UnixStream::shutdown`
* Read/Write/RawFd impls for `UnixStream`
* `UnixListener::bind`
* `UnixListener::accept`
* `UnixListener::try_clone`
* `UnixListener::local_addr`
* `UnixListener::set_nonblocking`
* `UnixListener::take_error`
* `UnixListener::incoming`
* RawFd impls for `UnixListener`
* `UnixDatagram::bind`
* `UnixDatagram::unbound`
* `UnixDatagram::pair`
* `UnixDatagram::connect`
* `UnixDatagram::try_clone`
* `UnixDatagram::local_addr`
* `UnixDatagram::peer_addr`
* `UnixDatagram::recv_from`
* `UnixDatagram::recv`
* `UnixDatagram::send_to`
* `UnixDatagram::send`
* `UnixDatagram::set_read_timeout`
* `UnixDatagram::set_write_timeout`
* `UnixDatagram::read_timeout`
* `UnixDatagram::write_timeout`
* `UnixDatagram::set_nonblocking`
* `UnixDatagram::take_error`
* `UnixDatagram::shutdown`
* RawFd impls for `UnixDatagram`
* `{BTree,Hash}Map::values_mut`
* `<[_]>::binary_search_by_key`
Deprecated:
* `StaticCondvar` - this, and all other static synchronization primitives
below, are usable today through the lazy-static crate on
stable Rust today. Additionally, we'd like the non-static
versions to be directly usable in a static context one day,
so they're unlikely to be the final forms of the APIs in any
case.
* `CONDVAR_INIT`
* `StaticMutex`
* `MUTEX_INIT`
* `StaticRwLock`
* `RWLOCK_INIT`
* `iter::Peekable::is_empty`
Closes #27717
Closes #27720
cc #27784 (but encode methods still exist)
Closes #30014
Closes #30425
Closes #30449
Closes #31190
Closes #31399
Closes #31767
Closes #32111
Closes #32281
Closes #32312
Closes #32551
Closes #33018
2016-05-17 13:57:07 -05:00
|
|
|
#[stable(feature = "panic_hooks", since = "1.10.0")]
|
2016-03-15 21:42:45 -05:00
|
|
|
pub use panicking::{take_hook, set_hook, PanicInfo, Location};
|
|
|
|
|
2016-05-24 16:24:44 -05:00
|
|
|
/// A marker trait which represents "panic safe" types in Rust.
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// This trait is implemented by default for many types and behaves similarly in
|
|
|
|
/// terms of inference of implementation to the `Send` and `Sync` traits. The
|
2016-05-24 00:28:32 -05:00
|
|
|
/// purpose of this trait is to encode what types are safe to cross a `catch_unwind`
|
|
|
|
/// boundary with no fear of unwind safety.
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
2016-05-24 00:28:32 -05:00
|
|
|
/// ## What is unwind safety?
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// In Rust a function can "return" early if it either panics or calls a
|
|
|
|
/// function which transitively panics. This sort of control flow is not always
|
|
|
|
/// anticipated, and has the possibility of causing subtle bugs through a
|
|
|
|
/// combination of two cricial components:
|
|
|
|
///
|
|
|
|
/// 1. A data structure is in a temporarily invalid state when the thread
|
|
|
|
/// panics.
|
|
|
|
/// 2. This broken invariant is then later observed.
|
|
|
|
///
|
2016-01-02 01:26:22 -06:00
|
|
|
/// Typically in Rust, it is difficult to perform step (2) because catching a
|
2015-08-31 10:51:53 -05:00
|
|
|
/// panic involves either spawning a thread (which in turns makes it difficult
|
2016-05-24 00:28:32 -05:00
|
|
|
/// to later witness broken invariants) or using the `catch_unwind` function in this
|
2016-01-02 01:26:22 -06:00
|
|
|
/// module. Additionally, even if an invariant is witnessed, it typically isn't a
|
2016-05-24 00:28:32 -05:00
|
|
|
/// problem in Rust because there are no uninitialized values (like in C or C++).
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// It is possible, however, for **logical** invariants to be broken in Rust,
|
2016-05-24 00:28:32 -05:00
|
|
|
/// which can end up causing behavioral bugs. Another key aspect of unwind safety
|
2016-01-02 01:26:22 -06:00
|
|
|
/// in Rust is that, in the absence of `unsafe` code, a panic cannot lead to
|
2015-08-31 10:51:53 -05:00
|
|
|
/// memory unsafety.
|
|
|
|
///
|
2016-05-24 00:28:32 -05:00
|
|
|
/// That was a bit of a whirlwind tour of unwind safety, but for more information
|
|
|
|
/// about unwind safety and how it applies to Rust, see an [associated RFC][rfc].
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md
|
|
|
|
///
|
2016-05-15 23:04:01 -05:00
|
|
|
/// ## What is `UnwindSafe`?
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
2016-05-24 00:28:32 -05:00
|
|
|
/// Now that we've got an idea of what unwind safety is in Rust, it's also
|
2016-01-02 01:26:22 -06:00
|
|
|
/// important to understand what this trait represents. As mentioned above, one
|
2016-05-24 00:28:32 -05:00
|
|
|
/// way to witness broken invariants is through the `catch_unwind` function in this
|
2015-08-31 10:51:53 -05:00
|
|
|
/// module as it allows catching a panic and then re-using the environment of
|
|
|
|
/// the closure.
|
|
|
|
///
|
2016-05-15 23:04:01 -05:00
|
|
|
/// Simply put, a type `T` implements `UnwindSafe` if it cannot easily allow
|
2016-05-24 00:28:32 -05:00
|
|
|
/// witnessing a broken invariant through the use of `catch_unwind` (catching a
|
2015-08-31 10:51:53 -05:00
|
|
|
/// panic). This trait is a marker trait, so it is automatically implemented for
|
2016-05-24 00:28:32 -05:00
|
|
|
/// many types, and it is also structurally composed (e.g. a struct is unwind
|
|
|
|
/// safe if all of its components are unwind safe).
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// Note, however, that this is not an unsafe trait, so there is not a succinct
|
|
|
|
/// contract that this trait is providing. Instead it is intended as more of a
|
2016-05-24 00:28:32 -05:00
|
|
|
/// "speed bump" to alert users of `catch_unwind` that broken invariants may be
|
2015-08-31 10:51:53 -05:00
|
|
|
/// witnessed and may need to be accounted for.
|
|
|
|
///
|
2016-04-07 12:42:53 -05:00
|
|
|
/// ## Who implements `UnwindSafe`?
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// Types such as `&mut T` and `&RefCell<T>` are examples which are **not**
|
2016-05-24 00:28:32 -05:00
|
|
|
/// unwind safe. The general idea is that any mutable state which can be shared
|
|
|
|
/// across `catch_unwind` is not unwind safe by default. This is because it is very
|
|
|
|
/// easy to witness a broken invariant outside of `catch_unwind` as the data is
|
2016-02-09 10:52:39 -06:00
|
|
|
/// simply accessed as usual.
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
2016-05-24 00:28:32 -05:00
|
|
|
/// Types like `&Mutex<T>`, however, are unwind safe because they implement
|
2015-08-31 10:51:53 -05:00
|
|
|
/// poisoning by default. They still allow witnessing a broken invariant, but
|
|
|
|
/// they already provide their own "speed bumps" to do so.
|
|
|
|
///
|
2016-04-07 12:42:53 -05:00
|
|
|
/// ## When should `UnwindSafe` be used?
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// Is not intended that most types or functions need to worry about this trait.
|
2016-05-24 00:28:32 -05:00
|
|
|
/// It is only used as a bound on the `catch_unwind` function and as mentioned above,
|
2016-05-15 23:04:01 -05:00
|
|
|
/// the lack of `unsafe` means it is mostly an advisory. The `AssertUnwindSafe`
|
2015-08-31 10:51:53 -05:00
|
|
|
/// wrapper struct in this module can be used to force this trait to be
|
2016-05-24 00:28:32 -05:00
|
|
|
/// implemented for any closed over variables passed to the `catch_unwind` function
|
2015-08-31 10:51:53 -05:00
|
|
|
/// (more on this below).
|
2016-04-07 12:42:53 -05:00
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
2015-08-31 10:51:53 -05:00
|
|
|
#[rustc_on_unimplemented = "the type {Self} may not be safely transferred \
|
2016-05-24 00:28:32 -05:00
|
|
|
across an unwind boundary"]
|
2016-04-07 12:42:53 -05:00
|
|
|
pub trait UnwindSafe {}
|
|
|
|
|
2015-12-21 11:39:45 -06:00
|
|
|
/// A marker trait representing types where a shared reference is considered
|
2016-05-24 00:28:32 -05:00
|
|
|
/// unwind safe.
|
2015-12-21 11:39:45 -06:00
|
|
|
///
|
|
|
|
/// This trait is namely not implemented by `UnsafeCell`, the root of all
|
|
|
|
/// interior mutability.
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// This is a "helper marker trait" used to provide impl blocks for the
|
2016-04-07 12:42:53 -05:00
|
|
|
/// `UnwindSafe` trait, for more information see that documentation.
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
2015-08-31 10:51:53 -05:00
|
|
|
#[rustc_on_unimplemented = "the type {Self} contains interior mutability \
|
|
|
|
and a reference may not be safely transferrable \
|
2016-05-24 00:28:32 -05:00
|
|
|
across a catch_unwind boundary"]
|
2016-04-07 12:42:53 -05:00
|
|
|
pub trait RefUnwindSafe {}
|
2015-08-31 10:51:53 -05:00
|
|
|
|
2016-05-24 00:28:32 -05:00
|
|
|
/// A simple wrapper around a type to assert that it is unwind safe.
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
2016-05-24 00:28:32 -05:00
|
|
|
/// When using `catch_unwind` it may be the case that some of the closed over
|
|
|
|
/// variables are not unwind safe. For example if `&mut T` is captured the
|
|
|
|
/// compiler will generate a warning indicating that it is not unwind safe. It
|
2015-08-31 10:51:53 -05:00
|
|
|
/// may not be the case, however, that this is actually a problem due to the
|
2016-05-24 00:28:32 -05:00
|
|
|
/// specific usage of `catch_unwind` if unwind safety is specifically taken into
|
2015-08-31 10:51:53 -05:00
|
|
|
/// account. This wrapper struct is useful for a quick and lightweight
|
2016-05-24 00:28:32 -05:00
|
|
|
/// annotation that a variable is indeed unwind safe.
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-04-07 12:42:53 -05:00
|
|
|
/// One way to use `AssertUnwindSafe` is to assert that the entire closure
|
2016-05-24 00:28:32 -05:00
|
|
|
/// itself is unwind safe, bypassing all checks for all variables:
|
2016-03-07 11:20:25 -06:00
|
|
|
///
|
2015-08-31 10:51:53 -05:00
|
|
|
/// ```
|
2016-04-07 12:42:53 -05:00
|
|
|
/// use std::panic::{self, AssertUnwindSafe};
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// let mut variable = 4;
|
|
|
|
///
|
2016-01-16 09:34:51 -06:00
|
|
|
/// // This code will not compile because the closure captures `&mut variable`
|
2016-05-24 00:28:32 -05:00
|
|
|
/// // which is not considered unwind safe by default.
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
2016-04-07 12:42:53 -05:00
|
|
|
/// // panic::catch_unwind(|| {
|
2015-08-31 10:51:53 -05:00
|
|
|
/// // variable += 3;
|
|
|
|
/// // });
|
|
|
|
///
|
2016-04-07 12:42:53 -05:00
|
|
|
/// // This, however, will compile due to the `AssertUnwindSafe` wrapper
|
|
|
|
/// let result = panic::catch_unwind(AssertUnwindSafe(|| {
|
2016-03-07 11:20:25 -06:00
|
|
|
/// variable += 3;
|
|
|
|
/// }));
|
|
|
|
/// // ...
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Wrapping the entire closure amounts to a blanket assertion that all captured
|
2016-04-07 12:42:53 -05:00
|
|
|
/// variables are unwind safe. This has the downside that if new captures are
|
|
|
|
/// added in the future, they will also be considered unwind safe. Therefore,
|
2016-03-07 11:20:25 -06:00
|
|
|
/// you may prefer to just wrap individual captures, as shown below. This is
|
|
|
|
/// more annotation, but it ensures that if a new capture is added which is not
|
2016-04-07 12:42:53 -05:00
|
|
|
/// unwind safe, you will get a compilation error at that time, which will
|
2016-03-07 11:20:25 -06:00
|
|
|
/// allow you to consider whether that new capture in fact represent a bug or
|
|
|
|
/// not.
|
|
|
|
///
|
|
|
|
/// ```
|
2016-04-07 12:42:53 -05:00
|
|
|
/// use std::panic::{self, AssertUnwindSafe};
|
2016-03-07 11:20:25 -06:00
|
|
|
///
|
|
|
|
/// let mut variable = 4;
|
|
|
|
/// let other_capture = 3;
|
|
|
|
///
|
2015-08-31 10:51:53 -05:00
|
|
|
/// let result = {
|
2016-04-07 12:42:53 -05:00
|
|
|
/// let mut wrapper = AssertUnwindSafe(&mut variable);
|
|
|
|
/// panic::catch_unwind(move || {
|
2016-03-07 11:20:25 -06:00
|
|
|
/// **wrapper += other_capture;
|
2015-08-31 10:51:53 -05:00
|
|
|
/// })
|
|
|
|
/// };
|
|
|
|
/// // ...
|
|
|
|
/// ```
|
2016-04-07 12:42:53 -05:00
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
pub struct AssertUnwindSafe<T>(
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
pub T
|
|
|
|
);
|
|
|
|
|
|
|
|
// Implementations of the `UnwindSafe` trait:
|
2015-08-31 10:51:53 -05:00
|
|
|
//
|
2016-04-07 12:42:53 -05:00
|
|
|
// * By default everything is unwind safe
|
|
|
|
// * pointers T contains mutability of some form are not unwind safe
|
2015-08-31 10:51:53 -05:00
|
|
|
// * Unique, an owning pointer, lifts an implementation
|
2016-04-07 12:42:53 -05:00
|
|
|
// * Types like Mutex/RwLock which are explicilty poisoned are unwind safe
|
|
|
|
// * Our custom AssertUnwindSafe wrapper is indeed unwind safe
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl UnwindSafe for .. {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<'a, T: ?Sized> !UnwindSafe for &'a mut T {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<'a, T: RefUnwindSafe + ?Sized> UnwindSafe for &'a T {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T: RefUnwindSafe + ?Sized> UnwindSafe for *const T {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T: RefUnwindSafe + ?Sized> UnwindSafe for *mut T {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T: UnwindSafe> UnwindSafe for Unique<T> {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T: RefUnwindSafe + ?Sized> UnwindSafe for Shared<T> {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T: ?Sized> UnwindSafe for Mutex<T> {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T: ?Sized> UnwindSafe for RwLock<T> {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T> UnwindSafe for AssertUnwindSafe<T> {}
|
2015-08-31 10:51:53 -05:00
|
|
|
|
|
|
|
// not covered via the Shared impl above b/c the inner contents use
|
2016-05-24 00:28:32 -05:00
|
|
|
// Cell/AtomicUsize, but the usage here is unwind safe so we can lift the
|
2015-08-31 10:51:53 -05:00
|
|
|
// impl up one level to Arc/Rc itself
|
2016-04-07 12:42:53 -05:00
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T: RefUnwindSafe + ?Sized> UnwindSafe for Rc<T> {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T: RefUnwindSafe + ?Sized> UnwindSafe for Arc<T> {}
|
2015-08-31 10:51:53 -05:00
|
|
|
|
2016-05-15 23:04:01 -05:00
|
|
|
// Pretty simple implementations for the `RefUnwindSafe` marker trait,
|
2015-12-21 11:39:45 -06:00
|
|
|
// 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
|
2016-01-02 01:26:22 -06:00
|
|
|
// everything else).
|
2016-04-07 12:42:53 -05:00
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl RefUnwindSafe for .. {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T: ?Sized> !RefUnwindSafe for UnsafeCell<T> {}
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T> RefUnwindSafe for AssertUnwindSafe<T> {}
|
|
|
|
|
2016-07-11 09:34:20 -05:00
|
|
|
#[stable(feature = "unwind_safe_lock_refs", since = "1.12.0")]
|
2016-07-09 00:48:04 -05:00
|
|
|
impl<T: ?Sized> RefUnwindSafe for Mutex<T> {}
|
2016-07-11 09:34:20 -05:00
|
|
|
#[stable(feature = "unwind_safe_lock_refs", since = "1.12.0")]
|
2016-07-09 00:48:04 -05:00
|
|
|
impl<T: ?Sized> RefUnwindSafe for RwLock<T> {}
|
|
|
|
|
2016-04-07 12:42:53 -05:00
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T> Deref for AssertUnwindSafe<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<T> DerefMut for AssertUnwindSafe<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
2015-08-31 10:51:53 -05:00
|
|
|
|
2016-04-07 12:42:53 -05:00
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
impl<R, F: FnOnce() -> R> FnOnce<()> for AssertUnwindSafe<F> {
|
|
|
|
type Output = R;
|
|
|
|
|
|
|
|
extern "rust-call" fn call_once(self, _args: ()) -> R {
|
|
|
|
(self.0)()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Invokes a closure, capturing the cause of an unwinding panic if one occurs.
|
2015-08-31 10:51:53 -05:00
|
|
|
///
|
|
|
|
/// This function will return `Ok` with the closure's result if the closure
|
|
|
|
/// does not panic, and will return `Err(cause)` if the closure panics. The
|
|
|
|
/// `cause` returned is the object with which panic was originally invoked.
|
|
|
|
///
|
|
|
|
/// It is currently undefined behavior to unwind from Rust code into foreign
|
|
|
|
/// code, so this function is particularly useful when Rust is called from
|
|
|
|
/// another language (normally C). This can run arbitrary Rust code, capturing a
|
|
|
|
/// panic and allowing a graceful handling of the error.
|
|
|
|
///
|
|
|
|
/// It is **not** recommended to use this function for a general try/catch
|
|
|
|
/// mechanism. The `Result` type is more appropriate to use for functions that
|
2016-04-07 12:42:53 -05:00
|
|
|
/// can fail on a regular basis. Additionally, this function is not guaranteed
|
2016-04-13 12:35:21 -05:00
|
|
|
/// to catch all panics, see the "Notes" section below.
|
2016-04-07 12:42:53 -05:00
|
|
|
///
|
2016-04-13 12:35:21 -05:00
|
|
|
/// The closure provided is required to adhere to the `UnwindSafe` trait to ensure
|
2016-04-07 12:42:53 -05:00
|
|
|
/// that all captured variables are safe to cross this boundary. The purpose of
|
|
|
|
/// this bound is to encode the concept of [exception safety][rfc] in the type
|
|
|
|
/// system. Most usage of this function should not need to worry about this
|
2016-05-24 00:28:32 -05:00
|
|
|
/// bound as programs are naturally unwind safe without `unsafe` code. If it
|
2016-04-07 12:42:53 -05:00
|
|
|
/// becomes a problem the associated `AssertUnwindSafe` wrapper type in this
|
2016-05-24 00:28:32 -05:00
|
|
|
/// module can be used to quickly assert that the usage here is indeed unwind
|
2015-08-31 10:51:53 -05:00
|
|
|
/// safe.
|
|
|
|
///
|
|
|
|
/// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md
|
|
|
|
///
|
2016-04-07 12:42:53 -05:00
|
|
|
/// # Notes
|
|
|
|
///
|
|
|
|
/// Note that this function **may not catch all panics** in Rust. A panic in
|
|
|
|
/// Rust is not always implemented via unwinding, but can be implemented by
|
|
|
|
/// aborting the process as well. This function *only* catches unwinding panics,
|
|
|
|
/// not those that abort the process.
|
|
|
|
///
|
2015-08-31 10:51:53 -05:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::panic;
|
|
|
|
///
|
2016-04-07 12:42:53 -05:00
|
|
|
/// let result = panic::catch_unwind(|| {
|
2015-08-31 10:51:53 -05:00
|
|
|
/// println!("hello!");
|
|
|
|
/// });
|
|
|
|
/// assert!(result.is_ok());
|
|
|
|
///
|
2016-04-07 12:42:53 -05:00
|
|
|
/// let result = panic::catch_unwind(|| {
|
2015-08-31 10:51:53 -05:00
|
|
|
/// panic!("oh no!");
|
|
|
|
/// });
|
|
|
|
/// assert!(result.is_err());
|
|
|
|
/// ```
|
2016-04-07 12:42:53 -05:00
|
|
|
#[stable(feature = "catch_unwind", since = "1.9.0")]
|
|
|
|
pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
|
2015-08-31 10:51:53 -05:00
|
|
|
unsafe {
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 18:18:40 -05:00
|
|
|
panicking::try(f)
|
2015-08-31 10:51:53 -05:00
|
|
|
}
|
|
|
|
}
|
2015-12-25 13:00:40 -06:00
|
|
|
|
2016-05-20 00:35:09 -05:00
|
|
|
/// Triggers a panic without invoking the panic hook.
|
2015-12-25 13:00:40 -06:00
|
|
|
///
|
2016-04-07 12:42:53 -05:00
|
|
|
/// This is designed to be used in conjunction with `catch_unwind` to, for
|
|
|
|
/// example, carry a panic across a layer of C code.
|
|
|
|
///
|
|
|
|
/// # Notes
|
|
|
|
///
|
|
|
|
/// Note that panics in Rust are not always implemented via unwinding, but they
|
|
|
|
/// may be implemented by aborting the process. If this function is called when
|
|
|
|
/// panics are implemented this way then this function will abort the process,
|
|
|
|
/// not trigger an unwind.
|
2015-12-25 13:00:40 -06:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```should_panic
|
|
|
|
/// use std::panic;
|
|
|
|
///
|
2016-04-07 12:42:53 -05:00
|
|
|
/// let result = panic::catch_unwind(|| {
|
2015-12-25 13:00:40 -06:00
|
|
|
/// panic!("oh no!");
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// if let Err(err) = result {
|
2016-04-07 12:42:53 -05:00
|
|
|
/// panic::resume_unwind(err);
|
2015-12-25 13:00:40 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2016-04-07 12:42:53 -05:00
|
|
|
#[stable(feature = "resume_unwind", since = "1.9.0")]
|
|
|
|
pub fn resume_unwind(payload: Box<Any + Send>) -> ! {
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 18:18:40 -05:00
|
|
|
panicking::rust_panic(payload)
|
2016-04-07 12:42:53 -05:00
|
|
|
}
|