Lines
100 %
Functions
Branches
//! Found in Phase 7 (`doc/ops/0010-common-lisp-booleans.org`) review: a
//! plain user `DEFUN` wrapping a runtime host-fn call, itself wrapping a
//! `setf`-counted effect, applied that effect FOUR times instead of once.
//!
//! `compile_for_effect`'s generic call dispatch classifies the whole call on
//! `eval_value` — that classification must be a pure, disposable probe, but
//! it (and two downstream classification probes in `compiler/expr/call.rs`:
//! `arg_resolves_runtime` / `infer_arg_type`) ran on the LIVE symbol table
//! instead of a clone, so each one independently re-applied the argument's
//! own compile-time effects (a nested `setf`) before the real emission
//! (`compile_arg_for_param`'s `compile_for_stack`) applied it a fourth time.
//! `(eq? nil v)` (Phase 7's own arm) evaluates `v` through this exact path
//! when `v` is `(some-defun (progn (setf counter (+ counter 1)) (host-fn)))`,
//! which is how this surfaced during Phase 7 review — but the bug is in the
//! shared call-classification machinery, not in Phase 7's arm itself, so
//! these tests exercise it directly (with and without the `eq?` wrapper).
use rpc::{ScriptCtx, Session};
use uuid::Uuid;
async fn rpc_response(form: &str) -> String {
let mut session = Session::new(ScriptCtx::new(Uuid::new_v4())).unwrap();
session.handle_form(&format!("(:id 1 :form {form})")).await
}
fn extract_value(response: &str) -> &str {
response
.split_once(":value ")
.map(|(_, rest)| rest.trim_end_matches(')').trim())
.unwrap_or(response)
#[tokio::test(flavor = "current_thread")]
async fn a_defun_wrapping_a_runtime_host_call_runs_the_nested_setf_exactly_once() {
let resp = rpc_response(
"(progn (defparameter p7-count 0) (defun p7-id (v) v) \
(eq? nil (p7-id (progn (setf p7-count (+ p7-count 1)) (rpc-protocol-version)))) \
p7-count)",
)
.await;
assert_eq!(extract_value(&resp), "1", "got: {resp}");
async fn the_nested_setf_runs_once_regardless_of_eq_operand_order() {
(eq? (p7-id (progn (setf p7-count (+ p7-count 1)) (rpc-protocol-version))) nil) \
/// Not wrapped in `eq?` at all — the shared call-classification machinery
/// itself is the bug, not Phase 7's arm; drop the `eq?` operand entirely to
/// confirm the effect-position DEFUN call path alone was already affected.
async fn a_defun_call_in_effect_position_runs_its_nested_setf_exactly_once() {
(p7-id (progn (setf p7-count (+ p7-count 1)) (rpc-protocol-version))) \
/// The whole call folds to a compile-time constant (the arg's `progn` folds
/// to a literal, not a host-fn placeholder) — a SEPARATE fallthrough path in
/// `compile_for_effect` (`compile_for_stack` + drop), which must also apply
/// the nested `setf` exactly once.
async fn a_folded_defun_call_still_runs_its_nested_setf_exactly_once() {
"(progn (defparameter cnt 0) (defun p7-id (v) v) \
(p7-id (progn (setf cnt (+ cnt 1)) 42)) \
cnt)",