1
//! The lexical bindings a `DEFUN` keeps past the binder that made them.
2

            
3
use crate::ast::Expr;
4
use crate::error::Result;
5
use crate::runtime::{Symbol, SymbolTable};
6

            
7
use super::shared::{assigns_through_macros, reads_free};
8

            
9
/// The lexical bindings in scope that `function` reads and never assigns: a
10
/// store would have to reach the binding's one shared place.
11
5269213
pub(super) fn kept_lexicals(symbols: &SymbolTable, function: &Expr) -> Result<Vec<Symbol>> {
12
5269213
    let Expr::Lambda(_, body) = function else {
13
        return Ok(Vec::new());
14
    };
15
5269213
    let mut kept = Vec::new();
16
5269213
    for binding in symbols.lexical_bindings() {
17
12970
        if reads_free(symbols, function, binding.name())?
18
4395
            && !assigns_through_macros(symbols, body, binding.name())?
19
        {
20
4187
            kept.push(binding);
21
8783
        }
22
    }
23
5269213
    Ok(kept)
24
5269213
}