Skip to content

Commit c405f97

Browse files
pvdrzqsdrqs
authored andcommitted
Use panic hooks instead of using catch_unwind
One of the advantages of doing this is that `ParseCallbacks` no longer needs to implement `UnwindSafe` which means that users can rely on `RefCell` and `Cell` to extract information from the callbacks. Users relying on `catch_unwind` can still achieve similar behavior using `std::thread::spawn`. Fixes rust-lang#2147.
1 parent 154fc11 commit c405f97

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

bindgen-cli/main.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ extern crate env_logger;
66
extern crate log;
77

88
use std::env;
9-
use std::panic;
109

1110
mod options;
1211
use crate::options::builder_from_flags;
@@ -41,18 +40,18 @@ pub fn main() {
4140
Ok((builder, output, verbose)) => {
4241
#[cfg(feature = "logging")]
4342
clang_version_check();
44-
let builder_result = panic::catch_unwind(|| {
45-
builder.generate().expect("Unable to generate bindings")
46-
});
4743

48-
if builder_result.is_err() {
44+
std::panic::set_hook(Box::new(move |_info| {
4945
if verbose {
50-
print_verbose_err();
46+
print_verbose_err()
5147
}
52-
std::process::exit(1);
53-
}
48+
}));
49+
50+
let bindings =
51+
builder.generate().expect("Unable to generate bindings");
52+
53+
let _ = std::panic::take_hook();
5454

55-
let bindings = builder_result.unwrap();
5655
bindings.write(output).expect("Unable to write output");
5756
}
5857
Err(error) => {

bindgen/callbacks.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pub use crate::ir::derive::CanDerive as ImplementsTrait;
55
pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue};
66
pub use crate::ir::int::IntKind;
77
use std::fmt;
8-
use std::panic::UnwindSafe;
98

109
/// An enum to allow ignoring parsing of macros.
1110
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -25,7 +24,7 @@ impl Default for MacroParsingBehavior {
2524

2625
/// A trait to allow configuring different kinds of types in different
2726
/// situations.
28-
pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
27+
pub trait ParseCallbacks: fmt::Debug {
2928
/// This function will be run on every macro that is identified.
3029
fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
3130
MacroParsingBehavior::Default

bindgen/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,11 +2128,6 @@ struct BindgenOptions {
21282128
merge_extern_blocks: bool,
21292129
}
21302130

2131-
/// TODO(emilio): This is sort of a lie (see the error message that results from
2132-
/// removing this), but since we don't share references across panic boundaries
2133-
/// it's ok.
2134-
impl ::std::panic::UnwindSafe for BindgenOptions {}
2135-
21362131
impl BindgenOptions {
21372132
fn build(&mut self) {
21382133
let mut regex_sets = [

0 commit comments

Comments
 (0)