1
//! Codex adversarial review of the reduced Phase 4 (BLOCK exit typing
2
//! excluded — see doc/ops/0010-common-lisp-booleans.org): four in-scope
3
//! bugs, each fixed at the root with a by-value regression test here.
4
//! `n` stands for `(rpc-protocol-version)`, which is 1.
5
//!
6
//! Item 1 (AND/OR native-stack overflow on a long flat chain) has its own
7
//! dedicated regression in `scripting/nms/tests/test_runner.rs`
8
//! (`long_and_or_chains_do_not_overflow_the_compiler_stack`), run as a real
9
//! subprocess so an actual SIGABRT crashes only the child.
10

            
11
use super::*;
12

            
13
// --- item 2: MAP's eval mirror must predict the FUNCTION's result type,
14
// not the input list's element type, for an inline lambda / bare defun
15
// over a genuinely runtime list (not just a let-bound closure). ---
16

            
17
#[tokio::test]
18
1
async fn map_with_inline_lambda_over_a_runtime_list_predicts_the_result_type() {
19
1
    let form = "(let ((xs (cons (rpc-protocol-version) nil))) (if (car (map (lambda (x) (> x 5)) xs)) 1 2))";
20
1
    assert_values(&[(form, "2")]).await;
21
1
}
22

            
23
// --- item 3: LABELS / FUNCALL / APPLY in effect position must never
24
// serialize their (discarded) value to the RPC output stream — a runtime
25
// loop running any of them thousands of times must not exhaust the output
26
// buffer. ---
27

            
28
#[tokio::test]
29
1
async fn funcall_in_a_runtime_loop_body_does_not_leak_output_entries() {
30
1
    let form = "(progn (do ((i (rpc-protocol-version) (+ i 1))) ((>= i 3000) nil) (funcall (lambda () 3))) 5)";
31
1
    assert_values(&[(form, "5")]).await;
32
1
}
33

            
34
#[tokio::test]
35
1
async fn labels_in_a_runtime_loop_body_does_not_leak_output_entries() {
36
1
    let form = "(progn (do ((i (rpc-protocol-version) (+ i 1))) ((>= i 3000) nil) (labels ((f () 3)) (f))) 5)";
37
1
    assert_values(&[(form, "5")]).await;
38
1
}
39

            
40
#[tokio::test]
41
1
async fn apply_in_a_runtime_loop_body_does_not_leak_output_entries() {
42
1
    let form = "(progn (do ((i (rpc-protocol-version) (+ i 1))) ((>= i 3000) nil) (apply (lambda (x) x) (list 3))) 5)";
43
1
    assert_values(&[(form, "5")]).await;
44
1
}
45

            
46
// --- item 4: a bodyless COND clause must fold a compile-time-constant test
47
// (string or `t`) BEFORE ever building a runtime branch — the rest of the
48
// chain is provably unreachable and must never even compile. ---
49

            
50
#[tokio::test]
51
1
async fn bodyless_cond_clause_folds_a_constant_string_test() {
52
1
    let form = r#"(or (cond ("x") (t missing)) 0)"#;
53
1
    assert_values(&[(form, "\"x\"")]).await;
54
1
}
55

            
56
#[tokio::test]
57
1
async fn bodyless_cond_clause_folds_a_constant_t_test() {
58
1
    let form = "(or (cond (t) (t missing)) 0)";
59
1
    assert_values(&[(form, "t")]).await;
60
1
}
61

            
62
#[tokio::test]
63
1
async fn bodyless_cond_clause_with_a_nil_test_falls_through() {
64
1
    let form = "(or (cond (nil) (t \"reached\")) 0)";
65
1
    assert_values(&[(form, "\"reached\"")]).await;
66
1
}