tamer: diagnose::panic::diagnostic_unreachable!: New macro

This is a diagnostic replacement for `unreachable!`.

Eventually TAMER'll have build-time checks to enforce the use of these over
alternatives; I need to survey the old instances on a case-by-case basis to
see what diagnostic information can be reasonably presented in that context.

DEV-13209
main
Mike Gerwitz 2022-11-09 10:47:17 -05:00
parent 5c5041f90e
commit 6ae6ca716c
1 changed files with 23 additions and 3 deletions

View File

@ -120,7 +120,13 @@ impl<'a> Diagnostic for DiagnosticDesc<'a> {
/// problem and getting themselves unstuck.
#[macro_export]
macro_rules! diagnostic_panic {
($desc_data:expr, $($panic_args:tt)*) => {{
($desc_data:expr, $($panic_args:tt)*) => {
$crate::diagnostic_panic!(
@panic!, $desc_data, $($panic_args)*
)
};
(@$macro:ident!, $desc_data:expr, $($panic_args:tt)*) => {{
use crate::diagnose::Reporter;
let mut reporter = crate::diagnose::panic::PanicReporter::new(
@ -133,7 +139,7 @@ macro_rules! diagnostic_panic {
std::cell::Cell::new($desc_data),
);
panic!(
$macro!(
"internal error:\n{}\n{}",
reporter.render(&desc),
// Be extra obnoxious.
@ -157,7 +163,7 @@ macro_rules! diagnostic_panic {
!!! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!!
\x1b[0m"
)
}}
}};
}
/// Produce a panic with diagnostic information and a rather obnoxious
@ -174,6 +180,20 @@ macro_rules! debug_diagnostic_panic {
}
}
/// Produce a panic using [`unreachable!`] with diagnostic information and a
/// rather obnoxious message describing this issue as a bug in TAMER.
///
/// This should be used in place of [`diagnostic_panic`] wherever
/// [`unreachable!`] would be used.
#[macro_export]
macro_rules! diagnostic_unreachable {
($desc_data:expr, $($panic_args:tt)*) => {
$crate::diagnostic_panic!(
@unreachable!, $desc_data, $($panic_args)*
)
}
}
/// Alternatives to `unwrap` and `expect` that utilize
/// [`diagnostic_panic!`].
pub trait DiagnosticPanic {