mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Merge pull request #19110 from eagr/panic-context
Simplify panic_context
This commit is contained in:
commit
aa64a6da0e
@ -1,48 +1,44 @@
|
|||||||
//! A micro-crate to enhance panic messages with context info.
|
//! 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};
|
use std::{cell::RefCell, panic, sync::Once};
|
||||||
|
|
||||||
pub fn enter(context: String) -> PanicContext {
|
/// Dummy for leveraging RAII cleanup to pop frames.
|
||||||
static ONCE: Once = Once::new();
|
|
||||||
ONCE.call_once(PanicContext::init);
|
|
||||||
|
|
||||||
with_ctx(|ctx| ctx.push(context));
|
|
||||||
PanicContext { _priv: () }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub struct PanicContext {
|
pub struct PanicContext {
|
||||||
|
// prevent arbitrary construction
|
||||||
_priv: (),
|
_priv: (),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PanicContext {
|
|
||||||
#[allow(clippy::print_stderr)]
|
|
||||||
fn init() {
|
|
||||||
let default_hook = panic::take_hook();
|
|
||||||
#[allow(deprecated)]
|
|
||||||
let hook = move |panic_info: &panic::PanicInfo<'_>| {
|
|
||||||
with_ctx(|ctx| {
|
|
||||||
if !ctx.is_empty() {
|
|
||||||
eprintln!("Panic context:");
|
|
||||||
for frame in ctx.iter() {
|
|
||||||
eprintln!("> {frame}\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default_hook(panic_info);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
panic::set_hook(Box::new(hook));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for PanicContext {
|
impl Drop for PanicContext {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
with_ctx(|ctx| assert!(ctx.pop().is_some()));
|
with_ctx(|ctx| assert!(ctx.pop().is_some()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn enter(frame: String) -> PanicContext {
|
||||||
|
#[allow(clippy::print_stderr)]
|
||||||
|
fn set_hook() {
|
||||||
|
let default_hook = panic::take_hook();
|
||||||
|
panic::set_hook(Box::new(move |panic_info| {
|
||||||
|
with_ctx(|ctx| {
|
||||||
|
if !ctx.is_empty() {
|
||||||
|
eprintln!("Panic context:");
|
||||||
|
for frame in ctx.iter() {
|
||||||
|
eprintln!("> {frame}\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
default_hook(panic_info);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
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>)) {
|
fn with_ctx(f: impl FnOnce(&mut Vec<String>)) {
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static CTX: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
|
static CTX: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user