1
//! Codex adversarial review, sixth pass on this slice: six correctness
2
//! findings + one simplification. `n` stands for `(rpc-protocol-version)`,
3
//! which is 1. (Every repro was first verified against a bare `nms`
4
//! sandbox using `primary-entity-idx` — the non-foldable runtime call
5
//! available there — while developing the fix, then ported to
6
//! `rpc-protocol-version` for this harness.)
7

            
8
use super::*;
9

            
10
// --- item 1: a FRESH definition (DEFVAR/DEFPARAMETER) registers into the
11
// symbol table UNCONDITIONALLY — the table has no way to represent "only
12
// bound when this runtime branch is taken" — so round 4's "a runtime-valued
13
// fresh binding is safe under a guard" exception was unsound: the DEFVAR
14
// registers regardless of whether the guarding AND actually took that
15
// branch at runtime. Now refused under a runtime guard regardless of the
16
// initializer's classification; DEFVAR's already-bound no-op (which
17
// registers nothing) is the one exception. ---
18

            
19
#[tokio::test]
20
1
async fn a_fresh_definition_under_a_runtime_guard_is_rejected_even_with_a_runtime_initializer() {
21
1
    let form = "(progn (and (> (rpc-protocol-version) 5) (defvar codex-r8-x (rpc-protocol-version))) \
22
1
                 (defvar codex-r8-x 9) codex-r8-x)";
23
1
    assert_error_code(form, "compile").await;
24
1
}
25

            
26
#[tokio::test]
27
1
async fn defvars_already_bound_no_op_still_stays_safe_under_a_runtime_guard() {
28
    // The one exception: an ALREADY-bound DEFVAR registers nothing, so it
29
    // stays safe under a guard even though the guard's own condition is
30
    // runtime.
31
1
    let form = "(progn (defvar codex-r8-y 1) \
32
1
                 (and (> (rpc-protocol-version) 5) (defvar codex-r8-y 9)) codex-r8-y)";
33
1
    assert_values(&[(form, "1")]).await;
34
1
}
35

            
36
// --- item 2: a deferred (constant-classified, DeferRuntime) initializer
37
// used to run against the ALREADY-published new binding — `defparameter_impl`
38
// published the probed value, then `finish_def_binding` (né
39
// `promote_def_binding`) re-executed the SAME initializer for effect against
40
// that already-updated table, double-applying a self-referential mutation.
41
// Fixed by compiling the initializer once, against the environment BEFORE
42
// the new binding is registered, then registering afterwards. ---
43

            
44
#[tokio::test]
45
1
async fn defparameter_redefinition_reads_its_own_old_value_not_the_probed_one() {
46
1
    let form = "(progn (defparameter codex-r8-z 0) \
47
1
                 (defparameter codex-r8-z (progn (setf codex-r8-z (+ codex-r8-z 1)) codex-r8-z)) \
48
1
                 codex-r8-z)";
49
1
    assert_values(&[(form, "1")]).await;
50
1
}
51

            
52
// --- item 3: `compile_cond_runtime_for_effect` opened conditional wasm
53
// blocks without marking `cond_depth`, so a compile-time-only `SETF` inside
54
// a runtime-guarded clause bypassed `reject_compile_time_setf_under_runtime_guard`
55
// entirely — applying its compile-time model update unconditionally despite
56
// sitting inside a real (but false, at runtime) `if` block. Value position
57
// already rejected the equivalent case; effect position now does too. ---
58

            
59
#[tokio::test]
60
1
async fn effect_position_cond_runs_a_stored_globals_setf_only_when_its_guard_holds() {
61
    // 0014 Stage 2: the global is a runtime cell; the untaken clause's
62
    // store never runs.
63
1
    let form = "(progn (defparameter codex-r8-w 0) \
64
1
                 (cond ((> (rpc-protocol-version) 5) (setf codex-r8-w 9))) codex-r8-w)";
65
1
    assert_values(&[(form, "0")]).await;
66
1
}
67

            
68
// --- item 4: a diverging COND clause test (one that never returns to
69
// decide a branch) was routed through the SAME held-test IF construction as
70
// a genuinely-boolean runtime test, so `if_form` classified the clause's
71
// (unreachable) body as a normal, possibly-reachable branch — surfacing an
72
// undefined symbol there as a compile error even though CL semantics say it
73
// never runs. Fixed by compiling/evaluating only the diverging test and
74
// returning, in both codegen and the eval mirror. ---
75

            
76
#[tokio::test]
77
1
async fn a_diverging_cond_test_never_compiles_its_unreachable_body() {
78
1
    let form = "(handler-case (cond ((error 'oops \"x\") no-such-codex-r8-symbol) (t 9)) \
79
1
                 (oops (e) 7))";
80
1
    assert_values(&[(form, "7")]).await;
81
1
}
82

            
83
// --- item 6: `expand_macro` cloned the CALLER's symbol table (carrying its
84
// `and_or_depth`/`cond_depth` guard state) to evaluate the macro's body —
85
// so a DEFVAR inside a macro's own body was wrongly rejected as "under a
86
// runtime guard" whenever the macro was CALLED from inside a runtime AND/OR,
87
// even though the macro body's registration happens in a private expansion
88
// environment discarded once the expansion is produced. Fixed by suspending
89
// guard tracking on that disposable clone before evaluating the body. ---
90

            
91
#[tokio::test]
92
1
async fn a_macro_bodys_own_definition_is_not_rejected_when_called_under_a_runtime_guard() {
93
1
    let form = "(progn (defmacro codex-r8-local-m () (defvar codex-r8-scratch 7) '7) \
94
1
                 (and (> (rpc-protocol-version) 0) (codex-r8-local-m)) 42)";
95
1
    assert_values(&[(form, "42")]).await;
96
1
}