1
//! Phase 4 (doc/ops/0010-common-lisp-booleans.org) special-form dispatch
2
//! regressions — NOT BLOCK exit typing, which is its own later slice (see
3
//! `doc/ops/0010-common-lisp-booleans.org`'s Phase 4 follow-up entry): every
4
//! special form with a `stack`/`effect` compiler must dispatch through
5
//! `special::compile_for_effect`, derived from the registry
6
//! (`is_eval_only_special_form`), not a hand-maintained name list that
7
//! silently drops wasm (and once missed `CATCH-EACH`, and categorically
8
//! couldn't cover `ERROR`, which has no dedicated effect handler of its
9
//! own). `n` stands for `(rpc-protocol-version)`, which is 1.
10

            
11
use super::*;
12

            
13
// --- every special form with a stack/effect compiler reaches real compile
14
// in effect position, not the eval-only fallback that never throws. ---
15

            
16
#[tokio::test]
17
1
async fn error_in_setf_rhs_is_caught() {
18
1
    let form = "(handler-case (let ((y 0)) (setf y (error 'x \"m\")) y) (x (e) 4))";
19
1
    assert_values(&[(form, "4")]).await;
20
1
}
21

            
22
#[tokio::test]
23
1
async fn error_in_let_body_is_caught() {
24
    // Pre-existing root cause: `(error …)` alone in a `LET` body (no SETF
25
    // involved at all) previously fell through to the eval-only fallback.
26
1
    let form = "(handler-case (let ((y 0)) (error 'x \"m\") y) (x (e) 4))";
27
1
    assert_values(&[(form, "4")]).await;
28
1
}
29

            
30
#[tokio::test]
31
1
async fn error_in_progn_is_caught() {
32
1
    let form = "(handler-case (progn (error 'x \"m\") 1) (x (e) 4))";
33
1
    assert_values(&[(form, "4")]).await;
34
1
}
35

            
36
#[tokio::test]
37
1
async fn error_in_if_effect_position_is_caught() {
38
1
    let form = "(handler-case (let ((y 0)) (if (> (rpc-protocol-version) 0) (setf y (error 'x \"m\"))) y) (x (e) 4))";
39
1
    assert_values(&[(form, "4")]).await;
40
1
}
41

            
42
#[tokio::test]
43
1
async fn error_in_dolist_body_is_caught() {
44
    let form =
45
1
        "(handler-case (let ((y 0)) (dolist (i (list 1)) (setf y (error 'x \"m\"))) y) (x (e) 4))";
46
1
    assert_values(&[(form, "4")]).await;
47
1
}
48

            
49
#[tokio::test]
50
1
async fn error_in_effect_position_is_caught() {
51
1
    let form = "(handler-case (progn (error 'x \"m\") 1) (x (e) 4))";
52
1
    assert_values(&[(form, "4")]).await;
53
1
}
54

            
55
// --- LABELS is not a pure definition form — it compiles and runs its body
56
// inline (its `SpecialFormSpec` declares a `stack` producer, unlike
57
// DEFUN/DEFSTRUCT/DEFMACRO/QUOTE which are eval-only), so it must reach
58
// real compilation in effect position too. ---
59

            
60
#[tokio::test]
61
1
async fn labels_body_runs_in_effect_position() {
62
1
    let form = "(let ((y 0)) (labels ((f (x) (setf y x))) (f 3)) y)";
63
1
    assert_values(&[(form, "3")]).await;
64
1
}
65

            
66
#[tokio::test]
67
1
async fn error_inside_a_labels_body_in_effect_position_is_caught() {
68
1
    let form = "(handler-case (progn (labels ((f (x) (error 'x \"m\"))) (f 1)) 1) (x (e) 4))";
69
1
    assert_values(&[(form, "4")]).await;
70
1
}
71

            
72
// --- a `stack`-producing reflective form (EVAL/COMPILE/DESCRIBE/PP/
73
// APROPOS/DEFTEST/ASSERT-EQUAL/RUN-TESTS/COVERAGE-DUMP) in effect position
74
// compiles via its OWN stack path (or, for EVAL, re-dispatches its resolved
75
// code through the SAME effect compiler) and drops the result, instead of
76
// its value-position `compile` fallback (which would serialize the
77
// discarded value onto the RPC output stream ahead of the program's real
78
// result). ---
79

            
80
#[tokio::test]
81
1
async fn eval_in_effect_position_does_not_leak_an_extra_output_entry() {
82
1
    let form = "(progn (eval '(+ 1 2)) 5)";
83
1
    assert_values(&[(form, "5")]).await;
84
1
}
85

            
86
#[tokio::test]
87
1
async fn eval_of_a_defvar_in_effect_position_registers_it() {
88
    // Fable round-4 finding 5: EVAL's effect path re-dispatches its resolved
89
    // code through `compile_for_effect`, which must handle a DEFVAR (a
90
    // definition form, not a stack value) correctly — this used to raise
91
    // "Undefined symbol: *EV*" because the naive fix routed EVAL's resolved
92
    // code through a stack-only path.
93
1
    let form = "(progn (eval (quote (defvar *ev* 9))) *ev*)";
94
1
    assert_values(&[(form, "9")]).await;
95
1
}
96

            
97
#[tokio::test]
98
1
async fn describe_in_effect_position_does_not_leak_an_extra_output_entry() {
99
1
    let form = "(progn (describe 'entity-count) 5)";
100
1
    assert_values(&[(form, "5")]).await;
101
1
}
102

            
103
#[tokio::test]
104
1
async fn pp_in_effect_position_does_not_leak_an_extra_output_entry() {
105
1
    let form = "(progn (pp 5) 5)";
106
1
    assert_values(&[(form, "5")]).await;
107
1
}
108

            
109
#[tokio::test]
110
1
async fn apropos_in_effect_position_does_not_leak_an_extra_output_entry() {
111
1
    let form = "(progn (apropos \"entity-count\") 5)";
112
1
    assert_values(&[(form, "5")]).await;
113
1
}
114

            
115
#[tokio::test]
116
1
async fn coverage_dump_in_effect_position_does_not_leak_an_extra_output_entry() {
117
1
    let form = "(progn (coverage-dump) 5)";
118
1
    assert_values(&[(form, "5")]).await;
119
1
}