Lines
100 %
Functions
Branches
//! Found in Phase 7 (`doc/ops/0010-common-lisp-booleans.org`) round-2
//! review: three more effect-ordering bugs in the shared call / SETF
//! machinery reworked by that phase's `eq?`-never-nil fix. All three are
//! about WHEN an argument's or a nested assignment's real effect applies
//! relative to storage that only gets created (or reused) partway through
//! compiling the SAME call/assignment.
//!
//! `n` below is `(rpc-protocol-version)`, which is `1`.
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)
/// `compile_arg_for_param` (`compiler/expr/call.rs`) reused an argument's
/// existing storage whenever it happened to RESOLVE to a `WasmLocal` — but a
/// compound argument expression can resolve to a `WasmLocal` only in its
/// FINAL form (e.g. `PROGN` returns its last form's value) while still
/// carrying its own real effects earlier in the same expression. Reusing
/// the storage instead of compiling the argument for real dropped those
/// effects entirely.
#[tokio::test(flavor = "current_thread")]
async fn a_progn_argument_ending_in_a_local_still_runs_its_own_effect() {
let resp = rpc_response(
"(progn (defun p7-id (v) v) \
(let ((n (rpc-protocol-version)) (hits 0)) \
(eq? nil (p7-id (progn (setf hits (+ hits 1)) n))) hits))",
)
.await;
assert_eq!(extract_value(&resp), "1", "got: {resp}");
/// The other half of the same bug: reusing the SOURCE local's storage
/// (rather than snapshotting into a fresh one) means a LATER argument that
/// mutates that same source variable retroactively changes an EARLIER
/// argument's already-bound value.
async fn an_earlier_argument_is_snapshotted_before_a_later_one_mutates_its_source() {
"(progn (defun p7-both (a b) a) \
(let ((n (rpc-protocol-version))) \
(p7-both n (progn (setf n 99) n))))",
/// `compile_const_place_setf_rhs` (`compiler/special/structure.rs`)
/// classified the SETF destination BEFORE compiling the RHS — but compiling
/// the RHS can itself promote that SAME destination to a real `WasmLocal`
/// (a nested SETF on the same place). The outer assignment then went
/// through `set_place`, which deliberately no-ops on an already
/// runtime-backed place (see its own doc comment) — silently dropping the
/// outer value in EFFECT position.
async fn a_setf_whose_rhs_promotes_its_own_destination_still_stores_its_own_value() {
"(progn (defparameter p7-x 0) \
(setf p7-x (progn (setf p7-x (rpc-protocol-version)) 2)) \
p7-x)",
assert_eq!(extract_value(&resp), "2", "got: {resp}");
/// Same bug, STACK position (`compile_setf_for_stack`): the outer SETF is
/// itself used as a value (here, an operand of `+`), which takes a
/// different codegen path than the effect-position case above and needed
/// the identical destination re-check.
async fn the_same_promotion_race_is_fixed_in_stack_position_too() {
"(progn (defparameter p7-y 0) \
(+ (setf p7-y (progn (setf p7-y (rpc-protocol-version)) 2)) 0) \
p7-y)",
/// `compile_and_bind_lambda_params` (`compiler/expr/call.rs`) cloned the
/// caller's symbol table into the callee's body environment BEFORE
/// compiling any argument — so an argument's own compile-time effect (here,
/// a `setf` on a variable the callee body itself reads) never reached the
/// body's environment. `eval.rs`'s `call_lambda` (the eval-only mirror) had
/// the identical premature snapshot.
async fn a_defun_body_sees_an_argument_side_mutation() {
"(progn (defparameter p7-c 0) (defun p7-read (unused) p7-c) \
(p7-read (progn (setf p7-c 1) 0)))",
/// Same bug, a DEFINITION (not just a mutation) introduced by an argument:
/// the callee body references a variable the argument itself just defined.
async fn a_defun_body_sees_an_argument_side_definition() {
"(progn (defun p7-read2 (unused) p7-newvar) \
(p7-read2 (progn (defvar p7-newvar 42) 0)))",
assert_eq!(extract_value(&resp), "42", "got: {resp}");