1
//! Codex adversarial review of the committed Phase 4 ("Common Lisp control
2
//! semantics", fef6dc08) / Phase 5 ("nil is never dereferenced", 12c8df0f):
3
//! three 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 2 (a long flat COND's constant-false clause prefix recursing the
7
//! compiler's native stack) has its own dedicated regression in
8
//! `scripting/nms/tests/test_runner.rs`
9
//! (`long_cond_chain_does_not_overflow_the_compiler_stack`), run as a real
10
//! subprocess so an actual SIGABRT crashes only the child.
11

            
12
use super::*;
13

            
14
// --- item 1: the t/nil identity shortcut in EQ?/EQUAL/EQL must compile
15
// BOTH operands exactly once, in source order — it used to resolve the
16
// known t/nil operand purely via `eval_value` (a compile-time classify with
17
// no wasm emission) and only ever compile the OTHER operand, silently
18
// dropping the known operand's own runtime effects. ---
19

            
20
#[tokio::test]
21
1
async fn eq_identity_test_does_not_drop_the_known_operand_side_effects() {
22
1
    let form = "(let ((n 0)) (eq? (progn (setf n 1) t) (> (rpc-protocol-version) 0)) n)";
23
1
    assert_values(&[(form, "1")]).await;
24
1
}
25

            
26
#[tokio::test]
27
1
async fn eq_identity_test_does_not_drop_the_known_operand_side_effects_reversed_operand_order() {
28
1
    let form = "(let ((n 0)) (eq? (> (rpc-protocol-version) 0) (progn (setf n 1) t)) n)";
29
1
    assert_values(&[(form, "1")]).await;
30
1
}
31

            
32
#[tokio::test]
33
1
async fn eq_identity_test_with_nil_known_side_does_not_drop_effects() {
34
1
    let form = "(let ((n 0)) (eq? (progn (setf n 1) nil) (> (rpc-protocol-version) 0)) n)";
35
1
    assert_values(&[(form, "1")]).await;
36
1
}
37

            
38
#[tokio::test]
39
1
async fn equal_identity_test_does_not_drop_the_known_operand_side_effects() {
40
1
    let form = "(let ((n 0)) (equal (progn (setf n 1) t) (> (rpc-protocol-version) 0)) n)";
41
1
    assert_values(&[(form, "1")]).await;
42
1
}
43

            
44
#[tokio::test]
45
1
async fn eql_identity_test_does_not_drop_the_known_operand_side_effects() {
46
1
    let form = "(let ((n 0)) (eql (progn (setf n 1) t) (> (rpc-protocol-version) 0)) n)";
47
1
    assert_values(&[(form, "1")]).await;
48
1
}
49

            
50
// --- item 3: AND/OR in effect position must not force every operand
51
// through the STACK compiler (which rejects a definition form — DEFVAR /
52
// DEFPARAMETER / DEFUN — that has no stack lowering at all). A statically
53
// deciding or final operand now compiles for effect only; a stack value is
54
// required exactly where a runtime operand's truthiness gates the rest. ---
55

            
56
#[tokio::test]
57
1
async fn defvar_as_the_final_and_operand_in_effect_position_still_runs() {
58
1
    let form = "(progn (and t (defvar codex-r2-a 7)) codex-r2-a)";
59
1
    assert_values(&[(form, "7")]).await;
60
1
}
61

            
62
#[tokio::test]
63
1
async fn defvar_as_the_final_or_operand_in_effect_position_still_runs() {
64
1
    let form = "(progn (or nil (defvar codex-r2-b 7)) codex-r2-b)";
65
1
    assert_values(&[(form, "7")]).await;
66
1
}
67

            
68
#[tokio::test]
69
1
async fn defparameter_as_the_final_and_operand_in_effect_position_still_runs() {
70
1
    let form = "(progn (and t (defparameter codex-r2-c 7)) codex-r2-c)";
71
1
    assert_values(&[(form, "7")]).await;
72
1
}
73

            
74
#[tokio::test]
75
1
async fn defparameter_as_the_final_or_operand_in_effect_position_still_runs() {
76
1
    let form = "(progn (or nil (defparameter codex-r2-d 7)) codex-r2-d)";
77
1
    assert_values(&[(form, "7")]).await;
78
1
}
79

            
80
#[tokio::test]
81
1
async fn defun_as_the_final_and_operand_in_effect_position_still_runs() {
82
1
    let form = "(progn (and t (defun codex-r2-e () 7)) (codex-r2-e))";
83
1
    assert_values(&[(form, "7")]).await;
84
1
}
85

            
86
#[tokio::test]
87
1
async fn defun_as_the_final_or_operand_in_effect_position_still_runs() {
88
1
    let form = "(progn (or nil (defun codex-r2-f () 7)) (codex-r2-f))";
89
1
    assert_values(&[(form, "7")]).await;
90
1
}
91

            
92
// A runtime operand's TRUTHINESS still correctly gates the rest of the
93
// chain in effect position — the fix must not turn AND/OR effect position
94
// into "always run everything".
95
#[tokio::test]
96
1
async fn and_effect_position_short_circuits_on_a_leading_false_runtime_operand() {
97
1
    let form = "(let ((n 0)) (and (> (rpc-protocol-version) 5) (setf n 1)) n)";
98
1
    assert_values(&[(form, "0")]).await;
99
1
}
100

            
101
#[tokio::test]
102
1
async fn and_effect_position_runs_second_operand_on_a_true_runtime_operand() {
103
1
    let form = "(let ((n 0)) (and (> (rpc-protocol-version) 0) (setf n 1)) n)";
104
1
    assert_values(&[(form, "1")]).await;
105
1
}
106

            
107
#[tokio::test]
108
1
async fn or_effect_position_skips_the_second_operand_on_a_true_runtime_operand() {
109
1
    let form = "(let ((n 0)) (or (> (rpc-protocol-version) 0) (setf n 1)) n)";
110
1
    assert_values(&[(form, "0")]).await;
111
1
}
112

            
113
#[tokio::test]
114
1
async fn or_effect_position_runs_second_operand_on_a_false_runtime_operand() {
115
1
    let form = "(let ((n 0)) (or (> (rpc-protocol-version) 5) (setf n 1)) n)";
116
1
    assert_values(&[(form, "1")]).await;
117
1
}