Lines
100 %
Functions
Branches
use super::*;
use crate::ast::Fraction;
use crate::ast::Number;
use crate::ast::{LambdaParams, WasmType};
use crate::runtime::{Symbol, SymbolKind};
fn runtime_symbols(names: &[&str]) -> SymbolTable {
// Reader case-folds source symbols to uppercase; matching that
// here means the SymbolTable lookup hits when the test body
// parses `x` as `X`.
let mut table = SymbolTable::new();
for n in names {
table.define(
Symbol::new(n.to_uppercase(), SymbolKind::Variable)
.with_value(Expr::WasmRuntime(WasmType::I32)),
);
}
table
fn parse(src: &str) -> Expr {
crate::Reader::parse_expr(src).expect("parse")
fn captured(symbols: &SymbolTable, params: &LambdaParams, body: &Expr) -> CaptureSet {
compute_captures(symbols, params, body).expect("captures")
fn capture_count(symbols: &SymbolTable, params: &LambdaParams, body: &Expr) -> usize {
captured(symbols, params, body).len()
#[test]
fn empty_body_has_no_captures() {
let symbols = runtime_symbols(&["x"]);
let params = LambdaParams::simple(vec!["a".into()]);
let body = Expr::Nil;
assert_eq!(capture_count(&symbols, ¶ms, &body), 0);
fn body_referencing_only_params_has_no_captures() {
let body = parse("(+ a 1)");
fn body_referencing_outer_runtime_name_captures_it() {
let body = parse("(+ a x)");
let captures = captured(&symbols, ¶ms, &body);
assert_eq!(captures.len(), 1);
assert!(captures.contains("X"));
fn multiple_captures_preserve_first_seen_order() {
let symbols = runtime_symbols(&["x", "y", "z"]);
let params = LambdaParams::simple(vec![]);
let body = parse("(+ z x y x)");
let collected: Vec<&str> = captures.iter().collect();
assert_eq!(collected, vec!["Z", "X", "Y"]);
fn a_lexical_constant_is_not_captured() {
let mut symbols = SymbolTable::new();
symbols.define(
Symbol::new("PI", SymbolKind::Variable)
.with_value(Expr::Number(Number::Scalar(Fraction::new(22, 7)))),
let body = parse("(+ a pi)");
fn constant_globals_are_not_captured() {
symbols.mark_special("PI");
fn unknown_symbols_are_not_captured() {
// Symbol not in the table at all — capture analysis can't
// claim it; later compile passes will surface UndefinedSymbol.
let symbols = runtime_symbols(&[]);
let body = parse("(+ undefined-thing 1)");
fn nested_let_shadows_outer_capture() {
let body = parse("(let ((x 5)) (+ x 1))");
fn nested_let_init_still_captures_outer() {
// The initializer of a LET binding sees the outer scope, so
// `x` in `(let ((x x)) ...)` is a capture of the outer x.
let body = parse("(let ((x x)) (+ x 1))");
fn nested_lambda_introduces_own_params_but_outer_capture_propagates() {
let body = parse("(lambda (a) (+ a x))");
fn nested_lambda_param_does_not_become_outer_capture() {
let body = parse("(lambda (a) (+ a 1))");
fn quoted_symbols_are_not_captures() {
let body = parse("'x");
fn unquote_under_a_template_quote_captures() {
let body = parse("`(a ',x)");
fn dolist_var_does_not_become_capture() {
let symbols = runtime_symbols(&["xs"]);
let body = parse("(dolist (item xs) (+ item 1))");
assert!(captures.contains("XS"));
assert!(!captures.contains("ITEM"));
fn do_with_empty_variable_list_still_captures() {
let symbols = runtime_symbols(&["c"]);
let body = parse("(do () ((> c 3) c) (setf c (+ c 1)))");
let captures = compute_captures(&symbols, ¶ms, &body).expect("captures");
assert!(captures.contains("C"));
fn captured_names(symbols: &SymbolTable, body: &str) -> Vec<String> {
compute_captures(symbols, ¶ms, &parse(body))
.expect("captures")
.iter()
.map(str::to_string)
.collect()
fn a_do_step_sees_every_loop_variable() {
let symbols = runtime_symbols(&["j"]);
let read = "(do ((i 0 (+ i j)) (j 0 (+ j 1))) ((> i 3) i))";
assert!(captured_names(&symbols, read).is_empty());
let store = "(do ((i 0 (progn (setf j 9) (+ i 1))) (j 0 (+ j 1))) ((> i 2) j))";
assert!(captured_names(&symbols, store).is_empty());
assert!(stored(&symbols, store).is_empty());
fn a_dolist_result_form_sees_the_loop_variable() {
assert!(captured_names(&symbols, "(dolist (x '(1 2) x) nil)").is_empty());
fn a_lambda_list_default_reads_the_enclosing_scope() {
let symbols = runtime_symbols(&["c", "y"]);
assert_eq!(
captured_names(&symbols, "(lambda (&optional (x c)) x)"),
["C"]
let later = "(lambda (&optional (x y) (y 2)) x)";
assert_eq!(captured_names(&symbols, later), ["Y"]);
fn a_parsed_lambda_default_reads_the_enclosing_scope() {
let params = LambdaParams {
optional: vec![("X".into(), Some(parse("c")))],
..LambdaParams::simple(vec![])
};
let lambda = Expr::Lambda(params, Box::new(parse("x")));
let found = compute_captures(&symbols, &LambdaParams::simple(vec![]), &lambda);
assert_eq!(found.expect("captures").iter().collect::<Vec<_>>(), ["C"]);
fn labels_names_and_parameters_are_bound() {
let symbols = runtime_symbols(&["f", "x", "y"]);
assert!(captured_names(&symbols, "(labels ((f (x) (f x))) (f 1))").is_empty());
captured_names(&symbols, "(labels ((f (x) y)) (f x))"),
["Y", "X"]
fn with_function(symbols: &mut SymbolTable, name: &str, body: &str) {
Symbol::new(name.to_uppercase(), SymbolKind::Function).with_function(Expr::Lambda(
LambdaParams::simple(vec![]),
Box::new(parse(body)),
)),
fn stored(symbols: &SymbolTable, body: &str) -> Vec<String> {
captured(symbols, ¶ms, &parse(body))
.stored_captures()
fn a_store_to_a_captured_name_is_reported() {
let symbols = runtime_symbols(&["x", "y"]);
assert_eq!(stored(&symbols, "(progn (setf x (+ x 1)) y)"), vec!["X"]);
fn a_store_through_a_field_place_is_not_a_store_to_the_variable() {
let symbols = runtime_symbols(&["p"]);
assert!(stored(&symbols, "(setf (pt-x p) 1)").is_empty());
fn a_store_to_a_local_binding_is_not_a_captured_store() {
assert!(stored(&symbols, "(let ((x 0)) (setf x 1) x)").is_empty());
fn a_called_user_function_contributes_its_captures_and_stores() {
let mut symbols = runtime_symbols(&["g"]);
with_function(&mut symbols, "reader", "g");
with_function(&mut symbols, "writer", "(setf g 1)");
let read = captured(&symbols, ¶ms, &parse("(reader)"));
assert!(read.contains("G"));
assert_eq!(read.stored_captures().count(), 0);
assert_eq!(stored(&symbols, "(writer)"), vec!["G"]);
fn mutually_recursive_functions_are_followed_once() {
with_function(&mut symbols, "ping", "(progn (pong) (setf g 1))");
with_function(&mut symbols, "pong", "(ping)");
assert_eq!(stored(&symbols, "(ping)"), vec!["G"]);
fn a_chain_of_calls_past_the_ceiling_is_refused() {
let links = crate::runtime::MAX_SHARED_RECURSION_DEPTH + 2;
for index in 0..links {
with_function(
&mut symbols,
&format!("f{index}"),
&format!("(f{})", index + 1),
with_function(&mut symbols, &format!("f{links}"), "(setf g 1)");
let refusal = compute_captures(&symbols, ¶ms, &parse("(f0)"));
assert!(matches!(refusal, Err(Error::Resource(_))));
fn with_macro(symbols: &mut SymbolTable, name: &str, params: LambdaParams, body: &str) {
Symbol::new(name.to_uppercase(), SymbolKind::Macro)
.with_function(Expr::Lambda(params, Box::new(parse(body)))),
fn a_template_macro_use_is_walked_through_its_expansion() {
with_macro(
"bump",
"'(setf g 1)",
with_macro(&mut symbols, "rd", LambdaParams::simple(vec![]), "'g");
assert_eq!(stored(&symbols, "(bump)"), vec!["G"]);
let read = captured(&symbols, ¶ms, &parse("(rd)"));
fn an_opaque_macro_use_may_store_to_every_capture() {
LambdaParams::simple(vec!["v".into()]),
"(list 'setf v 1)",
assert_eq!(stored(&symbols, "(bump g)"), vec!["G"]);
fn followed(symbols: &SymbolTable, src: &str) -> Vec<String> {
captured(symbols, ¶ms, &parse(src))
.followed_functions()
fn a_call_and_the_functions_behind_it_are_followed() {
let mut symbols = runtime_symbols(&[]);
with_function(&mut symbols, "outer", "(inner)");
with_function(&mut symbols, "inner", "1");
assert_eq!(followed(&symbols, "(outer)"), vec!["OUTER", "INNER"]);
fn a_function_named_as_a_value_is_followed() {
with_function(&mut symbols, "g", "1");
assert_eq!(followed(&symbols, "(funcall #'g 1)"), vec!["G"]);
assert_eq!(followed(&symbols, "(funcall 'g 1)"), vec!["G"]);
assert!(followed(&symbols, "(funcall 'h 1)").is_empty());
fn a_labels_function_shadows_a_call_and_sharp_quote_but_not_a_quoted_name() {
let labels = |use_form: &str| format!("(labels ((g (y) y)) {use_form})");
assert!(followed(&symbols, &labels("(g 1)")).is_empty());
assert!(followed(&symbols, &labels("(funcall #'g 1)")).is_empty());
assert_eq!(followed(&symbols, &labels("(funcall 'g 1)")), vec!["G"]);
fn a_function_name_in_a_constant_the_body_reads_is_followed() {
for (name, value) in [("n", "'g"), ("ops", "(list g)"), ("pair", "(g . 1)")] {
let value = parse(value);
symbols.define(Symbol::new(name.to_uppercase(), SymbolKind::Variable).with_value(value));
followed(&symbols, &format!("(funcall {name} 1)")),
vec!["G"]
fn a_quoted_list_names_the_functions_in_it() {
assert_eq!(followed(&symbols, "(car '(a g))"), vec!["G"]);
assert!(followed(&symbols, "(car '(a h))").is_empty());
fn a_clause_key_named_like_an_opaque_macro_is_not_a_macro_use() {
assert!(stored(&symbols, "(case g (bump g))").is_empty());
assert!(stored(&symbols, "(handler-case g (bump (e) 1))").is_empty());
assert_eq!(stored(&symbols, "(cond (t (bump g)))"), vec!["G"]);
fn a_labels_function_named_like_an_opaque_macro_is_not_a_macro_use() {
assert!(stored(&symbols, "(labels ((bump (v) v)) (bump g))").is_empty());
stored(&symbols, "(progn (labels ((f (v) v)) (f g)) (bump g))"),