Skip to main content

leanfran_runtime/
panic.rs

1//! Panics (`lean_panic_fn`, internal panics) and `dbgTrace`, with the messages of the C runtime.
2
3use crate::closure::lean_apply_1;
4use crate::object::{lean_box, lean_dec, lean_inc, Obj};
5use crate::string::lean_string_bytes;
6use std::cell::Cell;
7use std::io::Write;
8
9thread_local! {
10    static EXIT_ON_PANIC: Cell<bool> = const { Cell::new(false) };
11    static PANIC_MESSAGES: Cell<bool> = const { Cell::new(true) };
12}
13
14fn eprint_line(bytes: &[u8]) {
15    let mut e = std::io::stderr().lock();
16    let _ = e.write_all(bytes);
17    let _ = e.write_all(b"\n");
18    let _ = e.flush();
19}
20
21fn abort_on_panic() {
22    if std::env::var_os("LEAN_ABORT_ON_PANIC").is_some() {
23        std::process::abort();
24    }
25}
26
27/// `lean_set_exit_on_panic`: exit with code 1 after a panic message.
28pub unsafe fn lean_set_exit_on_panic(flag: bool) {
29    EXIT_ON_PANIC.set(flag);
30}
31
32/// `lean_internal_set_exit_on_panic : Bool → BaseIO Unit`.
33pub unsafe fn lean_internal_set_exit_on_panic(flag: u8) -> Obj {
34    EXIT_ON_PANIC.set(flag != 0);
35    lean_box(0)
36}
37
38/// `lean_set_panic_messages`: print panic messages or not.
39pub unsafe fn lean_set_panic_messages(flag: bool) {
40    PANIC_MESSAGES.set(flag);
41}
42
43fn panic_impl(msg: &[u8]) {
44    if PANIC_MESSAGES.get() {
45        eprint_line(msg);
46    }
47    abort_on_panic();
48    if EXIT_ON_PANIC.get() {
49        std::process::exit(1);
50    }
51}
52
53/// `lean_panic`: prints `msg` to stderr (and exits if `setExitOnPanic` was set).
54pub unsafe fn lean_panic(msg: &str, _force_stderr: bool) {
55    panic_impl(msg.as_bytes());
56}
57
58/// `panicCore`: prints the message `msg` (owned) and returns `default_val` (owned).
59pub unsafe fn lean_panic_fn(default_val: Obj, msg: Obj) -> Obj {
60    panic_impl(lean_string_bytes(msg));
61    lean_dec(msg);
62    default_val
63}
64
65/// `lean_panic_fn` with a borrowed default value.
66pub unsafe fn lean_panic_fn_borrowed(default_val: Obj, msg: Obj) -> Obj {
67    lean_inc(default_val);
68    lean_panic_fn(default_val, msg)
69}
70
71/// Prints `INTERNAL PANIC: msg` and exits with code 1.
72#[cold]
73pub fn lean_internal_panic(msg: &str) -> ! {
74    eprint_line(format!("INTERNAL PANIC: {msg}").as_bytes());
75    abort_on_panic();
76    std::process::exit(1)
77}
78
79#[cold]
80pub fn lean_internal_panic_out_of_memory() -> ! {
81    lean_internal_panic("out of memory")
82}
83
84#[cold]
85pub fn lean_internal_panic_unreachable() -> ! {
86    lean_internal_panic("unreachable code has been reached")
87}
88
89#[cold]
90pub fn lean_internal_panic_rc_overflow() -> ! {
91    lean_internal_panic("reference counter overflowed")
92}
93
94#[cold]
95pub fn lean_internal_panic_overflow() -> ! {
96    lean_internal_panic("integer overflow in runtime computation")
97}
98
99/// `sorry` at run time.
100pub unsafe fn lean_sorry(_dummy: u8) -> Obj {
101    lean_internal_panic("executed 'sorry'")
102}
103
104/// `dbgTrace`: prints `s` (owned) to stderr and applies `fn` (owned) to `()`.
105pub unsafe fn lean_dbg_trace(s: Obj, f: Obj) -> Obj {
106    eprint_line(lean_string_bytes(s));
107    lean_dec(s);
108    lean_apply_1(f, lean_box(0))
109}
110
111/// `dbgSleep`: sleeps `ms` milliseconds (not on a target without an OS) and applies `fn`
112/// (owned) to `()`.
113pub unsafe fn lean_dbg_sleep(ms: u32, f: Obj) -> Obj {
114    #[cfg(unix)]
115    std::thread::sleep(std::time::Duration::from_millis(ms as u64));
116    #[cfg(not(unix))]
117    let _ = ms;
118    lean_apply_1(f, lean_box(0))
119}
120
121/// `dbgTraceIfShared`: prints `shared RC s` when `a` is shared; returns `a`.
122pub unsafe fn lean_dbg_trace_if_shared(s: Obj, a: Obj) -> Obj {
123    if !crate::object::lean_is_scalar(a) && !crate::object::lean_is_exclusive(a) {
124        let mut line = b"shared RC ".to_vec();
125        line.extend_from_slice(lean_string_bytes(s));
126        eprint_line(&line);
127    }
128    a
129}
130
131/// `dbgStackTrace`: no stack traces are available; applies `fn` (owned) to `()`.
132pub unsafe fn lean_dbg_stack_trace(f: Obj) -> Obj {
133    lean_apply_1(f, lean_box(0))
134}