1
//! Codex adversarial review, third pass on this slice: eight findings — four
2
//! regressions the codex-round-4 AND/OR definition-rejection redesign
3
//! introduced, and four pre-existing defects in the same equality/COND
4
//! functions this session already touched. `n` stands for
5
//! `(rpc-protocol-version)`, which is 1. (Codex's own reproductions used
6
//! `(primary-entity-idx)`, which needs the scripting entity-execution
7
//! harness; `rpc-protocol-version` is the equivalent non-foldable runtime
8
//! call available in the `rpc::Session` harness these tests run under —
9
//! confirmed against the same repro shapes via a bare `nms` sandbox using
10
//! `primary-entity-idx` while developing the fix.)
11
//!
12
//! Item 4 (independent depth ceilings still combining into a native-stack
13
//! abort under a 2 MiB stack) has its own dedicated subprocess regression in
14
//! `scripting/nms/tests/test_runner.rs`
15
//! (`combined_recursion_families_do_not_overflow_the_compiler_stack`).
16

            
17
use super::*;
18

            
19
// --- item 1: a guarded DEFPARAMETER that REDEFINES an existing binding
20
// loses the previous value when the guard is skipped — `promote_def_binding`
21
// allocates a FRESH local whose store is conditional, so a skipped branch
22
// reads that fresh local's wasm default (0), not the old binding. Now
23
// rejected (structured error) regardless of whether the new value is
24
// constant or runtime, whenever the binding already existed. ---
25

            
26
#[tokio::test]
27
1
async fn defparameter_redefinition_under_a_runtime_guard_is_rejected_constant_initializer() {
28
1
    let form = "(progn (defparameter x 7) \
29
1
                 (and (> (rpc-protocol-version) 5) (defparameter x 9)) x)";
30
1
    assert_error_code(form, "compile").await;
31
1
}
32

            
33
#[tokio::test]
34
1
async fn defparameter_redefinition_under_a_runtime_guard_is_rejected_runtime_initializer() {
35
1
    let form = "(progn (defparameter x 7) \
36
1
                 (and (> (rpc-protocol-version) 5) \
37
1
                      (defparameter x (+ (rpc-protocol-version) 9))) \
38
1
                 x)";
39
1
    assert_error_code(form, "compile").await;
40
1
}
41

            
42
#[tokio::test]
43
1
async fn defparameter_redefinition_under_a_runtime_guard_is_rejected_when_original_was_runtime() {
44
1
    let form = "(progn (defparameter x (rpc-protocol-version)) \
45
1
                 (and (> (rpc-protocol-version) 5) (defparameter x 9)) \
46
1
                 x)";
47
1
    assert_error_code(form, "compile").await;
48
1
}
49

            
50
// Superseded by round 6 item 1: a brand-new DEFPARAMETER with a RUNTIME
51
// initializer is NOT safe under a guard after all — the symbol table has
52
// no way to represent "only bound when this runtime branch is taken", so
53
// registration is unconditional regardless of the initializer's
54
// classification. A fresh binding under a guard is now always rejected;
55
// see `codex_round8_regression_tests::a_fresh_definition_under_a_runtime_guard_is_rejected_even_with_a_runtime_initializer`.
56
#[tokio::test]
57
1
async fn defparameter_of_a_brand_new_binding_with_a_runtime_initializer_is_rejected() {
58
1
    let form = "(progn (and (> (rpc-protocol-version) 5) \
59
1
                            (defparameter codex-r5-fresh (+ (rpc-protocol-version) 9))) \
60
1
                 42)";
61
1
    assert_error_code(form, "compile").await;
62
1
}
63

            
64
// --- item 2: DEFVAR's "don't overwrite" no-op still evaluated its
65
// initializer, applying the initializer's compile-time effects even though
66
// the value itself was discarded. Now a true no-op: the initializer is
67
// never even evaluated when the variable is already bound. ---
68

            
69
#[tokio::test]
70
1
async fn defvar_no_op_under_a_guard_never_evaluates_its_initializer() {
71
1
    let form = "(progn (defparameter x 7) (defparameter y 0) \
72
1
                 (and (> (rpc-protocol-version) 5) \
73
1
                      (defvar x (progn (setf y 9) 3))) \
74
1
                 y)";
75
1
    assert_values(&[(form, "0")]).await;
76
1
}
77

            
78
// --- item 3: `and_or_depth` is a RECURSION counter (bumped for every
79
// wasm-runtime operand), not a genuine-conditionality flag — a Never-repr
80
// operand (e.g. an Index, always truthy) folds true in `IF` without ever
81
// emitting a real branch, but `enter_and_or_chain` used to fire anyway,
82
// falsely marking a later definition as "under a runtime guard". Now the
83
// SAME `is_runtime_test` check that decides whether a real branch is needed
84
// also gates entering the guard, in both stack/effect codegen and the eval
85
// mirror. ---
86

            
87
#[tokio::test]
88
1
async fn a_never_nil_and_operand_does_not_falsely_guard_a_later_definition_stack_position() {
89
1
    let form = "(and (rpc-protocol-version) (progn (defvar codex-r5-a 7) codex-r5-a))";
90
1
    assert_values(&[(form, "7")]).await;
91
1
}
92

            
93
#[tokio::test]
94
1
async fn a_never_nil_and_operand_does_not_falsely_guard_a_later_definition_effect_position() {
95
1
    let form = "(progn (and (rpc-protocol-version) (defvar codex-r5-b 7)) codex-r5-b)";
96
1
    assert_values(&[(form, "7")]).await;
97
1
}
98

            
99
#[tokio::test]
100
1
async fn a_never_nil_or_operand_does_not_falsely_guard_a_later_definition() {
101
    // OR's never-nil operand IS its result (short-circuits before the
102
    // definition), so this must compile — the definition never runs, but
103
    // it must not be *rejected* as falsely guarded either.
104
1
    let form = "(or (rpc-protocol-version) (defvar codex-r5-c 7))";
105
1
    assert_values(&[(form, "1")]).await;
106
1
}
107

            
108
// --- item 5: the string/non-string equality shortcut emitted the
109
// statically-false result WITHOUT compiling either original operand —
110
// dropping both operands' real effects entirely (not just the classification
111
// probe's). Now both originals compile for effect, once, in source order,
112
// before the constant `nil`. EQL/EQUAL share `compile_equal_to_stack`. ---
113

            
114
#[tokio::test]
115
1
async fn string_vs_nonstring_shortcut_still_compiles_both_operands_for_effect() {
116
1
    let form = r#"(progn (defparameter x 0)
117
1
                   (eq? (progn (setf x (+ x 1)) "hi") (rpc-protocol-version))
118
1
                   x)"#;
