1
//! `SETF` of a compile-time (non-`WasmLocal`-backed) place — a `DEFVAR`/
2
//! `DEFPARAMETER` global, or a struct field — whose RHS is a genuinely
3
//! RUNTIME value (a host-fn call, a nested effect, …).
4
//!
5
//! Found while implementing Phase 7 of `doc/ops/0010-common-lisp-booleans.org`:
6
//! that phase's `(eq? nil v)` arm routes `v` through `compile_for_effect`,
7
//! which made this pre-existing gap reachable through a form Phase 7 itself
8
//! introduced (`(eq? nil (setf <const-place> <runtime-rhs>))`). The bug
9
//! predates Phase 7 — `compile_for_effect(setf p 0) (setf p (<host-fn>)) p)`
10
//! alone already failed — but fixing it is in scope per CLAUDE.md ("fix
11
//! every bug discovered, even one that merely produces a wrong value").
12
//!
13
//! `compile_setf_for_effect` (`compiler/expr/effect.rs`) and
14
//! `compile_setf_for_stack` (`compiler/special/structure.rs`) used to take
15
//! a const-backed place's RHS through `eval_value` ALONE unless it
16
//! contained a nested `setf` to an EXISTING `WasmLocal` — `eval_value` never
17
//! emits wasm, so a host-fn call (or any other genuinely runtime RHS) was
18
//! silently never invoked, and the place's compile-time value became a bare
19
//! `WasmRuntime` placeholder with no backing storage — surfacing later as
20
//! an opaque `wasm[..]::function[..]` module-validation failure at RUNTIME,
21
//! not a clean compile error.
22
//!
23
//! The fix (`compile_const_place_setf_rhs`, shared by both codegen paths)
24
//! mirrors `DEFVAR`/`DEFPARAMETER`'s own promotion
25
//! (`compiler::special::binding::define::finish_def_binding`): a runtime
26
//! RHS is compiled for real and, for a plain symbol place, promoted into a
27
//! backing `WasmLocal`; a compound (struct-field) place never reaches it and
28
//! stores through its runtime setter instead.
29

            
30
#![cfg(not(miri))]
31

            
32
use nms::interpreter::Interpreter;
33
use scripting::nomiscript::{Number, Value};
34

            
35
#[test]
36
1
fn setf_of_a_defparameter_to_a_runtime_host_call_compiles_and_runs() {
37
1
    let mut interp = Interpreter::new(false).unwrap();
38
1
    assert_eq!(
39
1
        interp
40
1
            .eval("(defparameter p 0) (setf p (transaction-tag-count 0)) p")
41
1
            .unwrap(),
42
1
        vec![Value::Number(Number::Index(0))]
43
    );
44
1
}
45

            
46
/// The regression proper: the RHS's own side effect (a nested `setf` on a
47
/// second global, counted) must run — exactly once — even though the
48
/// PLACE it's being assigned into has no runtime storage yet.
49
#[test]
50
1
fn setf_of_a_defparameter_runs_the_runtime_rhs_effect_exactly_once() {
51
1
    let mut interp = Interpreter::new(false).unwrap();
52
1
    assert_eq!(
53
1
        interp
54
1
            .eval(
55
1
                "(defparameter p 0) (defparameter cnt 0) \
56
1
                 (setf p (progn (setf cnt (+ cnt 1)) (transaction-tag-count 0))) \
57
1
                 cnt"
58
1
            )
59
1
            .unwrap(),
60
1
        vec![Value::Number(Number::Index(1))]
61
    );
62
1
}
63

            
64
/// After a runtime-valued SETF promotes the place to a real `WasmLocal`, a
65
/// SECOND SETF on the same place must ride the ordinary runtime-local store
66
/// path (not re-enter the const-place promotion), so its own value sticks.
67
#[test]
68
1
fn a_second_setf_after_promotion_uses_the_promoted_local() {
69
1
    let mut interp = Interpreter::new(false).unwrap();
70
1
    assert_eq!(
71
1
        interp
72
1
            .eval(
73
1
                "(defparameter p 0) \
74
1
                 (setf p (transaction-tag-count 0)) \
75
1
                 (setf p (+ p 41)) \
76
1
                 p"
77
1
            )
78
1
            .unwrap(),
79
1
        vec![Value::Number(Number::Index(41))]
80
    );
81
1
}
82

            
83
/// A compound (struct-field) place stores a runtime RHS into the instance
84
/// itself, so the field reads back what the host fn returned.
85
#[test]
86
1
fn setf_of_a_struct_field_to_a_runtime_value_stores_it() {
87
1
    let mut interp = Interpreter::new(false).unwrap();
88
1
    let values = interp
89
1
        .eval(
90
1
            "(defstruct point x y) \
91
1
             (let* ((p (make-point :x 1 :y 2))) \
92
1
               (setf (point-x p) (transaction-tag-count 0)) (point-x p))",
93
1
        )
94
1
        .unwrap();
95
1
    assert_eq!(values.last(), Some(&Value::Number(Number::Index(0))));
96
1
}