1
//! Codex adversarial review of the codex-round-2 fixes (this session):
2
//! five findings, each a regression of round 2's own change or an unfixed
3
//! variant of the same class of bug, fixed at the root with a by-value
4
//! regression test here. `n` stands for `(rpc-protocol-version)`, which is
5
//! 1.
6
//!
7
//! Item 1 (COND/AND still able to overflow the native stack under a
8
//! constrained thread stack) has its own dedicated regressions in
9
//! `scripting/nms/tests/test_runner.rs`
10
//! (`cond_clauses_with_bodies_iterate_under_a_constrained_native_stack`,
11
//! `cond_runtime_chain_past_the_depth_ceiling_errors_not_crashes`,
12
//! `and_runtime_chain_past_the_depth_ceiling_errors_not_crashes`,
13
//! `cond_runtime_chain_under_the_depth_ceiling_still_runs_under_a_constrained_stack`),
14
//! run as real subprocesses under an explicit `ulimit -s 2048` so an actual
15
//! SIGABRT crashes only the child.
16

            
17
use super::*;
18

            
19
// --- item 2: EQ?/EQUAL/EQL's two classification probes must run on ONE
20
// disposable clone, sequentially — a separate clone per operand let the
21
// second probe see STALE state: a `defvar` the first operand just
22
// introduced read back as "undefined symbol", and a `setf` it just applied
23
// read back as the OLD value. ---
24

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

            
32
#[tokio::test]
33
1
async fn second_probe_sees_the_first_operands_setf_reversed_operand_order() {
34
    // Strict left-to-right evaluation: `x` (args[0]) is read BEFORE the
35
    // `setf` in args[1] runs, so it captures the OLD value `t` — which is
36
    // then `eq?` against the runtime `(> 1 0)`'s result, itself literally
37
    // the `t` object (ADR-0033: a boolean IS `t`/`nil`, not a separate
38
    // true-like value) — so this direction's correct answer is `t`, the
39
    // OPPOSITE of the non-reversed form's `nil` (which reads the ALREADY
40
    // mutated `x` second). Swapping operand order changes WHEN `x` is
41
    // read relative to the mutation, so the two directions legitimately
42
    // disagree — this pins that asymmetry rather than assuming it away.
43
1
    let form = "(progn (defparameter x t) \
44
1
                 (eq? x (progn (setf x nil) (> (rpc-protocol-version) 0))))";
45
1
    assert_values(&[(form, "t")]).await;
46
1
}
47

            
48
#[tokio::test]
49
1
async fn second_probe_sees_the_first_operands_defvar() {
50
1
    let form = "(eq? (progn (defvar x t) t) x)";
51
1
    assert_values(&[(form, "t")]).await;
52
1
}
53

            
54
// --- item 3: a definition (DEFVAR/DEFPARAMETER/DEFUN) compiled under a
55
// GENUINELY RUNTIME AND/OR guard has no way to make its compile-time
56
// symbol-table registration conditional on a runtime branch — DEFPARAMETER
57
// with a constant initializer applied unconditionally at compile time
58
// regardless of whether the guard is ever taken at runtime. Rather than
59
// silently mis-defining it, this is now a structured compile error (plan
60
// 0011: a definition must be statically placed). ---
61

            
62
#[tokio::test]
63
1
async fn defparameter_under_a_runtime_and_guard_is_a_structured_error() {
64
1
    let form = "(progn (defparameter x 0) \
65
1
                 (and (> (rpc-protocol-version) 5) (defparameter x 7)) x)";
66
1
    assert_error_code(form, "compile").await;
67
1
}
68

            
69
#[tokio::test]
70
1
async fn defparameter_under_a_runtime_or_guard_is_a_structured_error() {
71
1
    let form = "(progn (defparameter x 0) \
72
1
                 (or (> (rpc-protocol-version) 0) (defparameter x 7)) x)";
73
1
    assert_error_code(form, "compile").await;
74
1
}
75

            
76
#[tokio::test]
77
1
async fn the_definition_name_appears_in_the_rejection_message() {
78
1
    let response = eval_value_of(
79
1
        "(progn (defparameter x 0) \
80
1
         (and (> (rpc-protocol-version) 5) (defparameter x 7)) x)",
81
1
    )
