Lines
78.95 %
Functions
77.78 %
Branches
100 %
//! Whether a form calls a function that does not outlive its unit.
use super::super::setf_scan::code_forms;
use super::shared::labels_names;
use crate::ast::Expr;
use crate::runtime::SymbolTable;
/// Whether the macro-expanded `body` calls, or takes with FUNCTION, a LABELS
/// function of the enclosing code or a user macro that could not be expanded.
/// A name in a variable position or in quoted data is no call.
pub(in crate::compiler) fn calls_form_bound(symbols: &SymbolTable, body: &Expr) -> bool {
calls(symbols, body, &[])
}
fn calls(symbols: &SymbolTable, expr: &Expr, defined: &[String]) -> bool {
match expr {
Expr::List(elems) => list_calls(symbols, elems, defined),
Expr::Cons(car, cdr) => calls(symbols, car, defined) || calls(symbols, cdr, defined),
Expr::Lambda(_, body) => calls(symbols, body, defined),
Expr::Quasiquote(template) => template
.unquoted_forms()
.into_iter()
.any(|form| calls(symbols, form, defined)),
_ => false,
fn list_calls(symbols: &SymbolTable, elems: &[Expr], defined: &[String]) -> bool {
let Some((Expr::Symbol(head), args)) = elems.split_first() else {
return elems.iter().any(|elem| calls(symbols, elem, defined));
};
let bound = |name: &str| {
symbols.is_form_bound(name) && !defined.iter().any(|d| d.eq_ignore_ascii_case(name))
if head.eq_ignore_ascii_case("QUOTE") {
return false;
if bound(head) {
return true;
if head.eq_ignore_ascii_case("FUNCTION") {
return match args.first() {
Some(Expr::Symbol(name)) => bound(name),
Some(other) => calls(symbols, other, defined),
None => false,
if head.eq_ignore_ascii_case("LABELS") {
let inner: Vec<String> = defined.iter().cloned().chain(labels_names(args)).collect();
return code_forms(elems).any(|form| calls(symbols, form, &inner));
code_forms(elems).any(|form| calls(symbols, form, defined))
#[cfg(test)]
#[path = "form_bound_tests.rs"]
mod tests;