Skip to content

Add force_show_panics flag for proc-macro bridge #7047

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 27, 2020
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
3 changes: 3 additions & 0 deletions crates/proc_macro_srv/src/dylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl Expander {
&crate::proc_macro::bridge::server::SameThread,
crate::rustc_server::Rustc::default(),
parsed_body,
false,
);
return res.map(|it| it.subtree);
}
Expand All @@ -144,6 +145,7 @@ impl Expander {
&crate::proc_macro::bridge::server::SameThread,
crate::rustc_server::Rustc::default(),
parsed_body,
false,
);
return res.map(|it| it.subtree);
}
Expand All @@ -153,6 +155,7 @@ impl Expander {
crate::rustc_server::Rustc::default(),
parsed_attributes,
parsed_body,
false,
);
return res.map(|it| it.subtree);
}
Expand Down
12 changes: 8 additions & 4 deletions crates/proc_macro_srv/src/proc_macro/bridge/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,21 @@ impl BridgeState<'_> {

impl Bridge<'_> {
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.
// NB. the server can't do this because it may use a different libstd.
static HIDE_PANICS_DURING_EXPANSION: Once = Once::new();
HIDE_PANICS_DURING_EXPANSION.call_once(|| {
let prev = panic::take_hook();
panic::set_hook(Box::new(move |info| {
let hide = BridgeState::with(|state| match state {
BridgeState::NotConnected => false,
BridgeState::Connected(_) | BridgeState::InUse => true,
let show = BridgeState::with(|state| match state {
BridgeState::NotConnected => 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)
}
}));
Expand Down
3 changes: 3 additions & 0 deletions crates/proc_macro_srv/src/proc_macro/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ pub struct Bridge<'a> {

/// Server-side function that the client uses to make requests.
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> {}
Expand Down
34 changes: 30 additions & 4 deletions crates/proc_macro_srv/src/proc_macro/bridge/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub trait ExecutionStrategy {
input: Buffer<u8>,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
client_data: D,
force_show_panics: bool,
) -> Buffer<u8>;
}

Expand All @@ -150,10 +151,14 @@ impl ExecutionStrategy for SameThread {
input: Buffer<u8>,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
client_data: D,
force_show_panics: bool,
) -> Buffer<u8> {
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,
)
}
}

Expand All @@ -169,6 +174,7 @@ impl ExecutionStrategy for CrossThread1 {
input: Buffer<u8>,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
client_data: D,
force_show_panics: bool,
) -> Buffer<u8> {
use std::sync::mpsc::channel;

Expand All @@ -182,7 +188,11 @@ impl ExecutionStrategy for CrossThread1 {
};

run_client(
Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() },
Bridge {
cached_buffer: input,
dispatch: (&mut dispatch).into(),
force_show_panics,
},
client_data,
)
});
Expand All @@ -204,6 +214,7 @@ impl ExecutionStrategy for CrossThread2 {
input: Buffer<u8>,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
client_data: D,
force_show_panics: bool,
) -> Buffer<u8> {
use std::sync::{Arc, Mutex};

Expand All @@ -229,7 +240,11 @@ impl ExecutionStrategy for CrossThread2 {
};

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,
);

Expand Down Expand Up @@ -268,14 +283,21 @@ fn run_server<
input: I,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
client_data: D,
force_show_panics: bool,
) -> Result<O, PanicMessage> {
let mut dispatcher =
Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) };

let mut b = Buffer::new();
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)
}
Expand All @@ -286,6 +308,7 @@ impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
strategy: &impl ExecutionStrategy,
server: S,
input: S::TokenStream,
force_show_panics: bool,
) -> Result<S::TokenStream, PanicMessage> {
let client::Client { get_handle_counters, run, f } = *self;
run_server(
Expand All @@ -295,6 +318,7 @@ impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
<MarkedTypes<S> as Types>::TokenStream::mark(input),
run,
f,
force_show_panics,
)
.map(<MarkedTypes<S> as Types>::TokenStream::unmark)
}
Expand All @@ -307,6 +331,7 @@ impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenSt
server: S,
input: S::TokenStream,
input2: S::TokenStream,
force_show_panics: bool,
) -> Result<S::TokenStream, PanicMessage> {
let client::Client { get_handle_counters, run, f } = *self;
run_server(
Expand All @@ -319,6 +344,7 @@ impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenSt
),
run,
f,
force_show_panics,
)
.map(<MarkedTypes<S> as Types>::TokenStream::unmark)
}
Expand Down