Skip to content

Commit 3356d66

Browse files
committed
Inline and remove Bridge::enter.
It has a single callsite.
1 parent 15f5622 commit 3356d66

File tree

1 file changed

+42
-45
lines changed

1 file changed

+42
-45
lines changed

library/proc_macro/src/bridge/client.rs

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -323,27 +323,6 @@ impl Bridge<'_> {
323323
})
324324
}
325325

326-
fn enter<R>(self, f: impl FnOnce() -> R) -> R {
327-
let force_show_panics = self.force_show_panics;
328-
// Hide the default panic output within `proc_macro` expansions.
329-
// NB. the server can't do this because it may use a different libstd.
330-
static HIDE_PANICS_DURING_EXPANSION: Once = Once::new();
331-
HIDE_PANICS_DURING_EXPANSION.call_once(|| {
332-
let prev = panic::take_hook();
333-
panic::set_hook(Box::new(move |info| {
334-
let show = BridgeState::with(|state| match state {
335-
BridgeState::NotConnected => true,
336-
BridgeState::Connected(_) | BridgeState::InUse => force_show_panics,
337-
});
338-
if show {
339-
prev(info)
340-
}
341-
}));
342-
});
343-
344-
BRIDGE_STATE.with(|state| state.set(BridgeState::Connected(self), f))
345-
}
346-
347326
fn with<R>(f: impl FnOnce(&mut Bridge<'_>) -> R) -> R {
348327
BridgeState::with(|state| match state {
349328
BridgeState::NotConnected => {
@@ -388,7 +367,6 @@ impl<I, O> Clone for Client<I, O> {
388367

389368
/// Client-side helper for handling client panics, entering the bridge,
390369
/// deserializing input and serializing output.
391-
// FIXME(eddyb) maybe replace `Bridge::enter` with this?
392370
fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
393371
mut bridge: Bridge<'_>,
394372
f: impl FnOnce(A) -> R,
@@ -397,29 +375,48 @@ fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
397375
let mut buf = bridge.cached_buffer.take();
398376

399377
panic::catch_unwind(panic::AssertUnwindSafe(|| {
400-
bridge.enter(|| {
401-
let reader = &mut &buf[..];
402-
let input = A::decode(reader, &mut ());
403-
404-
// Put the `cached_buffer` back in the `Bridge`, for requests.
405-
Bridge::with(|bridge| bridge.cached_buffer = buf.take());
406-
407-
let output = f(input);
408-
409-
// Take the `cached_buffer` back out, for the output value.
410-
buf = Bridge::with(|bridge| bridge.cached_buffer.take());
411-
412-
// HACK(eddyb) Separate encoding a success value (`Ok(output)`)
413-
// from encoding a panic (`Err(e: PanicMessage)`) to avoid
414-
// having handles outside the `bridge.enter(|| ...)` scope, and
415-
// to catch panics that could happen while encoding the success.
416-
//
417-
// Note that panics should be impossible beyond this point, but
418-
// this is defensively trying to avoid any accidental panicking
419-
// reaching the `extern "C"` (which should `abort` but might not
420-
// at the moment, so this is also potentially preventing UB).
421-
buf.clear();
422-
Ok::<_, ()>(output).encode(&mut buf, &mut ());
378+
let force_show_panics = bridge.force_show_panics;
379+
// Hide the default panic output within `proc_macro` expansions.
380+
// NB. the server can't do this because it may use a different libstd.
381+
static HIDE_PANICS_DURING_EXPANSION: Once = Once::new();
382+
HIDE_PANICS_DURING_EXPANSION.call_once(|| {
383+
let prev = panic::take_hook();
384+
panic::set_hook(Box::new(move |info| {
385+
let show = BridgeState::with(|state| match state {
386+
BridgeState::NotConnected => true,
387+
BridgeState::Connected(_) | BridgeState::InUse => force_show_panics,
388+
});
389+
if show {
390+
prev(info)
391+
}
392+
}));
393+
});
394+
395+
BRIDGE_STATE.with(|state| {
396+
state.set(BridgeState::Connected(bridge), || {
397+
let reader = &mut &buf[..];
398+
let input = A::decode(reader, &mut ());
399+
400+
// Put the `cached_buffer` back in the `Bridge`, for requests.
401+
Bridge::with(|bridge| bridge.cached_buffer = buf.take());
402+
403+
let output = f(input);
404+
405+
// Take the `cached_buffer` back out, for the output value.
406+
buf = Bridge::with(|bridge| bridge.cached_buffer.take());
407+
408+
// HACK(eddyb) Separate encoding a success value (`Ok(output)`)
409+
// from encoding a panic (`Err(e: PanicMessage)`) to avoid
410+
// having handles outside the `bridge.enter(|| ...)` scope, and
411+
// to catch panics that could happen while encoding the success.
412+
//
413+
// Note that panics should be impossible beyond this point, but
414+
// this is defensively trying to avoid any accidental panicking
415+
// reaching the `extern "C"` (which should `abort` but might not
416+
// at the moment, so this is also potentially preventing UB).
417+
buf.clear();
418+
Ok::<_, ()>(output).encode(&mut buf, &mut ());
419+
})
423420
})
424421
}))
425422
.map_err(PanicMessage::from)

0 commit comments

Comments
 (0)