Add force_show_panics flag

This commit is contained in:
Edwin Cheng 2020-12-27 18:00:59 +08:00
parent f3125555a8
commit da92f46cc8
4 changed files with 44 additions and 8 deletions

View file

@ -136,6 +136,7 @@ impl Expander {
&crate::proc_macro::bridge::server::SameThread, &crate::proc_macro::bridge::server::SameThread,
crate::rustc_server::Rustc::default(), crate::rustc_server::Rustc::default(),
parsed_body, parsed_body,
false,
); );
return res.map(|it| it.subtree); return res.map(|it| it.subtree);
} }
@ -144,6 +145,7 @@ impl Expander {
&crate::proc_macro::bridge::server::SameThread, &crate::proc_macro::bridge::server::SameThread,
crate::rustc_server::Rustc::default(), crate::rustc_server::Rustc::default(),
parsed_body, parsed_body,
false,
); );
return res.map(|it| it.subtree); return res.map(|it| it.subtree);
} }
@ -153,6 +155,7 @@ impl Expander {
crate::rustc_server::Rustc::default(), crate::rustc_server::Rustc::default(),
parsed_attributes, parsed_attributes,
parsed_body, parsed_body,
false,
); );
return res.map(|it| it.subtree); return res.map(|it| it.subtree);
} }

View file

@ -303,17 +303,21 @@ impl BridgeState<'_> {
impl Bridge<'_> { impl Bridge<'_> {
fn enter<R>(self, f: impl FnOnce() -> R) -> R { fn enter<R>(self, f: impl FnOnce() -> R) -> R {
let force_show_panics = self.force_show_panics;
// Hide the default panic output within `proc_macro` expansions. // Hide the default panic output within `proc_macro` expansions.
// NB. the server can't do this because it may use a different libstd. // NB. the server can't do this because it may use a different libstd.
static HIDE_PANICS_DURING_EXPANSION: Once = Once::new(); static HIDE_PANICS_DURING_EXPANSION: Once = Once::new();
HIDE_PANICS_DURING_EXPANSION.call_once(|| { HIDE_PANICS_DURING_EXPANSION.call_once(|| {
let prev = panic::take_hook(); let prev = panic::take_hook();
panic::set_hook(Box::new(move |info| { panic::set_hook(Box::new(move |info| {
let hide = BridgeState::with(|state| match state { let show = BridgeState::with(|state| match state {
BridgeState::NotConnected => false, BridgeState::NotConnected => true,
BridgeState::Connected(_) | BridgeState::InUse => true, // Something weird is going on, so don't suppress any backtraces
BridgeState::InUse => true,
BridgeState::Connected(bridge) => force_show_panics,
}); });
if !hide { if show {
prev(info) prev(info)
} }
})); }));

View file

@ -225,6 +225,9 @@ pub struct Bridge<'a> {
/// Server-side function that the client uses to make requests. /// Server-side function that the client uses to make requests.
dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>, dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>,
/// If 'true', always invoke the default panic hook
force_show_panics: bool,
} }
// impl<'a> !Sync for Bridge<'a> {} // impl<'a> !Sync for Bridge<'a> {}

View file

@ -138,6 +138,7 @@ pub trait ExecutionStrategy {
input: Buffer<u8>, input: Buffer<u8>,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
client_data: D, client_data: D,
force_show_panics: bool,
) -> Buffer<u8>; ) -> Buffer<u8>;
} }
@ -150,10 +151,14 @@ impl ExecutionStrategy for SameThread {
input: Buffer<u8>, input: Buffer<u8>,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
client_data: D, client_data: D,
force_show_panics: bool,
) -> Buffer<u8> { ) -> Buffer<u8> {
let mut dispatch = |b| dispatcher.dispatch(b); let mut dispatch = |b| dispatcher.dispatch(b);
run_client(Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, client_data) run_client(
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into(), force_show_panics },
client_data,
)
} }
} }
@ -169,6 +174,7 @@ impl ExecutionStrategy for CrossThread1 {
input: Buffer<u8>, input: Buffer<u8>,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
client_data: D, client_data: D,
force_show_panics: bool,
) -> Buffer<u8> { ) -> Buffer<u8> {
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
@ -182,7 +188,11 @@ impl ExecutionStrategy for CrossThread1 {
}; };
run_client( run_client(
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, Bridge {
cached_buffer: input,
dispatch: (&mut dispatch).into(),
force_show_panics,
},
client_data, client_data,
) )
}); });
@ -204,6 +214,7 @@ impl ExecutionStrategy for CrossThread2 {
input: Buffer<u8>, input: Buffer<u8>,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
client_data: D, client_data: D,
force_show_panics: bool,
) -> Buffer<u8> { ) -> Buffer<u8> {
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -229,7 +240,11 @@ impl ExecutionStrategy for CrossThread2 {
}; };
let r = run_client( let r = run_client(
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, Bridge {
cached_buffer: input,
dispatch: (&mut dispatch).into(),
force_show_panics,
},
client_data, client_data,
); );
@ -268,6 +283,7 @@ fn run_server<
input: I, input: I,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
client_data: D, client_data: D,
force_show_panics: bool,
) -> Result<O, PanicMessage> { ) -> Result<O, PanicMessage> {
let mut dispatcher = let mut dispatcher =
Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) }; Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) };
@ -275,7 +291,13 @@ fn run_server<
let mut b = Buffer::new(); let mut b = Buffer::new();
input.encode(&mut b, &mut dispatcher.handle_store); input.encode(&mut b, &mut dispatcher.handle_store);
b = strategy.run_bridge_and_client(&mut dispatcher, b, run_client, client_data); b = strategy.run_bridge_and_client(
&mut dispatcher,
b,
run_client,
client_data,
force_show_panics,
);
Result::decode(&mut &b[..], &mut dispatcher.handle_store) Result::decode(&mut &b[..], &mut dispatcher.handle_store)
} }
@ -286,6 +308,7 @@ impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
strategy: &impl ExecutionStrategy, strategy: &impl ExecutionStrategy,
server: S, server: S,
input: S::TokenStream, input: S::TokenStream,
force_show_panics: bool,
) -> Result<S::TokenStream, PanicMessage> { ) -> Result<S::TokenStream, PanicMessage> {
let client::Client { get_handle_counters, run, f } = *self; let client::Client { get_handle_counters, run, f } = *self;
run_server( run_server(
@ -295,6 +318,7 @@ impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
<MarkedTypes<S> as Types>::TokenStream::mark(input), <MarkedTypes<S> as Types>::TokenStream::mark(input),
run, run,
f, f,
force_show_panics,
) )
.map(<MarkedTypes<S> as Types>::TokenStream::unmark) .map(<MarkedTypes<S> as Types>::TokenStream::unmark)
} }
@ -307,6 +331,7 @@ impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenSt
server: S, server: S,
input: S::TokenStream, input: S::TokenStream,
input2: S::TokenStream, input2: S::TokenStream,
force_show_panics: bool,
) -> Result<S::TokenStream, PanicMessage> { ) -> Result<S::TokenStream, PanicMessage> {
let client::Client { get_handle_counters, run, f } = *self; let client::Client { get_handle_counters, run, f } = *self;
run_server( run_server(
@ -319,6 +344,7 @@ impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenSt
), ),
run, run,
f, f,
force_show_panics,
) )
.map(<MarkedTypes<S> as Types>::TokenStream::unmark) .map(<MarkedTypes<S> as Types>::TokenStream::unmark)
} }