1
//! The errors of a read or call of a global whose value did not survive its
2
//! unit (plan 0014 PERSIST).
3

            
4
use crate::error::Error;
5

            
6
use super::entry::Symbol;
7
use super::table::SymbolTable;
8

            
9
impl SymbolTable {
10
    /// The error of a read of a global whose value did not survive its unit.
11
    #[must_use]
12
211152
    pub(crate) fn explain_expired(&self, err: Error) -> Error {
13
211152
        self.lost_read(&read_name(&err)).unwrap_or(err)
14
211152
    }
15

            
16
    /// The error of a call of `name`, a bound symbol that is no function: a
17
    /// variable holding a function has no function cell, a rebound one is not
18
    /// callable, else the lost value of a global, else the generic one.
19
    #[must_use]
20
2947
    pub(crate) fn not_callable(&self, name: &str, tail: &str) -> Error {
21
1249
        let generic = || Error::Compile(format!("symbol '{name}' is not callable{tail}"));
22
2947
        match self.lookup(name).and_then(Symbol::value) {
23
2945
            Some(value) if value.is_function_value() => Error::Compile(format!(
24
1696
                "undefined function {name}; use (funcall {} …)",
25
1696
                name.to_lowercase()
26
1696
            )),
27
1249
            Some(_) => generic(),
28
2
            None => self.lost_read(name).unwrap_or_else(generic),
29
        }
30
2947
    }
31

            
32
218278
    pub(crate) fn lost_read(&self, read: &str) -> Option<Error> {
33
218278
        if let Some(type_name) = self.lost_homes.get(read) {
34
3537
            return Some(Error::Compile(format!(
35
3537
                "{read}: its {type_name} value does not survive the form"
36
3537
            )));
37
214741
        }
38
214741
        self.is_expired(read).then(|| {
39
4
            Error::Compile(format!(
40
4
                "{read}: its runtime value does not survive the form"
41
4
            ))
42
4
        })
43
218278
    }
44
}
45

            
46
/// The global a failed read names.
47
211152
fn read_name(err: &Error) -> String {
48
211152
    let named = match err {
49
30485
        Error::UndefinedSymbol(name) => return name.clone(),
50
146375
        Error::Compile(msg) => msg.strip_prefix("symbol '"),
51
34292
        _ => None,
52
    };
53
180667
    named
54
4166
        .and_then(|rest| {
55
4166
            let (name, tail) = rest.split_once('\'')?;
56
4166
            (tail == " has no value").then_some(name)
57
4166
        })
58
        .unwrap_or_default()
59
        .to_string()
60
211152
}