119
1
    assert_values(&[(form, "1")]).await;
120
1
}
121

            
122
#[tokio::test]
123
1
async fn string_vs_nonstring_shortcut_still_folds_to_nil() {
124
1
    let form = r#"(eq? "hi" (rpc-protocol-version))"#;
125
1
    assert_values(&[(form, "nil")]).await;
126
1
}
127

            
128
// --- item 6: the numeric equality fallback (shared by `=`/`<`/`>`/`<=`/
129
// `>=`/`/=`/EQ?'s numeric path) classified operands via `resolve_all` on the
130
// LIVE table, then compiled the ORIGINALS again for the runtime path —
131
// applying a nested `setf` TWICE. `resolve_all` now probes on one disposable
132
// clone; the all-constant fold path compiles the originals for effect
133
// exactly once before emitting the fold. ---
134

            
135
#[tokio::test]
136
1
async fn numeric_equality_fallback_does_not_double_apply_a_runtime_operands_effects() {
137
1
    let form = "(progn (defparameter x 0) \
138
1
                 (eq? (progn (setf x (+ x 1)) (rpc-protocol-version)) 0) \
139
1
                 x)";
140
1
    assert_values(&[(form, "1")]).await;
141
1
}
142

            
143
#[tokio::test]
144
1
async fn numeric_equality_all_constant_fold_still_applies_effects_exactly_once() {
145
1
    let form = "(let ((x 0)) (= (progn (setf x (+ x 1)) 3) 3) x)";
146
1
    assert_values(&[(form, "1")]).await;
147
1
}
148

            
149
// --- item 7: `compile_cross_type_nil_identity`'s "different static types
150
// are disjoint" assumption is false for `AnyRef` (the ADR-0025 widen
151
// target), which can literally box the `t` singleton — a runtime `Bool`
152
// true compared against an `AnyRef` holding `t` (e.g. from an `IF` whose
153
// arms disagree in type) must be `t`, not `nil`. Fixed for both operand
154
// orders via `compile_bool_anyref_identity`. ---
155

            
156
#[tokio::test]
157
1
async fn bool_true_equals_an_anyref_boxing_t() {
158
1
    let form = "(eq? (> (+ (rpc-protocol-version) 1) 0) \
159
1
                 (if (> (+ (rpc-protocol-version) 1) 0) t 7))";
160
1
    assert_values(&[(form, "t")]).await;
161
1
}
162

            
163
#[tokio::test]
164
1
async fn anyref_boxing_t_equals_bool_true_reversed_operand_order() {
165
1
    let form = "(eq? (if (> (+ (rpc-protocol-version) 1) 0) t 7) \
166
1
                 (> (+ (rpc-protocol-version) 1) 0))";
167
1
    assert_values(&[(form, "t")]).await;
168
1
}
169

            
170
#[tokio::test]
171
1
async fn bool_false_equals_an_anyref_boxing_nil() {
172
1
    let form = "(eq? (> 0 (+ (rpc-protocol-version) 1)) \
173
1
                 (if (> 0 (+ (rpc-protocol-version) 1)) t nil))";
174
1
    assert_values(&[(form, "t")]).await;
175
1
}
176

            
177
#[tokio::test]
178
1
async fn bool_false_does_not_equal_an_anyref_boxing_t() {
179
1
    let form = "(eq? (> 0 (+ (rpc-protocol-version) 1)) \
180
1
                 (if (> (+ (rpc-protocol-version) 1) 0) t 7))";
181
1
    assert_values(&[(form, "nil")]).await;
182
1
}
183

            
184
// --- item 8: a bodyless RUNTIME COND test's eval-side effects were only
185
// ever applied on a disposable clone (for classification into the
186
// placeholder `if_form` reconciles arms against) — the LIVE table never saw
187
// a `defvar` the test introduced, so a later clause referencing it failed
188
// "undefined symbol" on the eval-mirror surface even though codegen (which
189
// compiles the test for real before the continuation) had no such bug.
190
// Fixed in `if_form::if_form`'s runtime-test branch (covers COND's
191
// with-body delegation too) and explicitly in `cond_form`'s bodyless path
192
// (which hands `if_form` a placeholder, not the original test). ---
193

            
194
#[tokio::test]
195
1
async fn bodyless_runtime_cond_test_definition_is_visible_to_a_later_clause() {
196
1
    let form = "(let ((v (cond ((progn (defvar codex-r5-z 7) (> (rpc-protocol-version) 5))) \
197
1
                              (t codex-r5-z)))) \
198
1
                 v)";
199
1
    assert_values(&[(form, "7")]).await;
200
1
}