1
//! Codex adversarial review, ninth pass on this slice: 2 correctness
2
//! findings + 1 simplification. IMPORTANT: Codex's sandbox failed again,
3
//! so items 1-2 are CODE-DERIVED PREDICTIONS, not observed failures. Both
4
//! reproduced against a bare `nms` sandbox before fixing. Item 1's fix
5
//! disproved HALF its own two-part prescription — see below. Item 3 is a
6
//! pure simplification with no behavior change, covered by existing tests;
7
//! no new test needed for it. `n` stands for `(rpc-protocol-version)`,
8
//! which is 1.
9

            
10
use super::*;
11

            
12
// --- item 1: `compile_concrete_vs_anyref_identity` sized its comparison
13
// scratch local from the CALLER's eval-side probe classification (e.g. a
14
// `SETF`'s eval handler reporting the RHS literal's own type, `I32`,
15
// instead of a `Ratio`-typed place's actual declared type, which real
16
// codegen coerces the stored value into via `compile_for_stack_as`) rather
17
// than from what `compile_for_stack` actually pushes. Fixed by having
18
// `compile_concrete_vs_anyref_identity` size its local (and choose its
19
// `nil_repr`/`ref.test` dispatch) from `compile_for_stack`'s ACTUAL return
20
// type. This alone reproduces `t`/`nil` correctly for every case below —
21
// no wasm validation failure.
22
//
23
// The prescribed SECOND part ("make SETF's eval result reflect the
24
// destination's store coercion") was tried and REVERTED: it broke an
25
// EXISTING invariant — `compile_setf_for_stack`'s const-place branch
26
// (codegen) also calls the SAME `set_place` helper the eval-only `setf`
27
// uses, then feeds its return straight into `compile_for_stack`, which
28
// requires a genuinely compile-for-stack-able value (a literal, or a
29
// `WasmLocal` with a real backing local) and explicitly rejects a bare
30
// `WasmRuntime` placeholder with no backing store — exactly what "reflect
31
// the destination's coercion" would return for a runtime-backed place.
32
// `scripting/tests/tag_sync_comparison_test.rs`'s
33
// `nested_runtime_setf_in_const_place_value_is_emitted` caught this
34
// (`(list (setf y (setf x 1))) …` — the nested `(setf x 1)`'s result feeds
35
// `y`'s const-place store, then gets `compile_for_stack`'d directly).
36
// Routing to `compile_concrete_vs_anyref_identity` in the first place does
37
// NOT depend on the probe's exact type (only on "some concrete type,
38
// paired with AnyRef" — true whether the probe says `I32` or `Ratio`), so
39
// the first part alone is both necessary and sufficient here. ---
40

            
41
#[tokio::test]
42
1
async fn setf_into_a_ratio_local_reports_the_locals_type_not_the_literals() {
43
1
    let form = "(let ((x (index->scalar (rpc-protocol-version)))) \
44
1
                 (eq? (setf x 1) (if (= (rpc-protocol-version) (rpc-protocol-version)) \
45
1
                 (index->scalar (rpc-protocol-version)) \"z\")))";
46
1
    assert_values(&[(form, "t")]).await;
47
1
}
48

            
49
#[tokio::test]
50
1
async fn setf_into_a_ratio_local_reversed_operand_order() {
51
1
    let form = "(let ((x (index->scalar (rpc-protocol-version)))) \
52
1
                 (eq? (if (= (rpc-protocol-version) (rpc-protocol-version)) \
53
1
                 (index->scalar (rpc-protocol-version)) \"z\") (setf x 1)))";
54
1
    assert_values(&[(form, "t")]).await;
55
1
}
56

            
57
#[tokio::test]
58
1
async fn setf_into_a_ratio_local_eql_and_equal_aliases() {
59
1
    let eql_form = "(let ((x (index->scalar (rpc-protocol-version)))) \
60
1
                 (eql (setf x 1) (if (= (rpc-protocol-version) (rpc-protocol-version)) \
61
1
                 (index->scalar (rpc-protocol-version)) \"z\")))";
62
1
    let equal_form = "(let ((x (index->scalar (rpc-protocol-version)))) \
63
1
                 (equal (setf x 1) (if (= (rpc-protocol-version) (rpc-protocol-version)) \
64
1
                 (index->scalar (rpc-protocol-version)) \"z\")))";
65
1
    assert_values(&[(eql_form, "t"), (equal_form, "t")]).await;
66
1
}
67

            
68
#[tokio::test]
69
1
async fn setf_into_a_ratio_local_mismatched_value_is_still_nil_not_a_crash() {
70
1
    let form = "(let ((x (index->scalar (rpc-protocol-version)))) \
71
1
                 (eq? (setf x 2) (if (= (rpc-protocol-version) (rpc-protocol-version)) \
72
1
                 (index->scalar (rpc-protocol-version)) \"z\")))";
73
1
    assert_values(&[(form, "nil")]).await;
74
1
}
75

            
76
// --- item 2: `let_eval.rs`'s eval-only LET/LET* bound a constant
77
// initializer straight to its literal value, but CODEGEN's
78
// `bind_runtime_or_const` promotes exactly that binding to a real
79
// `WasmLocal` whenever the body mutates it via SETF (`assigned_var_runtime_type`)
80
// — a safety net for loop accumulators. So a guarded SETF into a
81
// constant-initialized-but-mutated LET-var was wrongly rejected as
82
// compile-time-only. Fixed by reusing `assigned_var_runtime_type`'s SAME
83
// promotion decision on the eval side (`promote_assigned_binding`),
84
// explicitly EXCLUDED while evaluating a macro's own private body (a new
85
// `SymbolTable::is_macro_expansion` flag) so pure compile-time arithmetic a
86
// macro uses to build its expansion is not disturbed. ---
87

            
88
#[tokio::test]
89
1
async fn a_constant_initialized_let_var_mutated_under_and_is_promoted_like_codegen() {
90
1
    let form = "(let ((result (let ((x 0)) (and (> (rpc-protocol-version) 0) (setf x 2)) x))) \
91
1
                 result)";
92
1
    assert_values(&[(form, "2")]).await;
93
1
}
94

            
95
#[tokio::test]
96
1
async fn a_constant_initialized_let_star_var_mutated_under_and_is_promoted_like_codegen() {
97
1
    let form = "(let ((result (let* ((x 0)) (and (> (rpc-protocol-version) 0) (setf x 2)) x))) \
98
1
                 result)";
99
1
    assert_values(&[(form, "2")]).await;
100
1
}
101

            
102
#[tokio::test]
103
1
async fn a_constant_initialized_let_var_mutated_under_or_is_promoted_like_codegen() {
104
1
    let form = "(let ((x 0)) (or nil (setf x 3)) x)";
105
1
    assert_values(&[(form, "3")]).await;
106
1
}
107

            
108
#[tokio::test]
109
1
async fn a_constant_initialized_let_var_mutated_under_cond_is_promoted_like_codegen() {
110
1
    let form = "(let ((x 0)) (cond ((> (rpc-protocol-version) 0) (setf x 4))) x)";
111
1
    assert_values(&[(form, "4")]).await;
112
1
}
113

            
114
#[tokio::test]
115
1
async fn a_nested_initializer_probe_still_sees_the_promoted_binding() {
116
    // The whole guarded LET sits inside an EQ? operand, which classifies it
117
    // on a disposable clone FIRST (a "nested initializer probe") before the
118
    // real emission — the promotion decision must hold up under that too.
119
1
    let form = "(eq? (let ((x 0)) (and (> (rpc-protocol-version) 0) (setf x 5)) x) 5)";
120
1
    assert_values(&[(form, "t")]).await;
121
1
}
122

            
123
#[tokio::test]
124
1
async fn a_stored_global_under_a_guard_is_a_runtime_cell() {
125
    // 0014 Stage 2: a DEFPARAMETER the unit stores to is a runtime cell, so
126
    // the guarded store runs when the guard holds.
127
1
    let form = "(progn (defparameter codex-r11-zz 0) \
128
1
                 (and (> (rpc-protocol-version) 0) (setf codex-r11-zz 9)) codex-r11-zz)";
129
1
    assert_values(&[(form, "9")]).await;
130
1
}
131

            
132
#[tokio::test]
133
1
async fn a_macros_own_pure_compile_time_let_setf_arithmetic_still_folds() {
134
    // Regression guard: the promotion must NOT reach into a macro's private
135
    // expansion body — that LET/SETF computes the macro's OUTPUT at
136
    // compile time and is never itself codegen'd.
137
1
    let form = "(progn (defmacro codex-r11-double (n) (let ((r n)) (setf r (* r 2)) r)) \
138
1
                 (codex-r11-double 5))";
139
1
    assert_values(&[(form, "10")]).await;
140
1
}