1
//! Codex adversarial review, eighth pass on this slice: 4 findings.
2
//! IMPORTANT: Codex's sandbox failed again, so items 1-2 are CODE-DERIVED
3
//! PREDICTIONS, not observed failures — both were reproduced against a
4
//! bare `nms` sandbox (using `primary-entity-idx`, the non-foldable
5
//! runtime call available there) before fixing, and both predictions held
6
//! exactly (unlike round 7's two disproven VALUE predictions). Items 3/4
7
//! are pure simplifications with no behavior change, covered by existing
8
//! tests; no new tests needed for them. `n` stands for
9
//! `(rpc-protocol-version)`, which is 1.
10

            
11
use super::*;
12

            
13
// --- item 1: the eval-side SETF rejection (`setf`/`set_place` in
14
// structure.rs) recognized only `Expr::WasmLocal` as "runtime-backed", but
15
// `let_eval.rs` binds a genuinely-runtime initializer to
16
// `Expr::WasmRuntime(ty)` (it never allocates a real local — only CODEGEN's
17
// `let_compile.rs` does that, promoting `WasmRuntime` to `WasmLocal` before
18
// the body compiles). So a legitimate runtime mutation, probed by the
19
// eval-only surface before codegen ever ran, was wrongly rejected as
20
// "compile-time-only". Fixed by broadening both checks to
21
// `Expr::is_wasm_runtime()` (true for `WasmRuntime` OR `WasmLocal`). ---
22

            
23
#[tokio::test]
24
1
async fn a_runtime_backed_let_local_setf_under_a_guard_is_not_rejected() {
25
1
    let form = "(let ((result (let ((x (rpc-protocol-version))) \
26
1
                 (and (> x 0) (setf x 2)) x))) result)";
27
1
    assert_values(&[(form, "2")]).await;
28
1
}
29

            
30
#[tokio::test]
31
1
async fn a_runtime_backed_let_local_setfs_new_value_is_visible_after_the_guard() {
32
1
    let form = "(let ((x (rpc-protocol-version))) (and (> x 0) (setf x 5)) (= x 5))";
33
1
    assert_values(&[(form, "t")]).await;
34
1
}
35

            
36
#[tokio::test]
37
1
async fn a_stored_global_setf_under_a_guard_runs_at_runtime() {
38
    // 0014 Stage 2: a DEFPARAMETER the unit stores to is a runtime cell
39
    // from its definition, so the guarded store is a `local.set`.
40
1
    let form = "(progn (defparameter codex-r10-zz 0) \
41
1
                 (and (> (rpc-protocol-version) 0) (setf codex-r10-zz 9)) codex-r10-zz)";
42
1
    assert_values(&[(form, "9")]).await;
43
1
}
44

            
45
// --- item 2: a LITERAL operand (a string, a number, …) has no
46
// `Expr::wasm_type()` of its own (only an already-classified
47
// `WasmRuntime`/`WasmLocal` does), so the concrete-vs-AnyRef dispatch in
48
// `to_stack.rs` (keyed on `(a.wasm_type(), b.wasm_type())`) silently
49
// skipped a literal operand entirely — its `None` never matched
50
// `Some(ty)`, so e.g. a literal string against an `AnyRef`-boxed string
51
// fell through to the wrong shortcut instead of the concrete-vs-AnyRef
52
// runtime discrimination. Fixed by classifying through
53
// `classify_stack_type` (which also handles literals) instead of the
54
// narrower `wasm_type()`. ---
55

            
56
#[tokio::test]
57
1
async fn a_literal_string_equals_an_anyref_boxing_the_same_string() {
58
1
    let form = "(eq? \"x\" (if (= (rpc-protocol-version) 1) \"x\" 1))";
59
1
    assert_values(&[(form, "t")]).await;
60
1
}
61

            
62
#[tokio::test]
63
1
async fn a_literal_string_equals_an_anyref_boxing_the_same_string_reversed_operand_order() {
64
1
    let form = "(eq? (if (= (rpc-protocol-version) 1) \"x\" 1) \"x\")";
65
1
    assert_values(&[(form, "t")]).await;
66
1
}
67

            
68
#[tokio::test]
69
1
async fn eql_and_equal_aliases_also_reach_the_literal_vs_anyref_dispatch() {
70
1
    let eql_form = "(eql \"x\" (if (= (rpc-protocol-version) 1) \"x\" 1))";
71
1
    let equal_form = "(equal \"x\" (if (= (rpc-protocol-version) 1) \"x\" 1))";
72
1
    assert_values(&[(eql_form, "t"), (equal_form, "t")]).await;
73
1
}
74

            
75
#[tokio::test]
76
1
async fn a_literal_string_does_not_equal_an_anyref_boxing_a_different_string() {
77
1
    let form = "(eq? \"x\" (if (= (rpc-protocol-version) 1) \"y\" 1))";
78
1
    assert_values(&[(form, "nil")]).await;
79
1
}
80

            
81
#[tokio::test]
82
1
async fn a_literal_number_equals_an_anyref_boxing_the_same_index_value() {
83
    // The guard `(= n n)` is always true, so the IF's index arm
84
    // `(rpc-protocol-version)` (= 1) is taken, boxed to `AnyRef` since the
85
    // other arm is a string; the literal `1` must equal it.
86
1
    let form = "(eq? 1 (if (= (rpc-protocol-version) (rpc-protocol-version)) \
87
1
                 (rpc-protocol-version) \"z\"))";
88
1
    assert_values(&[(form, "t")]).await;
89
1
}