diff options
| author | Lukas Wirth <lukastw97@gmail.com> | 2025-02-12 13:41:11 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-12 13:41:11 +0000 |
| commit | 53e687058c78b737accabc736cf469d63c7ff7cf (patch) | |
| tree | 8873a3b9277cb72c811959173936b28bb0a6c7b1 | |
| parent | 815919578b3edcc82f1f43b94705a392e1f15390 (diff) | |
| parent | 01794a4973c6e9b41af78cebd2f8adf9cdb36f9f (diff) | |
| download | rust-53e687058c78b737accabc736cf469d63c7ff7cf.tar.gz rust-53e687058c78b737accabc736cf469d63c7ff7cf.zip | |
Merge pull request #19110 from eagr/panic-context
Simplify panic_context
| -rw-r--r-- | src/tools/rust-analyzer/crates/stdx/src/panic_context.rs | 40 |
1 files changed, 18 insertions, 22 deletions
diff --git a/src/tools/rust-analyzer/crates/stdx/src/panic_context.rs b/src/tools/rust-analyzer/crates/stdx/src/panic_context.rs index 4ec74c0742a..a35d50b78df 100644 --- a/src/tools/rust-analyzer/crates/stdx/src/panic_context.rs +++ b/src/tools/rust-analyzer/crates/stdx/src/panic_context.rs @@ -1,28 +1,25 @@ //! A micro-crate to enhance panic messages with context info. -//! -//! FIXME: upstream to <https://github.com/kriomant/panic-context> ? use std::{cell::RefCell, panic, sync::Once}; -pub fn enter(context: String) -> PanicContext { - static ONCE: Once = Once::new(); - ONCE.call_once(PanicContext::init); - - with_ctx(|ctx| ctx.push(context)); - PanicContext { _priv: () } -} - +/// Dummy for leveraging RAII cleanup to pop frames. #[must_use] pub struct PanicContext { + // prevent arbitrary construction _priv: (), } -impl PanicContext { +impl Drop for PanicContext { + fn drop(&mut self) { + with_ctx(|ctx| assert!(ctx.pop().is_some())); + } +} + +pub fn enter(frame: String) -> PanicContext { #[allow(clippy::print_stderr)] - fn init() { + fn set_hook() { let default_hook = panic::take_hook(); - #[allow(deprecated)] - let hook = move |panic_info: &panic::PanicInfo<'_>| { + panic::set_hook(Box::new(move |panic_info| { with_ctx(|ctx| { if !ctx.is_empty() { eprintln!("Panic context:"); @@ -30,17 +27,16 @@ impl PanicContext { eprintln!("> {frame}\n"); } } - default_hook(panic_info); }); - }; - panic::set_hook(Box::new(hook)); + default_hook(panic_info); + })); } -} -impl Drop for PanicContext { - fn drop(&mut self) { - with_ctx(|ctx| assert!(ctx.pop().is_some())); - } + static SET_HOOK: Once = Once::new(); + SET_HOOK.call_once(set_hook); + + with_ctx(|ctx| ctx.push(frame)); + PanicContext { _priv: () } } fn with_ctx(f: impl FnOnce(&mut Vec<String>)) { |
