1
//! `is_capture_free` soundness tests: a body inlined at a HOF call site must
2
//! reference no creation-site `Variable`. The walk must see through quasiquote
3
//! unquotes and nested lambdas (real capture → reject) while treating the
4
//! closure's own params as bound (shadowing an outer name → still inlinable).
5

            
6
use super::is_capture_free;
7
use crate::ast::{Expr, LambdaParams};
8
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
9

            
10
36
fn sym(s: &str) -> Expr {
11
36
    Expr::Symbol(s.to_string())
12
36
}
13

            
14
13
fn list(items: Vec<Expr>) -> Expr {
15
13
    Expr::List(items)
16
13
}
17

            
18
/// A symbol table where `x` is a creation-site `Variable` (a capturable name).
19
8
fn table_with_var_x() -> SymbolTable {
20
8
    let mut t = SymbolTable::new();
21
8
    t.define(Symbol::new("x", SymbolKind::Variable).with_value(Expr::T));
22
8
    t
23
8
}
24

            
25
15
fn params(required: &[&str]) -> LambdaParams {
26
15
    LambdaParams::simple(required.iter().map(|s| (*s).to_string()).collect())
27
15
}
28

            
29
#[test]
30
1
fn free_variable_in_body_is_not_capture_free() {
31
1
    let body = list(vec![sym("+"), sym("acc"), sym("x")]);
32
1
    assert!(!is_capture_free(
33
1
        &table_with_var_x(),
34
1
        &params(&["acc"]),
35
1
        &body
36
1
    ));
37
1
}
38

            
39
#[test]
40
1
fn param_shadowing_outer_var_stays_capture_free() {
41
    // `x` is the closure's own param, not a capture, even though an outer `x`
42
    // exists — params shadow the creation scope.
43
1
    let body = list(vec![sym("+"), sym("x"), Expr::Nil]);
44
1
    assert!(is_capture_free(&table_with_var_x(), &params(&["x"]), &body));
45
1
}
46

            
47
#[test]
48
1
fn quasiquote_unquoting_outer_var_is_not_capture_free() {
49
    // `(lambda (acc) `(foo ,x))` — the unquote evaluates the creation-site `x`.
50
1
    let body = Expr::Quasiquote(Box::new(list(vec![
51
1
        sym("foo"),
52
1
        Expr::Unquote(Box::new(sym("x"))),
53
1
    ])));
54
1
    assert!(!is_capture_free(
55
1
        &table_with_var_x(),
56
1
        &params(&["acc"]),
57
1
        &body
58
1
    ));
59
1
}
60

            
61
#[test]
62
1
fn unquote_under_a_template_quote_is_not_capture_free() {
63
    // `(lambda (acc) `(a ',x))` — the quote is template text, the unquote runs.
64
1
    let body = Expr::Quasiquote(Box::new(list(vec![
65
1
        sym("a"),
66
1
        Expr::Quote(Box::new(Expr::Unquote(Box::new(sym("x"))))),
67
1
    ])));
68
1
    assert!(!is_capture_free(
69
1
        &table_with_var_x(),
70
1
        &params(&["acc"]),
71
1
        &body
72
1
    ));
73
1
}
74

            
75
#[test]
76
1
fn quoted_outer_var_is_data_and_capture_free() {
77
    // `(lambda (acc) 'x)` — quoted `x` is data, never evaluated.
78
1
    let body = Expr::Quote(Box::new(sym("x")));
79
1
    assert!(is_capture_free(
80
1
        &table_with_var_x(),
81
1
        &params(&["acc"]),
82
1
        &body
83
1
    ));
84
1
}
85

            
86
#[test]
87
1
fn nested_lambda_capturing_outer_var_is_not_capture_free() {
88
    // `(lambda (acc) (map (lambda (z) (+ z x)) acc))` — the inner lambda body
89
    // captures the creation-site `x`; the walk must not stop at the binder.
90
1
    let inner = Expr::Lambda(
91
1
        params(&["z"]),
92
1
        Box::new(list(vec![sym("+"), sym("z"), sym("x")])),
93
1
    );
94
1
    let body = list(vec![sym("map"), inner, sym("acc")]);
95
1
    assert!(!is_capture_free(
96
1
        &table_with_var_x(),
97
1
        &params(&["acc"]),
98
1
        &body
99
1
    ));
100
1
}
101

            
102
#[test]
103
1
fn dotted_quasiquote_unquoting_outer_var_is_not_capture_free() {
104
    // `(lambda (acc) `(,x . nil))` — the unquoted `x` rides a dotted Cons; the
105
    // walk must descend into both Cons arms, not stop at the pair.
106
1
    let body = Expr::Quasiquote(Box::new(Expr::Cons(
107
1
        Box::new(Expr::Unquote(Box::new(sym("x")))),
108
1
        Box::new(Expr::Nil),
109
1
    )));
110
1
    assert!(!is_capture_free(
111
1
        &table_with_var_x(),
112
1
        &params(&["acc"]),
113
1
        &body
114
1
    ));
115
1
}
116

            
117
#[test]
118
1
fn macro_headed_form_is_not_capture_free() {
119
    // `(lambda (acc) (use-x))` where `use-x` is a macro: it can expand to a
120
    // creation-scope variable, so the body is conservatively non-inlinable.
121
1
    let mut t = SymbolTable::new();
122
1
    t.define(
123
1
        Symbol::new("use-x", SymbolKind::Macro).with_function(Expr::Lambda(
124
1
            LambdaParams::simple(vec![]),
125
1
            Box::new(Expr::Nil),
126
1
        )),
127
1
    );
128
1
    let body = list(vec![sym("use-x")]);
129
1
    assert!(!is_capture_free(&t, &params(&["acc"]), &body));
130
1
}
131

            
132
#[test]
133
1
fn lambda_with_optional_param_is_not_inline_candidate() {
134
    // Optional/key/aux defaults and `&rest` need their own capture analysis, so
135
    // such a lambda is never recorded for inlining (stays on `call_ref`).
136
1
    let mut p = params(&["acc"]);
137
1
    p.optional.push(("y".to_string(), Some(Expr::T)));
138
1
    let body = list(vec![sym("+"), sym("acc"), sym("y")]);
139
1
    assert!(!is_capture_free(&table_with_var_x(), &p, &body));
140
1
}
141

            
142
5
fn table_with_function(name: &str, kind: SymbolKind, local: bool) -> SymbolTable {
143
5
    let mut t = SymbolTable::new();
144
5
    let mut function = Symbol::new(name, kind).with_function(Expr::Lambda(
145
5
        LambdaParams::simple(vec!["a".to_string()]),
146
5
        Box::new(sym("a")),
147
5
    ));
148
5
    if local {
149
1
        function = function.local_function();
150
4
    }
151
5
    t.define(function);
152
5
    t
153
5
}
154

            
155
#[test]
156
1
fn global_defun_in_operator_position_is_capture_free() {
157
1
    let t = table_with_function("h", SymbolKind::Function, false);
158
1
    let body = list(vec![sym("h"), sym("z")]);
159
1
    assert!(is_capture_free(&t, &params(&["z"]), &body));
160
1
}
161

            
162
#[test]
163
1
fn global_defun_as_a_value_is_not_capture_free() {
164
1
    let t = table_with_function("h", SymbolKind::Function, false);
165
1
    let body = list(vec![sym("list"), sym("h")]);
166
1
    assert!(!is_capture_free(&t, &params(&["z"]), &body));
167
1
}
168

            
169
#[test]
170
1
fn labels_function_in_operator_position_is_not_capture_free() {
171
1
    let t = table_with_function("h", SymbolKind::Function, true);
172
1
    let body = list(vec![sym("h"), sym("z")]);
173
1
    assert!(!is_capture_free(&t, &params(&["z"]), &body));
174
1
}
175

            
176
#[test]
177
1
fn redefined_builtin_in_operator_position_is_not_capture_free() {
178
1
    let t = table_with_function("+", SymbolKind::Operator, false);
179
1
    let body = list(vec![sym("+"), sym("z"), sym("z")]);
180
1
    assert!(!is_capture_free(&t, &params(&["z"]), &body));
181
1
}
182

            
183
#[test]
184
1
fn defun_head_with_a_captured_argument_is_not_capture_free() {
185
1
    let mut t = table_with_function("h", SymbolKind::Function, false);
186
1
    t.define(Symbol::new("x", SymbolKind::Variable).with_value(Expr::T));
187
1
    let body = list(vec![sym("h"), sym("x")]);
188
1
    assert!(!is_capture_free(&t, &params(&["z"]), &body));
189
1
}