82
1
    .await;
83
1
    assert!(
84
1
        response.contains("DEFPARAMETER"),
85
1
        "error should name the rejected form: {response}"
86
1
    );
87
1
}
88

            
89
// A definition after a COMPILE-TIME-CONSTANT (non-runtime) AND/OR guard is
90
// unaffected — the rejection is specific to a genuinely runtime guard.
91
#[tokio::test]
92
1
async fn defvar_after_a_constant_and_guard_still_runs() {
93
1
    let form = "(progn (and t (defvar codex-r3-a 7)) codex-r3-a)";
94
1
    assert_values(&[(form, "7")]).await;
95
1
}
96

            
97
// --- item 4: the all-constant EQ?/EQUAL/EQL fold used `eval_value` (a pure
98
// compile-time classify) to compute the SAME value it emitted, so a `setf`
99
// on a genuinely-runtime local inside an operand never got its real
100
// `local.set` emitted. The fold's value now comes from the sequential
101
// disposable probe (item 2), and both originals are compiled for EFFECT
102
// (real bytecode) exactly once, in source order, before the folded
103
// constant is emitted. ---
104

            
105
#[tokio::test]
106
1
async fn all_constant_fold_still_emits_a_runtime_setf_inside_an_operand() {
107
1
    let form = "(let ((x (rpc-protocol-version))) (eq? (progn (setf x 9) t) t) x)";
108
1
    assert_values(&[(form, "9")]).await;
109
1
}
110

            
111
#[tokio::test]
112
1
async fn all_constant_fold_still_emits_a_runtime_setf_reversed_operand_order() {
113
1
    let form = "(let ((x (rpc-protocol-version))) (eq? t (progn (setf x 9) t)) x)";
114
1
    assert_values(&[(form, "9")]).await;
115
1
}
116

            
117
#[tokio::test]
118
1
async fn all_constant_fold_still_emits_a_runtime_setf_for_a_nil_nil_fold() {
119
1
    let form = "(let ((x (rpc-protocol-version))) (eq? (progn (setf x 9) nil) nil) x)";
120
1
    assert_values(&[(form, "9")]).await;
121
1
}
122

            
123
// --- item 5: `apply_const_test` used to call `eval_value` alone when a
124
// COND/IF test's value was statically known — that classifies the fold but
125
// never emits a runtime `setf` inside the test as real bytecode. It now
126
// ALSO compiles the test for effect, once, regardless of whether its value
127
// is constant. Covers a true and a false test, in both stack and effect
128
// position. ---
129

            
130
#[tokio::test]
131
1
async fn constant_false_cond_test_still_emits_its_runtime_setf_stack_position() {
132
1
    let form = "(let ((x (rpc-protocol-version))) \
133
1
                 (or nil (cond ((progn (setf x 9) nil)) (t x))))";
134
1
    assert_values(&[(form, "9")]).await;
135
1
}
136

            
137
#[tokio::test]
138
1
async fn constant_true_cond_test_still_emits_its_runtime_setf_stack_position() {
139
1
    let form = "(let ((x (rpc-protocol-version))) \
140
1
                 (cond ((progn (setf x 9) t)) (t 0)) x)";
141
1
    assert_values(&[(form, "9")]).await;
142
1
}
143

            
144
#[tokio::test]
145
1
async fn constant_false_cond_test_still_emits_its_runtime_setf_effect_position() {
146
1
    let form = "(let ((x (rpc-protocol-version))) \
147
1
                 (progn (cond ((progn (setf x 9) nil) 1))) x)";
148
1
    assert_values(&[(form, "9")]).await;
149
1
}
150

            
151
#[tokio::test]
152
1
async fn constant_true_cond_test_still_emits_its_runtime_setf_effect_position() {
153
1
    let form = "(let ((x (rpc-protocol-version))) \
154
1
                 (progn (cond ((progn (setf x 9) t)))) x)";
155
1
    assert_values(&[(form, "9")]).await;
156
1
}