1
//! Found in Phase 7 (`doc/ops/0010-common-lisp-booleans.org`) review: a
2
//! plain user `DEFUN` wrapping a runtime host-fn call, itself wrapping a
3
//! `setf`-counted effect, applied that effect FOUR times instead of once.
4
//!
5
//! `compile_for_effect`'s generic call dispatch classifies the whole call on
6
//! `eval_value` — that classification must be a pure, disposable probe, but
7
//! it (and two downstream classification probes in `compiler/expr/call.rs`:
8
//! `arg_resolves_runtime` / `infer_arg_type`) ran on the LIVE symbol table
9
//! instead of a clone, so each one independently re-applied the argument's
10
//! own compile-time effects (a nested `setf`) before the real emission
11
//! (`compile_arg_for_param`'s `compile_for_stack`) applied it a fourth time.
12
//!
13
//! `(eq? nil v)` (Phase 7's own arm) evaluates `v` through this exact path
14
//! when `v` is `(some-defun (progn (setf counter (+ counter 1)) (host-fn)))`,
15
//! which is how this surfaced during Phase 7 review — but the bug is in the
16
//! shared call-classification machinery, not in Phase 7's arm itself, so
17
//! these tests exercise it directly (with and without the `eq?` wrapper).
18

            
19
use rpc::{ScriptCtx, Session};
20
use uuid::Uuid;
21

            
22
4
async fn rpc_response(form: &str) -> String {
23
4
    let mut session = Session::new(ScriptCtx::new(Uuid::new_v4())).unwrap();
24
4
    session.handle_form(&format!("(:id 1 :form {form})")).await
25
4
}
26

            
27
4
fn extract_value(response: &str) -> &str {
28
4
    response
29
4
        .split_once(":value ")
30
4
        .map(|(_, rest)| rest.trim_end_matches(')').trim())
31
4
        .unwrap_or(response)
32
4
}
33

            
34
#[tokio::test(flavor = "current_thread")]
35
1
async fn a_defun_wrapping_a_runtime_host_call_runs_the_nested_setf_exactly_once() {
36
1
    let resp = rpc_response(
37
1
        "(progn (defparameter p7-count 0) (defun p7-id (v) v) \
38
1
         (eq? nil (p7-id (progn (setf p7-count (+ p7-count 1)) (rpc-protocol-version)))) \
39
1
         p7-count)",
40
1
    )
41
1
    .await;
42
1
    assert_eq!(extract_value(&resp), "1", "got: {resp}");
43
1
}
44

            
45
#[tokio::test(flavor = "current_thread")]
46
1
async fn the_nested_setf_runs_once_regardless_of_eq_operand_order() {
47
1
    let resp = rpc_response(
48
1
        "(progn (defparameter p7-count 0) (defun p7-id (v) v) \
49
1
         (eq? (p7-id (progn (setf p7-count (+ p7-count 1)) (rpc-protocol-version))) nil) \
50
1
         p7-count)",
51
1
    )
52
1
    .await;
53
1
    assert_eq!(extract_value(&resp), "1", "got: {resp}");
54
1
}
55

            
56
/// Not wrapped in `eq?` at all — the shared call-classification machinery
57
/// itself is the bug, not Phase 7's arm; drop the `eq?` operand entirely to
58
/// confirm the effect-position DEFUN call path alone was already affected.
59
#[tokio::test(flavor = "current_thread")]
60
1
async fn a_defun_call_in_effect_position_runs_its_nested_setf_exactly_once() {
61
1
    let resp = rpc_response(
62
1
        "(progn (defparameter p7-count 0) (defun p7-id (v) v) \
63
1
         (p7-id (progn (setf p7-count (+ p7-count 1)) (rpc-protocol-version))) \
64
1
         p7-count)",
65
1
    )
66
1
    .await;
67
1
    assert_eq!(extract_value(&resp), "1", "got: {resp}");
68
1
}
69

            
70
/// The whole call folds to a compile-time constant (the arg's `progn` folds
71
/// to a literal, not a host-fn placeholder) — a SEPARATE fallthrough path in
72
/// `compile_for_effect` (`compile_for_stack` + drop), which must also apply
73
/// the nested `setf` exactly once.
74
#[tokio::test(flavor = "current_thread")]
75
1
async fn a_folded_defun_call_still_runs_its_nested_setf_exactly_once() {
76
1
    let resp = rpc_response(
77
1
        "(progn (defparameter cnt 0) (defun p7-id (v) v) \
78
1
         (p7-id (progn (setf cnt (+ cnt 1)) 42)) \
79
1
         cnt)",
80
1
    )
81
1
    .await;
82
1
    assert_eq!(extract_value(&resp), "1", "got: {resp}");
83
1
}