Skip to content

Add std::panic::propagate #30557

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
Jan 7, 2016
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
27 changes: 27 additions & 0 deletions src/libstd/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#![unstable(feature = "std_panic", reason = "awaiting feedback",
issue = "27719")]

use any::Any;
use boxed::Box;
use cell::UnsafeCell;
use ops::{Deref, DerefMut};
use ptr::{Unique, Shared};
Expand Down Expand Up @@ -259,3 +261,28 @@ pub fn recover<F: FnOnce() -> R + RecoverSafe, R>(f: F) -> Result<R> {
}
Ok(result.unwrap())
}

/// Triggers a panic without invoking the panic handler.
///
/// This is designed to be used in conjunction with `recover` to, for example,
/// carry a panic across a layer of C code.
///
/// # Examples
///
/// ```should_panic
/// #![feature(std_panic, recover, panic_propagate)]
///
/// use std::panic;
///
/// let result = panic::recover(|| {
/// panic!("oh no!");
/// });
///
/// if let Err(err) = result {
/// panic::propagate(err);
/// }
/// ```
#[unstable(feature = "panic_propagate", reason = "awaiting feedback", issue = "30752")]
pub fn propagate(payload: Box<Any + Send>) -> ! {
unwind::rust_panic(payload)
}
2 changes: 1 addition & 1 deletion src/libstd/sys/common/unwind/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub fn panicking() -> bool {
#[inline(never)]
#[no_mangle]
#[allow(private_no_mangle_fns)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

presumably can remove this allow if the fn is becoming public?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly - not sure how deeply that lint looks. This function is now public within its module, but not public outside of the crate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, still required: http://is.gd/Z4qyRE

On Fri, Dec 25, 2015 at 8:37 PM, Steven Fackler [email protected]
wrote:

In src/libstd/sys/common/unwind/mod.rs
#30557 (comment):

@@ -172,7 +172,7 @@ pub fn panicking() -> bool {
#[inline(never)]
#[no_mangle]
#[allow(private_no_mangle_fns)]

Possibly - not sure how deeply that lint looks. This function is now
public within its module, but not public outside of the crate.


Reply to this email directly or view it on GitHub
https://github.com/rust-lang/rust/pull/30557/files#r48444144.

fn rust_panic(cause: Box<Any + Send + 'static>) -> ! {
pub fn rust_panic(cause: Box<Any + Send + 'static>) -> ! {
unsafe {
imp::panic(cause)
}
Expand Down
35 changes: 35 additions & 0 deletions src/test/run-pass/panic-recover-propagate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

#![feature(std_panic, recover, panic_propagate, panic_handler, const_fn)]

use std::sync::atomic::{AtomicUsize, Ordering};
use std::panic;
use std::thread;

static A: AtomicUsize = AtomicUsize::new(0);

fn main() {
panic::set_handler(|_| {
A.fetch_add(1, Ordering::SeqCst);
});

let result = thread::spawn(|| {
let result = panic::recover(|| {
panic!("hi there");
});

panic::propagate(result.err().unwrap());
}).join();

let msg = *result.err().unwrap().downcast::<&'static str>().unwrap();
assert_eq!("hi there", msg);
assert_eq!(1, A.load(Ordering::SeqCst));
}