Lines
100 %
Functions
92.86 %
Branches
//! The errors of a read or call of a global whose value did not survive its
//! unit (plan 0014 PERSIST).
use crate::error::Error;
use super::entry::Symbol;
use super::table::SymbolTable;
impl SymbolTable {
/// The error of a read of a global whose value did not survive its unit.
#[must_use]
pub(crate) fn explain_expired(&self, err: Error) -> Error {
self.lost_read(&read_name(&err)).unwrap_or(err)
}
/// The error of a call of `name`, a bound symbol that is no function: a
/// variable holding a function has no function cell, a rebound one is not
/// callable, else the lost value of a global, else the generic one.
pub(crate) fn not_callable(&self, name: &str, tail: &str) -> Error {
let generic = || Error::Compile(format!("symbol '{name}' is not callable{tail}"));
match self.lookup(name).and_then(Symbol::value) {
Some(value) if value.is_function_value() => Error::Compile(format!(
"undefined function {name}; use (funcall {} …)",
name.to_lowercase()
)),
Some(_) => generic(),
None => self.lost_read(name).unwrap_or_else(generic),
pub(crate) fn lost_read(&self, read: &str) -> Option<Error> {
if let Some(type_name) = self.lost_homes.get(read) {
return Some(Error::Compile(format!(
"{read}: its {type_name} value does not survive the form"
)));
self.is_expired(read).then(|| {
Error::Compile(format!(
"{read}: its runtime value does not survive the form"
))
})
/// The global a failed read names.
fn read_name(err: &Error) -> String {
let named = match err {
Error::UndefinedSymbol(name) => return name.clone(),
Error::Compile(msg) => msg.strip_prefix("symbol '"),
_ => None,
};
named
.and_then(|rest| {
let (name, tail) = rest.split_once('\'')?;
(tail == " has no value").then_some(name)
.unwrap_or_default()
.to_string()