Lines
100 %
Functions
Branches
//! Codex adversarial review, ninth pass on this slice: 2 correctness
//! findings + 1 simplification. IMPORTANT: Codex's sandbox failed again,
//! so items 1-2 are CODE-DERIVED PREDICTIONS, not observed failures. Both
//! reproduced against a bare `nms` sandbox before fixing. Item 1's fix
//! disproved HALF its own two-part prescription — see below. Item 3 is a
//! pure simplification with no behavior change, covered by existing tests;
//! no new test needed for it. `n` stands for `(rpc-protocol-version)`,
//! which is 1.
use super::*;
// --- item 1: `compile_concrete_vs_anyref_identity` sized its comparison
// scratch local from the CALLER's eval-side probe classification (e.g. a
// `SETF`'s eval handler reporting the RHS literal's own type, `I32`,
// instead of a `Ratio`-typed place's actual declared type, which real
// codegen coerces the stored value into via `compile_for_stack_as`) rather
// than from what `compile_for_stack` actually pushes. Fixed by having
// `compile_concrete_vs_anyref_identity` size its local (and choose its
// `nil_repr`/`ref.test` dispatch) from `compile_for_stack`'s ACTUAL return
// type. This alone reproduces `t`/`nil` correctly for every case below —
// no wasm validation failure.
//
// The prescribed SECOND part ("make SETF's eval result reflect the
// destination's store coercion") was tried and REVERTED: it broke an
// EXISTING invariant — `compile_setf_for_stack`'s const-place branch
// (codegen) also calls the SAME `set_place` helper the eval-only `setf`
// uses, then feeds its return straight into `compile_for_stack`, which
// requires a genuinely compile-for-stack-able value (a literal, or a
// `WasmLocal` with a real backing local) and explicitly rejects a bare
// `WasmRuntime` placeholder with no backing store — exactly what "reflect
// the destination's coercion" would return for a runtime-backed place.
// `scripting/tests/tag_sync_comparison_test.rs`'s
// `nested_runtime_setf_in_const_place_value_is_emitted` caught this
// (`(list (setf y (setf x 1))) …` — the nested `(setf x 1)`'s result feeds
// `y`'s const-place store, then gets `compile_for_stack`'d directly).
// Routing to `compile_concrete_vs_anyref_identity` in the first place does
// NOT depend on the probe's exact type (only on "some concrete type,
// paired with AnyRef" — true whether the probe says `I32` or `Ratio`), so
// the first part alone is both necessary and sufficient here. ---
#[tokio::test]
async fn setf_into_a_ratio_local_reports_the_locals_type_not_the_literals() {
let form = "(let ((x (index->scalar (rpc-protocol-version)))) \
(eq? (setf x 1) (if (= (rpc-protocol-version) (rpc-protocol-version)) \
(index->scalar (rpc-protocol-version)) \"z\")))";
assert_values(&[(form, "t")]).await;
}
async fn setf_into_a_ratio_local_reversed_operand_order() {
(eq? (if (= (rpc-protocol-version) (rpc-protocol-version)) \
(index->scalar (rpc-protocol-version)) \"z\") (setf x 1)))";
async fn setf_into_a_ratio_local_eql_and_equal_aliases() {
let eql_form = "(let ((x (index->scalar (rpc-protocol-version)))) \
(eql (setf x 1) (if (= (rpc-protocol-version) (rpc-protocol-version)) \
let equal_form = "(let ((x (index->scalar (rpc-protocol-version)))) \
(equal (setf x 1) (if (= (rpc-protocol-version) (rpc-protocol-version)) \
assert_values(&[(eql_form, "t"), (equal_form, "t")]).await;
async fn setf_into_a_ratio_local_mismatched_value_is_still_nil_not_a_crash() {
(eq? (setf x 2) (if (= (rpc-protocol-version) (rpc-protocol-version)) \
assert_values(&[(form, "nil")]).await;
// --- item 2: `let_eval.rs`'s eval-only LET/LET* bound a constant
// initializer straight to its literal value, but CODEGEN's
// `bind_runtime_or_const` promotes exactly that binding to a real
// `WasmLocal` whenever the body mutates it via SETF (`assigned_var_runtime_type`)
// — a safety net for loop accumulators. So a guarded SETF into a
// constant-initialized-but-mutated LET-var was wrongly rejected as
// compile-time-only. Fixed by reusing `assigned_var_runtime_type`'s SAME
// promotion decision on the eval side (`promote_assigned_binding`),
// explicitly EXCLUDED while evaluating a macro's own private body (a new
// `SymbolTable::is_macro_expansion` flag) so pure compile-time arithmetic a
// macro uses to build its expansion is not disturbed. ---
async fn a_constant_initialized_let_var_mutated_under_and_is_promoted_like_codegen() {
let form = "(let ((result (let ((x 0)) (and (> (rpc-protocol-version) 0) (setf x 2)) x))) \
result)";
assert_values(&[(form, "2")]).await;
async fn a_constant_initialized_let_star_var_mutated_under_and_is_promoted_like_codegen() {
let form = "(let ((result (let* ((x 0)) (and (> (rpc-protocol-version) 0) (setf x 2)) x))) \
async fn a_constant_initialized_let_var_mutated_under_or_is_promoted_like_codegen() {
let form = "(let ((x 0)) (or nil (setf x 3)) x)";
assert_values(&[(form, "3")]).await;
async fn a_constant_initialized_let_var_mutated_under_cond_is_promoted_like_codegen() {
let form = "(let ((x 0)) (cond ((> (rpc-protocol-version) 0) (setf x 4))) x)";
assert_values(&[(form, "4")]).await;
async fn a_nested_initializer_probe_still_sees_the_promoted_binding() {
// The whole guarded LET sits inside an EQ? operand, which classifies it
// on a disposable clone FIRST (a "nested initializer probe") before the
// real emission — the promotion decision must hold up under that too.
let form = "(eq? (let ((x 0)) (and (> (rpc-protocol-version) 0) (setf x 5)) x) 5)";
async fn a_stored_global_under_a_guard_is_a_runtime_cell() {
// 0014 Stage 2: a DEFPARAMETER the unit stores to is a runtime cell, so
// the guarded store runs when the guard holds.
let form = "(progn (defparameter codex-r11-zz 0) \
(and (> (rpc-protocol-version) 0) (setf codex-r11-zz 9)) codex-r11-zz)";
assert_values(&[(form, "9")]).await;
async fn a_macros_own_pure_compile_time_let_setf_arithmetic_still_folds() {
// Regression guard: the promotion must NOT reach into a macro's private
// expansion body — that LET/SETF computes the macro's OUTPUT at
// compile time and is never itself codegen'd.
let form = "(progn (defmacro codex-r11-double (n) (let ((r n)) (setf r (* r 2)) r)) \
(codex-r11-double 5))";
assert_values(&[(form, "10")]).await;