flux_common/
bug.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::{
    cell::Cell,
    fmt,
    panic::{Location, UnwindSafe},
};

use flux_config as config;
use rustc_errors::{ExplicitBug, MultiSpan};
use rustc_middle::ty::tls;
use rustc_span::{ErrorGuaranteed, Span};

thread_local! {
    static TRACKED_SPAN: Cell<Option<Span>> = const { Cell::new(None) };
}

pub fn track_span<R>(span: Span, f: impl FnOnce() -> R) -> R {
    TRACKED_SPAN.with(|cell| {
        if span.is_dummy() {
            return f();
        }
        let old = cell.replace(Some(span));
        let r = f();
        cell.set(old);
        r
    })
}

#[macro_export]
macro_rules! tracked_span_dbg_assert_eq {
    ($($arg:tt)*) => {
        if core::cfg!(debug_assertions) {
            $crate::tracked_span_assert_eq!($($arg)*);
        }
    };
}

#[macro_export]
macro_rules! tracked_span_assert_eq {
    ($left:expr, $right:expr $(,)?) => {
        match (&$left, &$right) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    $crate::tracked_span_bug!(
                        "assertion `left == right` failed\n  left: {left_val:?}\n right: {right_val:?}"
                    )
                }
            }
        }
    };
}

#[macro_export]
macro_rules! tracked_span_bug {
    () => ( $crate::tracked_span_bug!("impossible case reached") );
    ($msg:expr) => ({ $crate::bug::tracked_span_bug_fmt(::std::format_args!($msg)) });
    ($msg:expr,) => ({ $crate::tracked_span_bug!($msg) });
    ($fmt:expr, $($arg:tt)+) => ({
        $crate::bug::tracked_span_bug_fmt(::std::format_args!($fmt, $($arg)+))
    });
}

#[macro_export]
macro_rules! bug {
    () => ( $crate::bug!("impossible case reached") );
    ($msg:expr) => ({ $crate::bug::bug_fmt(::std::format_args!($msg)) });
    ($msg:expr,) => ({ $crate::bug!($msg) });
    ($fmt:expr, $($arg:tt)+) => ({
        $crate::bug::bug_fmt(::std::format_args!($fmt, $($arg)+))
    });
}

#[macro_export]
macro_rules! span_bug {
    ($span:expr, $msg:expr) => ({ $crate::bug::span_bug_fmt($span, ::std::format_args!($msg)) });
    ($span:expr, $msg:expr,) => ({ $crate::span_bug!($span, $msg) });
    ($span:expr, $fmt:expr, $($arg:tt)+) => ({
        $crate::bug::span_bug_fmt($span, ::std::format_args!($fmt, $($arg)+))
    });
}

#[track_caller]
pub fn bug_fmt(args: fmt::Arguments<'_>) -> ! {
    opt_span_bug_fmt(None::<Span>, args, Location::caller());
}

#[track_caller]
pub fn span_bug_fmt<S: Into<MultiSpan>>(span: S, args: fmt::Arguments<'_>) -> ! {
    opt_span_bug_fmt(Some(span), args, Location::caller());
}

#[track_caller]
pub fn tracked_span_bug_fmt(args: fmt::Arguments<'_>) -> ! {
    let location = Location::caller();
    opt_span_bug_fmt(TRACKED_SPAN.get(), args, location);
}

#[track_caller]
fn opt_span_bug_fmt<S: Into<MultiSpan>>(
    span: Option<S>,
    args: fmt::Arguments<'_>,
    location: &'static Location<'static>,
) -> ! {
    tls::with_opt(
        #[track_caller]
        move |tcx| {
            let msg = format!("{location}: {args}");
            match (tcx, span) {
                (Some(tcx), Some(span)) => tcx.dcx().span_bug(span, msg),
                (Some(tcx), None) => tcx.dcx().bug(msg),
                (None, _) => std::panic::panic_any(msg),
            }
        },
    )
}

pub fn catch_bugs<R>(msg: &str, f: impl FnOnce() -> R + UnwindSafe) -> Result<R, ErrorGuaranteed> {
    if config::catch_bugs() {
        match std::panic::catch_unwind(f) {
            Ok(v) => Ok(v),
            Err(payload) => {
                tls::with_opt(move |tcx| {
                    let Some(tcx) = tcx else { std::panic::resume_unwind(payload) };

                    if payload.is::<ExplicitBug>() {
                        eprintln!("note: bug caught [{msg}]\n");
                        Err(tcx.dcx().delayed_bug("bug wasn't reported"))
                    } else {
                        eprintln!("note: uncaught panic [{msg}]\n");
                        std::panic::resume_unwind(payload)
                    }
                })
            }
        }
    } else {
        Ok(f())
    }
}