Lines
100 %
Functions
Branches
//! Codex adversarial review, third pass on this slice: eight findings — four
//! regressions the codex-round-4 AND/OR definition-rejection redesign
//! introduced, and four pre-existing defects in the same equality/COND
//! functions this session already touched. `n` stands for
//! `(rpc-protocol-version)`, which is 1. (Codex's own reproductions used
//! `(primary-entity-idx)`, which needs the scripting entity-execution
//! harness; `rpc-protocol-version` is the equivalent non-foldable runtime
//! call available in the `rpc::Session` harness these tests run under —
//! confirmed against the same repro shapes via a bare `nms` sandbox using
//! `primary-entity-idx` while developing the fix.)
//!
//! Item 4 (independent depth ceilings still combining into a native-stack
//! abort under a 2 MiB stack) has its own dedicated subprocess regression in
//! `scripting/nms/tests/test_runner.rs`
//! (`combined_recursion_families_do_not_overflow_the_compiler_stack`).
use super::*;
// --- item 1: a guarded DEFPARAMETER that REDEFINES an existing binding
// loses the previous value when the guard is skipped — `promote_def_binding`
// allocates a FRESH local whose store is conditional, so a skipped branch
// reads that fresh local's wasm default (0), not the old binding. Now
// rejected (structured error) regardless of whether the new value is
// constant or runtime, whenever the binding already existed. ---
#[tokio::test]
async fn defparameter_redefinition_under_a_runtime_guard_is_rejected_constant_initializer() {
let form = "(progn (defparameter x 7) \
(and (> (rpc-protocol-version) 5) (defparameter x 9)) x)";
assert_error_code(form, "compile").await;
}
async fn defparameter_redefinition_under_a_runtime_guard_is_rejected_runtime_initializer() {
(and (> (rpc-protocol-version) 5) \
(defparameter x (+ (rpc-protocol-version) 9))) \
x)";
async fn defparameter_redefinition_under_a_runtime_guard_is_rejected_when_original_was_runtime() {
let form = "(progn (defparameter x (rpc-protocol-version)) \
(and (> (rpc-protocol-version) 5) (defparameter x 9)) \
// Superseded by round 6 item 1: a brand-new DEFPARAMETER with a RUNTIME
// initializer is NOT safe under a guard after all — the symbol table has
// no way to represent "only bound when this runtime branch is taken", so
// registration is unconditional regardless of the initializer's
// classification. A fresh binding under a guard is now always rejected;
// see `codex_round8_regression_tests::a_fresh_definition_under_a_runtime_guard_is_rejected_even_with_a_runtime_initializer`.
async fn defparameter_of_a_brand_new_binding_with_a_runtime_initializer_is_rejected() {
let form = "(progn (and (> (rpc-protocol-version) 5) \
(defparameter codex-r5-fresh (+ (rpc-protocol-version) 9))) \
42)";
// --- item 2: DEFVAR's "don't overwrite" no-op still evaluated its
// initializer, applying the initializer's compile-time effects even though
// the value itself was discarded. Now a true no-op: the initializer is
// never even evaluated when the variable is already bound. ---
async fn defvar_no_op_under_a_guard_never_evaluates_its_initializer() {
let form = "(progn (defparameter x 7) (defparameter y 0) \
(defvar x (progn (setf y 9) 3))) \
y)";
assert_values(&[(form, "0")]).await;
// --- item 3: `and_or_depth` is a RECURSION counter (bumped for every
// wasm-runtime operand), not a genuine-conditionality flag — a Never-repr
// operand (e.g. an Index, always truthy) folds true in `IF` without ever
// emitting a real branch, but `enter_and_or_chain` used to fire anyway,
// falsely marking a later definition as "under a runtime guard". Now the
// SAME `is_runtime_test` check that decides whether a real branch is needed
// also gates entering the guard, in both stack/effect codegen and the eval
// mirror. ---
async fn a_never_nil_and_operand_does_not_falsely_guard_a_later_definition_stack_position() {
let form = "(and (rpc-protocol-version) (progn (defvar codex-r5-a 7) codex-r5-a))";
assert_values(&[(form, "7")]).await;
async fn a_never_nil_and_operand_does_not_falsely_guard_a_later_definition_effect_position() {
let form = "(progn (and (rpc-protocol-version) (defvar codex-r5-b 7)) codex-r5-b)";
async fn a_never_nil_or_operand_does_not_falsely_guard_a_later_definition() {
// OR's never-nil operand IS its result (short-circuits before the
// definition), so this must compile — the definition never runs, but
// it must not be *rejected* as falsely guarded either.
let form = "(or (rpc-protocol-version) (defvar codex-r5-c 7))";
assert_values(&[(form, "1")]).await;
// --- item 5: the string/non-string equality shortcut emitted the
// statically-false result WITHOUT compiling either original operand —
// dropping both operands' real effects entirely (not just the classification
// probe's). Now both originals compile for effect, once, in source order,
// before the constant `nil`. EQL/EQUAL share `compile_equal_to_stack`. ---
async fn string_vs_nonstring_shortcut_still_compiles_both_operands_for_effect() {
let form = r#"(progn (defparameter x 0)
(eq? (progn (setf x (+ x 1)) "hi") (rpc-protocol-version))
x)"#;
async fn string_vs_nonstring_shortcut_still_folds_to_nil() {
let form = r#"(eq? "hi" (rpc-protocol-version))"#;
assert_values(&[(form, "nil")]).await;
// --- item 6: the numeric equality fallback (shared by `=`/`<`/`>`/`<=`/
// `>=`/`/=`/EQ?'s numeric path) classified operands via `resolve_all` on the
// LIVE table, then compiled the ORIGINALS again for the runtime path —
// applying a nested `setf` TWICE. `resolve_all` now probes on one disposable
// clone; the all-constant fold path compiles the originals for effect
// exactly once before emitting the fold. ---
async fn numeric_equality_fallback_does_not_double_apply_a_runtime_operands_effects() {
let form = "(progn (defparameter x 0) \
(eq? (progn (setf x (+ x 1)) (rpc-protocol-version)) 0) \
async fn numeric_equality_all_constant_fold_still_applies_effects_exactly_once() {
let form = "(let ((x 0)) (= (progn (setf x (+ x 1)) 3) 3) x)";
// --- item 7: `compile_cross_type_nil_identity`'s "different static types
// are disjoint" assumption is false for `AnyRef` (the ADR-0025 widen
// target), which can literally box the `t` singleton — a runtime `Bool`
// true compared against an `AnyRef` holding `t` (e.g. from an `IF` whose
// arms disagree in type) must be `t`, not `nil`. Fixed for both operand
// orders via `compile_bool_anyref_identity`. ---
async fn bool_true_equals_an_anyref_boxing_t() {
let form = "(eq? (> (+ (rpc-protocol-version) 1) 0) \
(if (> (+ (rpc-protocol-version) 1) 0) t 7))";
assert_values(&[(form, "t")]).await;
async fn anyref_boxing_t_equals_bool_true_reversed_operand_order() {
let form = "(eq? (if (> (+ (rpc-protocol-version) 1) 0) t 7) \
(> (+ (rpc-protocol-version) 1) 0))";
async fn bool_false_equals_an_anyref_boxing_nil() {
let form = "(eq? (> 0 (+ (rpc-protocol-version) 1)) \
(if (> 0 (+ (rpc-protocol-version) 1)) t nil))";
async fn bool_false_does_not_equal_an_anyref_boxing_t() {
// --- item 8: a bodyless RUNTIME COND test's eval-side effects were only
// ever applied on a disposable clone (for classification into the
// placeholder `if_form` reconciles arms against) — the LIVE table never saw
// a `defvar` the test introduced, so a later clause referencing it failed
// "undefined symbol" on the eval-mirror surface even though codegen (which
// compiles the test for real before the continuation) had no such bug.
// Fixed in `if_form::if_form`'s runtime-test branch (covers COND's
// with-body delegation too) and explicitly in `cond_form`'s bodyless path
// (which hands `if_form` a placeholder, not the original test). ---
async fn bodyless_runtime_cond_test_definition_is_visible_to_a_later_clause() {
let form = "(let ((v (cond ((progn (defvar codex-r5-z 7) (> (rpc-protocol-version) 5))) \
(t codex-r5-z)))) \
v)";