Lines
100 %
Functions
Branches
//! `SETF` of a compile-time (non-`WasmLocal`-backed) place — a `DEFVAR`/
//! `DEFPARAMETER` global, or a struct field — whose RHS is a genuinely
//! RUNTIME value (a host-fn call, a nested effect, …).
//!
//! Found while implementing Phase 7 of `doc/ops/0010-common-lisp-booleans.org`:
//! that phase's `(eq? nil v)` arm routes `v` through `compile_for_effect`,
//! which made this pre-existing gap reachable through a form Phase 7 itself
//! introduced (`(eq? nil (setf <const-place> <runtime-rhs>))`). The bug
//! predates Phase 7 — `compile_for_effect(setf p 0) (setf p (<host-fn>)) p)`
//! alone already failed — but fixing it is in scope per CLAUDE.md ("fix
//! every bug discovered, even one that merely produces a wrong value").
//! `compile_setf_for_effect` (`compiler/expr/effect.rs`) and
//! `compile_setf_for_stack` (`compiler/special/structure.rs`) used to take
//! a const-backed place's RHS through `eval_value` ALONE unless it
//! contained a nested `setf` to an EXISTING `WasmLocal` — `eval_value` never
//! emits wasm, so a host-fn call (or any other genuinely runtime RHS) was
//! silently never invoked, and the place's compile-time value became a bare
//! `WasmRuntime` placeholder with no backing storage — surfacing later as
//! an opaque `wasm[..]::function[..]` module-validation failure at RUNTIME,
//! not a clean compile error.
//! The fix (`compile_const_place_setf_rhs`, shared by both codegen paths)
//! mirrors `DEFVAR`/`DEFPARAMETER`'s own promotion
//! (`compiler::special::binding::define::finish_def_binding`): a runtime
//! RHS is compiled for real and, for a plain symbol place, promoted into a
//! backing `WasmLocal`; a compound (struct-field) place never reaches it and
//! stores through its runtime setter instead.
#![cfg(not(miri))]
use nms::interpreter::Interpreter;
use scripting::nomiscript::{Number, Value};
#[test]
fn setf_of_a_defparameter_to_a_runtime_host_call_compiles_and_runs() {
let mut interp = Interpreter::new(false).unwrap();
assert_eq!(
interp
.eval("(defparameter p 0) (setf p (transaction-tag-count 0)) p")
.unwrap(),
vec![Value::Number(Number::Index(0))]
);
}
/// The regression proper: the RHS's own side effect (a nested `setf` on a
/// second global, counted) must run — exactly once — even though the
/// PLACE it's being assigned into has no runtime storage yet.
fn setf_of_a_defparameter_runs_the_runtime_rhs_effect_exactly_once() {
.eval(
"(defparameter p 0) (defparameter cnt 0) \
(setf p (progn (setf cnt (+ cnt 1)) (transaction-tag-count 0))) \
cnt"
)
vec![Value::Number(Number::Index(1))]
/// After a runtime-valued SETF promotes the place to a real `WasmLocal`, a
/// SECOND SETF on the same place must ride the ordinary runtime-local store
/// path (not re-enter the const-place promotion), so its own value sticks.
fn a_second_setf_after_promotion_uses_the_promoted_local() {
"(defparameter p 0) \
(setf p (transaction-tag-count 0)) \
(setf p (+ p 41)) \
p"
vec![Value::Number(Number::Index(41))]
/// A compound (struct-field) place stores a runtime RHS into the instance
/// itself, so the field reads back what the host fn returned.
fn setf_of_a_struct_field_to_a_runtime_value_stores_it() {
let values = interp
"(defstruct point x y) \
(let* ((p (make-point :x 1 :y 2))) \
(setf (point-x p) (transaction-tag-count 0)) (point-x p))",
.unwrap();
assert_eq!(values.last(), Some(&Value::Number(Number::Index(0))));