Lines
100 %
Functions
Branches
//! Codex adversarial review, sixth pass on this slice: six correctness
//! findings + one simplification. `n` stands for `(rpc-protocol-version)`,
//! which is 1. (Every repro was first verified against a bare `nms`
//! sandbox using `primary-entity-idx` — the non-foldable runtime call
//! available there — while developing the fix, then ported to
//! `rpc-protocol-version` for this harness.)
use super::*;
// --- item 1: a FRESH definition (DEFVAR/DEFPARAMETER) registers into the
// symbol table UNCONDITIONALLY — the table has no way to represent "only
// bound when this runtime branch is taken" — so round 4's "a runtime-valued
// fresh binding is safe under a guard" exception was unsound: the DEFVAR
// registers regardless of whether the guarding AND actually took that
// branch at runtime. Now refused under a runtime guard regardless of the
// initializer's classification; DEFVAR's already-bound no-op (which
// registers nothing) is the one exception. ---
#[tokio::test]
async fn a_fresh_definition_under_a_runtime_guard_is_rejected_even_with_a_runtime_initializer() {
let form = "(progn (and (> (rpc-protocol-version) 5) (defvar codex-r8-x (rpc-protocol-version))) \
(defvar codex-r8-x 9) codex-r8-x)";
assert_error_code(form, "compile").await;
}
async fn defvars_already_bound_no_op_still_stays_safe_under_a_runtime_guard() {
// The one exception: an ALREADY-bound DEFVAR registers nothing, so it
// stays safe under a guard even though the guard's own condition is
// runtime.
let form = "(progn (defvar codex-r8-y 1) \
(and (> (rpc-protocol-version) 5) (defvar codex-r8-y 9)) codex-r8-y)";
assert_values(&[(form, "1")]).await;
// --- item 2: a deferred (constant-classified, DeferRuntime) initializer
// used to run against the ALREADY-published new binding — `defparameter_impl`
// published the probed value, then `finish_def_binding` (né
// `promote_def_binding`) re-executed the SAME initializer for effect against
// that already-updated table, double-applying a self-referential mutation.
// Fixed by compiling the initializer once, against the environment BEFORE
// the new binding is registered, then registering afterwards. ---
async fn defparameter_redefinition_reads_its_own_old_value_not_the_probed_one() {
let form = "(progn (defparameter codex-r8-z 0) \
(defparameter codex-r8-z (progn (setf codex-r8-z (+ codex-r8-z 1)) codex-r8-z)) \
codex-r8-z)";
// --- item 3: `compile_cond_runtime_for_effect` opened conditional wasm
// blocks without marking `cond_depth`, so a compile-time-only `SETF` inside
// a runtime-guarded clause bypassed `reject_compile_time_setf_under_runtime_guard`
// entirely — applying its compile-time model update unconditionally despite
// sitting inside a real (but false, at runtime) `if` block. Value position
// already rejected the equivalent case; effect position now does too. ---
async fn effect_position_cond_runs_a_stored_globals_setf_only_when_its_guard_holds() {
// 0014 Stage 2: the global is a runtime cell; the untaken clause's
// store never runs.
let form = "(progn (defparameter codex-r8-w 0) \
(cond ((> (rpc-protocol-version) 5) (setf codex-r8-w 9))) codex-r8-w)";
assert_values(&[(form, "0")]).await;
// --- item 4: a diverging COND clause test (one that never returns to
// decide a branch) was routed through the SAME held-test IF construction as
// a genuinely-boolean runtime test, so `if_form` classified the clause's
// (unreachable) body as a normal, possibly-reachable branch — surfacing an
// undefined symbol there as a compile error even though CL semantics say it
// never runs. Fixed by compiling/evaluating only the diverging test and
// returning, in both codegen and the eval mirror. ---
async fn a_diverging_cond_test_never_compiles_its_unreachable_body() {
let form = "(handler-case (cond ((error 'oops \"x\") no-such-codex-r8-symbol) (t 9)) \
(oops (e) 7))";
assert_values(&[(form, "7")]).await;
// --- item 6: `expand_macro` cloned the CALLER's symbol table (carrying its
// `and_or_depth`/`cond_depth` guard state) to evaluate the macro's body —
// so a DEFVAR inside a macro's own body was wrongly rejected as "under a
// runtime guard" whenever the macro was CALLED from inside a runtime AND/OR,
// even though the macro body's registration happens in a private expansion
// environment discarded once the expansion is produced. Fixed by suspending
// guard tracking on that disposable clone before evaluating the body. ---
async fn a_macro_bodys_own_definition_is_not_rejected_when_called_under_a_runtime_guard() {
let form = "(progn (defmacro codex-r8-local-m () (defvar codex-r8-scratch 7) '7) \
(and (> (rpc-protocol-version) 0) (codex-r8-local-m)) 42)";
assert_values(&[(form, "42")]).await;