1
//! Codex adversarial review of the codex-round-3 AND/OR definition
2
//! rejection (this session): the syntactic pre-scan (`find_definition_form`
3
//! over the guarded suffix) was both too broad (rejected a definition a
4
//! constant operand earlier in the SAME suffix already makes unreachable)
5
//! and too narrow (blind to macro expansion and to nesting other than
6
//! `PROGN`/`AND`/`OR`). Replaced with a check at each definition form's OWN
7
//! registration site (`SymbolTable::is_under_runtime_and_or_guard`,
8
//! `reject_definition_under_runtime_guard` in `compiler/special/mod.rs`),
9
//! gated on `and_or_depth` — the SAME counter `enter_and_or_chain` already
10
//! sets around a genuinely-runtime AND/OR continuation, in BOTH effect and
11
//! stack position. `n` stands for `(rpc-protocol-version)`, which is 1.
12

            
13
use super::*;
14

            
15
// --- item 1 (too broad): a definition after a CONSTANT operand that
16
// itself decides the chain (a `nil` in AND, a `t` in OR) is unreachable —
17
// short-circuiting means the definition is never even compiled, so it must
18
// never be rejected. ---
19

            
20
#[tokio::test]
21
1
async fn and_never_reaches_a_definition_past_a_constant_nil() {
22
1
    let form = "(progn (and (> (rpc-protocol-version) 0) nil (defvar unreachable-a 7)) 42)";
23
1
    assert_values(&[(form, "42")]).await;
24
1
}
25

            
26
#[tokio::test]
27
1
async fn or_never_reaches_a_definition_past_a_constant_t() {
28
1
    let form = "(progn (or (> (rpc-protocol-version) 0) t (defvar unreachable-b 7)) 42)";
29
1
    assert_values(&[(form, "42")]).await;
30
1
}
31

            
32
#[tokio::test]
33
1
async fn and_never_reaches_a_definition_past_a_constant_nil_deeper_in_the_chain() {
34
    // The constant-false decider is not immediately after the runtime
35
    // operand — a longer chain must short-circuit past it just the same.
36
1
    let form = "(progn (and (> (rpc-protocol-version) 0) 1 2 nil (defvar unreachable-c 7)) 42)";
37
1
    assert_values(&[(form, "42")]).await;
38
1
}
39

            
40
#[tokio::test]
41
1
async fn or_never_reaches_a_definition_past_a_constant_t_deeper_in_the_chain() {
42
1
    let form = "(progn (or (> (rpc-protocol-version) 0) nil nil t (defvar unreachable-d 7)) 42)";
43
1
    assert_values(&[(form, "42")]).await;
44
1
}
45

            
46
// A definition that IS genuinely reachable after a non-deciding constant
47
// operand (one that doesn't short-circuit) is still correctly rejected —
48
// the short-circuit fix must not swallow the real bug.
49
#[tokio::test]
50
1
async fn and_still_rejects_a_definition_genuinely_reached_past_a_truthy_constant() {
51
1
    let form = "(progn (and (> (rpc-protocol-version) 0) 5 (defvar reached-a 7)) 42)";
52
1
    assert_error_code(form, "compile").await;
53
1
}
54

            
55
// --- item 2 (too narrow): a definition the OLD syntactic pre-scan could
56
// not see — reached through macro expansion, or nested in a form other
57
// than PROGN/AND/OR — must still be rejected, because the check now lives
58
// at the definition's own registration site rather than a scan of source
59
// syntax. ---
60

            
61
#[tokio::test]
62
1
async fn definition_reached_through_a_macro_expansion_is_still_rejected() {
63
1
    let form = "(progn (defparameter x 0) \
64
1
                 (defmacro define-x () (quote (defparameter x 7))) \
65
1
                 (and (> (rpc-protocol-version) 5) (define-x)) x)";
66
1
    assert_error_code(form, "compile").await;
67
1
}
68

            
69
#[tokio::test]
70
1
async fn definition_nested_inside_if_under_the_guard_is_still_rejected() {
71
1
    let form = "(progn (defparameter x 0) \
72
1
                 (and (> (rpc-protocol-version) 5) (if t (defparameter x 7))) x)";
73
1
    assert_error_code(form, "compile").await;
74
1
}
75

            
76
#[tokio::test]
77
1
async fn definition_nested_inside_cond_under_the_guard_is_still_rejected() {
78
1
    let form = "(progn (defparameter x 0) \
79
1
                 (and (> (rpc-protocol-version) 5) (cond (t (defparameter x 7)))) x)";
80
1
    assert_error_code(form, "compile").await;
81
1
}
82

            
83
#[tokio::test]
84
1
async fn definition_nested_inside_let_under_the_guard_is_still_rejected() {
85
1
    let form = "(progn (defparameter x 0) \
86
1
                 (and (> (rpc-protocol-version) 5) (let ((y 1)) (defparameter x 7))) x)";
87
1
    assert_error_code(form, "compile").await;
88
1
}
89

            
90
#[tokio::test]
91
1
async fn definition_nested_inside_handler_case_under_the_guard_is_still_rejected() {
92
1
    let form = "(progn (defparameter x 0) \
93
1
                 (and (> (rpc-protocol-version) 5) \
94
1
                      (handler-case (defparameter x 7) (t (e) nil))) \
95
1
                 x)";
96
1
    assert_error_code(form, "compile").await;
97
1
}
98

            
99
#[tokio::test]
100
1
async fn definition_nested_inside_dolist_under_the_guard_is_still_rejected() {
101
1
    let form = "(progn (defparameter x 0) \
102
1
                 (and (> (rpc-protocol-version) 5) \
103
1
                      (dolist (i (list 1)) (defparameter x 7))) \
104
1
                 x)";
105
1
    assert_error_code(form, "compile").await;
106
1
}
107

            
108
// --- item 3: a definition in STACK position under a runtime guard behaves
109
// consistently with the effect-position rule (same rejection, same
110
// mechanism — `and_or_depth` is set identically by the stack-position
111
// runtime hand-off), and the guarded-suffix compile still genuinely
112
// short-circuits (an operand after a decided value never runs — not just
113
// "doesn't error"). ---
114

            
115
#[tokio::test]
116
1
async fn stack_position_definition_under_a_runtime_and_guard_is_also_rejected() {
117
    // DEFPARAMETER has no stack producer of its own (`stack: None`), so it
118
    // can only appear as a NON-last (effect-compiled) operand even when the
119
    // enclosing AND itself sits in stack/value position — this pins that
120
    // the SAME rejection still fires there.
121
1
    let form = "(let ((v (and (> (rpc-protocol-version) 5) (defparameter x 7) 9))) v)";
122
1
    assert_error_code(form, "compile").await;
123
1
}
124

            
125
#[tokio::test]
126
1
async fn guarded_suffix_still_short_circuits_an_operand_with_a_real_side_effect() {
127
    // Not just "doesn't error": an operand AFTER a decided constant must
128
    // never actually RUN. `setf` on a runtime local is a real, observable
129
    // side effect — if short-circuiting broke, `n` would become 1.
130
1
    let form = "(let ((n 0)) \
131
1
                 (and (> (rpc-protocol-version) 0) nil (setf n 1)) \
132
1
                 n)";
133
1
    assert_values(&[(form, "0")]).await;
134
1
}