1
//! Found in Phase 7 (`doc/ops/0010-common-lisp-booleans.org`) round-2
2
//! review: three more effect-ordering bugs in the shared call / SETF
3
//! machinery reworked by that phase's `eq?`-never-nil fix. All three are
4
//! about WHEN an argument's or a nested assignment's real effect applies
5
//! relative to storage that only gets created (or reused) partway through
6
//! compiling the SAME call/assignment.
7
//!
8
//! `n` below is `(rpc-protocol-version)`, which is `1`.
9

            
10
use rpc::{ScriptCtx, Session};
11
use uuid::Uuid;
12

            
13
6
async fn rpc_response(form: &str) -> String {
14
6
    let mut session = Session::new(ScriptCtx::new(Uuid::new_v4())).unwrap();
15
6
    session.handle_form(&format!("(:id 1 :form {form})")).await
16
6
}
17

            
18
6
fn extract_value(response: &str) -> &str {
19
6
    response
20
6
        .split_once(":value ")
21
6
        .map(|(_, rest)| rest.trim_end_matches(')').trim())
22
6
        .unwrap_or(response)
23
6
}
24

            
25
/// `compile_arg_for_param` (`compiler/expr/call.rs`) reused an argument's
26
/// existing storage whenever it happened to RESOLVE to a `WasmLocal` — but a
27
/// compound argument expression can resolve to a `WasmLocal` only in its
28
/// FINAL form (e.g. `PROGN` returns its last form's value) while still
29
/// carrying its own real effects earlier in the same expression. Reusing
30
/// the storage instead of compiling the argument for real dropped those
31
/// effects entirely.
32
#[tokio::test(flavor = "current_thread")]
33
1
async fn a_progn_argument_ending_in_a_local_still_runs_its_own_effect() {
34
1
    let resp = rpc_response(
35
1
        "(progn (defun p7-id (v) v) \
36
1
         (let ((n (rpc-protocol-version)) (hits 0)) \
37
1
           (eq? nil (p7-id (progn (setf hits (+ hits 1)) n))) hits))",
38
1
    )
39
1
    .await;
40
1
    assert_eq!(extract_value(&resp), "1", "got: {resp}");
41
1
}
42

            
43
/// The other half of the same bug: reusing the SOURCE local's storage
44
/// (rather than snapshotting into a fresh one) means a LATER argument that
45
/// mutates that same source variable retroactively changes an EARLIER
46
/// argument's already-bound value.
47
#[tokio::test(flavor = "current_thread")]
48
1
async fn an_earlier_argument_is_snapshotted_before_a_later_one_mutates_its_source() {
49
1
    let resp = rpc_response(
50
1
        "(progn (defun p7-both (a b) a) \
51
1
         (let ((n (rpc-protocol-version))) \
52
1
           (p7-both n (progn (setf n 99) n))))",
53
1
    )
54
1
    .await;
55
1
    assert_eq!(extract_value(&resp), "1", "got: {resp}");
56
1
}
57

            
58
/// `compile_const_place_setf_rhs` (`compiler/special/structure.rs`)
59
/// classified the SETF destination BEFORE compiling the RHS — but compiling
60
/// the RHS can itself promote that SAME destination to a real `WasmLocal`
61
/// (a nested SETF on the same place). The outer assignment then went
62
/// through `set_place`, which deliberately no-ops on an already
63
/// runtime-backed place (see its own doc comment) — silently dropping the
64
/// outer value in EFFECT position.
65
#[tokio::test(flavor = "current_thread")]
66
1
async fn a_setf_whose_rhs_promotes_its_own_destination_still_stores_its_own_value() {
67
1
    let resp = rpc_response(
68
1
        "(progn (defparameter p7-x 0) \
69
1
         (setf p7-x (progn (setf p7-x (rpc-protocol-version)) 2)) \
70
1
         p7-x)",
71
1
    )
72
1
    .await;
73
1
    assert_eq!(extract_value(&resp), "2", "got: {resp}");
74
1
}
75

            
76
/// Same bug, STACK position (`compile_setf_for_stack`): the outer SETF is
77
/// itself used as a value (here, an operand of `+`), which takes a
78
/// different codegen path than the effect-position case above and needed
79
/// the identical destination re-check.
80
#[tokio::test(flavor = "current_thread")]
81
1
async fn the_same_promotion_race_is_fixed_in_stack_position_too() {
82
1
    let resp = rpc_response(
83
1
        "(progn (defparameter p7-y 0) \
84
1
         (+ (setf p7-y (progn (setf p7-y (rpc-protocol-version)) 2)) 0) \
85
1
         p7-y)",
86
1
    )
87
1
    .await;
88
1
    assert_eq!(extract_value(&resp), "2", "got: {resp}");
89
1
}
90

            
91
/// `compile_and_bind_lambda_params` (`compiler/expr/call.rs`) cloned the
92
/// caller's symbol table into the callee's body environment BEFORE
93
/// compiling any argument — so an argument's own compile-time effect (here,
94
/// a `setf` on a variable the callee body itself reads) never reached the
95
/// body's environment. `eval.rs`'s `call_lambda` (the eval-only mirror) had
96
/// the identical premature snapshot.
97
#[tokio::test(flavor = "current_thread")]
98
1
async fn a_defun_body_sees_an_argument_side_mutation() {
99
1
    let resp = rpc_response(
100
1
        "(progn (defparameter p7-c 0) (defun p7-read (unused) p7-c) \
101
1
         (p7-read (progn (setf p7-c 1) 0)))",
102
1
    )
103
1
    .await;
104
1
    assert_eq!(extract_value(&resp), "1", "got: {resp}");
105
1
}
106

            
107
/// Same bug, a DEFINITION (not just a mutation) introduced by an argument:
108
/// the callee body references a variable the argument itself just defined.
109
#[tokio::test(flavor = "current_thread")]
110
1
async fn a_defun_body_sees_an_argument_side_definition() {
111
1
    let resp = rpc_response(
112
1
        "(progn (defun p7-read2 (unused) p7-newvar) \
113
1
         (p7-read2 (progn (defvar p7-newvar 42) 0)))",
114
1
    )
115
1
    .await;
116
1
    assert_eq!(extract_value(&resp), "42", "got: {resp}");
117
1
}