1
//! DOLIST codegen on runtime pair-chains. Exercises the runtime
2
//! variant of `compile_dolist` that the static-eval path can't reach.
3

            
4
use super::common::{compile_and_validate, compile_expect_error};
5

            
6
#[test]
7
1
fn dolist_over_static_integer_list() {
8
1
    compile_and_validate("(let* ((sum 0)) (dolist (i '(1 2 3)) (setf sum (+ sum i))) sum)");
9
1
}
10

            
11
#[test]
12
1
fn dolist_over_runtime_pair_chain() {
13
    // `(get-input-entities)` returns a runtime `pair<i32>` chain. The
14
    // dolist body sees `i` as a runtime i32 local.
15
1
    compile_and_validate(
16
1
        "(let* ((acc 0)) (dolist (i (get-input-entities)) (setf acc (+ acc 1))) acc)",
17
1
    );
18
1
}
19

            
20
#[test]
21
1
fn dolist_with_explicit_result_form() {
22
1
    compile_and_validate("(let* ((acc 0)) (dolist (i '(1 2)) (setf acc (+ acc i))) acc)");
23
1
}
24

            
25
#[test]
26
1
fn dolist_over_cons_ratio_chain() {
27
1
    compile_and_validate(
28
1
        "(let* ((sum 0)) \
29
1
           (dolist (r (cons (/ 1 2) (cons (/ 3 4) nil))) \
30
1
             (setf sum (+ sum r))) \
31
1
           sum)",
32
1
    );
33
1
}
34

            
35
#[test]
36
1
fn dolist_over_string_chain() {
37
1
    compile_and_validate(
38
1
        "(let* ((count 0)) \
39
1
           (dolist (s (cons \"a\" (cons \"b\" nil))) \
40
1
             (setf count (+ count 1))) \
41
1
           count)",
42
1
    );
43
1
}
44

            
45
/// A DOLIST as the value of a `defun` body (value/stack position). The form
46
/// used to carry `stack: None`, so this errored with "special form 'DOLIST'
47
/// cannot produce stack value". No result form ≡ nil. Locks in the stack path.
48
#[test]
49
1
fn dolist_in_value_position_no_result_form() {
50
1
    compile_and_validate("(defun walk (xs) (dolist (x xs) x)) (walk '(1 2 3))");
51
1
}
52

            
53
/// Same, with an explicit result form that the stack path must leave typed
54
/// on the operand stack.
55
#[test]
56
1
fn dolist_in_value_position_with_result_form() {
57
1
    compile_and_validate("(let* ((acc 0)) (dolist (x '(1 2 3) acc) (setf acc (+ acc x))))");
58
1
}
59

            
60
/// A CONSTANT-list dolist whose body `setf`s an OUTER variable to a runtime
61
/// value. The const-fold path must run the same setf-target promotion the
62
/// runtime path does — without it, `out` is left a bare `WasmRuntime`
63
/// placeholder with no stack producer and the wasm is malformed.
64
#[test]
65
1
fn const_dolist_body_setf_outer_to_runtime_compiles() {
66
1
    compile_and_validate("(let ((out nil)) (dolist (x '(1)) (setf out (get-input-entities))) out)");
67
1
}
68

            
69
/// A nil-initialized let local whose only `setf` store comes from a USER
70
/// function returning a ref type. The local must be sized from the function's
71
/// result type (PairRef), not the falsy `Bool` of its `nil` init — otherwise
72
/// the ref store fails wasm validation. Locks in that setf-local inference
73
/// types a user-fn rhs via its body, not just pure-native rhs.
74
#[test]
75
1
fn setf_local_sized_from_user_fn_ref_return_compiles() {
76
1
    compile_and_validate("(defun f () (get-input-entities)) (let ((out nil)) (setf out (f)) out)");
77
1
}
78

            
79
/// A let-bound runtime closure used as a FOLD accumulator function. Its result
80
/// type (Ratio, from its Ratio-default arithmetic body) is recorded at the
81
/// closure-emit site, so the eval-side fold result type AGREES with codegen and
82
/// the `out` local is sized correctly. Before the closure-result recording the
83
/// eval mirror sized `out` from the integer seed (Index) while codegen emitted
84
/// Ratio → invalid wasm. `out` is seeded `0` rather than `nil` (ADR-0033: `Ratio`
85
/// is `Never` — a `nil` init sized to it is a compile error, covered separately
86
/// by `nil_default::fold_symbol_nil_seed_ratio_accumulator`).
87
#[test]
88
1
fn fold_runtime_closure_scalar_into_setf_local_compiles() {
89
1
    compile_and_validate(
90
1
        "(let ((f (lambda (acc x) (+ acc x)))) \
91
1
           (let ((out 0)) (setf out (fold f 0 (list 1 2 3))) out))",
92
1
    );
93
1
}
94

            
95
/// A let-bound runtime closure that BUILDS a list (`(cons x acc)`). Param-usage
96
/// inference types `acc` as a `PairRef` (a list accumulator) instead of the
97
/// default Ratio — otherwise `(cons x acc)` rejects `acc` as a non-pair cdr.
98
#[test]
99
1
fn fold_runtime_closure_list_builder_compiles() {
100
1
    compile_and_validate("(let ((f (lambda (acc x) (cons x acc)))) (fold f nil (list 1 2 3)))");
101
1
}
102

            
103
/// MAP / FILTER over let-bound runtime closures with scalar results compile
104
/// (the closure's Ratio-default params match the Ratio-cell literal list).
105
#[test]
106
1
fn map_filter_runtime_closures_compile() {
107
1
    compile_and_validate("(let ((g (lambda (x) (* x 2)))) (map g (list 1 2 3)))");
108
1
    compile_and_validate("(let ((p (lambda (x) (< 1 x)))) (filter p (list 1 2 3)))");
109
1
}
110

            
111
/// MAP result element is the closure's RESULT type, not the input element: a
112
/// closure returning a StringRef, mapped over a list and stored into a nil-init
113
/// local, must size that local as `pair<string>` (eval uses the recorded
114
/// closure result, matching codegen) rather than `pair<ratio>` → invalid wasm.
115
#[test]
116
1
fn map_runtime_closure_result_sizes_setf_local() {
117
1
    compile_and_validate(
118
1
        "(let ((g (lambda (x) (tag-name 0)))) \
119
1
           (let ((out nil)) (setf out (map g (list 1 2))) out))",
120
1
    );
121
1
}
122

            
123
/// FILTER keeps INPUT elements: a closure predicate over a Ratio list, stored
124
/// into a nil-init local, sizes it as `pair<ratio>` (the input element) via the
125
/// eval path that now mirrors `compile_filter_literal` (input-element widening)
126
/// rather than the old `pair<ratio>` default that drifted for non-Ratio lists.
127
#[test]
128
1
fn filter_runtime_closure_into_setf_local_compiles() {
129
1
    compile_and_validate(
130
1
        "(let ((p (lambda (n) (< 0 n)))) \
131
1
           (let ((out nil)) (setf out (filter p (list 1 2 3))) out))",
132
1
    );
133
1
}
134

            
135
/// A body/program TAIL that is `(setf …)`: its value must be serialized to the
136
/// output buffer (the `compile_expr` contract), not stranded on the wasm stack
137
/// — otherwise the enclosing block ends non-empty (invalid wasm).
138
#[test]
139
1
fn tail_setf_serializes_value_not_left_on_stack() {
140
1
    compile_and_validate("(let ((x 0)) (setf x 5))");
141
1
}
142

            
143
/// `(setf <ratio-local> <integer-literal>)`: the rhs literal must coerce to the
144
/// local's declared type (Ratio), crossing the Index↔Scalar boundary — not be
145
/// stored as a raw I32 into a ref-typed local (invalid wasm).
146
#[test]
147
1
fn setf_coerces_literal_rhs_to_local_type() {
148
1
    compile_and_validate("(let ((x 1/2)) (setf x 1) x)");
149
1
}
150

            
151
/// SETF-promotion inference is scope-aware: a shadowed inner `(setf x 2)` must
152
/// NOT promote the OUTER `x` to a runtime local. Here the outer `x` stays the
153
/// const `1`, so `(+ x 1/2)` is `(+ 1 1/2)` (both Scalar) and compiles; if the
154
/// shadowed setf wrongly promoted the outer `x` to a runtime Index, the add
155
/// would be an Index/Scalar stratum mismatch.
156
#[test]
157
1
fn setf_promotion_respects_shadowing() {
158
1
    compile_and_validate("(let ((x 1/1)) (let ((x 0)) (setf x 2)) (+ x 1/2))");
159
1
}
160

            
161
/// FOLD with a let-bound closure over a RUNTIME I32 list (`get-input-entities`
162
/// yields a `pair<i32>`). The closure param `x` defaults to Ratio, so a
163
/// `call_ref` would reject the i32 element. The HOF recovers the closure's body
164
/// (`CompileContext::closure_body`) and INLINES it per element instead, binding
165
/// `x` to the actual i32 — so the closure works over a non-Ratio list.
166
#[test]
167
1
fn fold_let_closure_over_runtime_i32_list_inlines() {
168
1
    compile_and_validate("(let ((f (lambda (acc x) (+ acc 1)))) (fold f 0 (get-input-entities)))");
169
1
}
170

            
171
/// SETF must invalidate the recorded closure body only AFTER the rhs is
172
/// compiled: the rhs here FOLDS the closure being reassigned over a runtime i32
173
/// list, which requires the inline path (a Ratio-default `call_ref` would
174
/// mismatch the i32 element). Forgetting the body before compiling the rhs would
175
/// drop the inline and fail to compile.
176
#[test]
177
1
fn setf_rhs_may_fold_the_closure_being_reassigned() {
178
1
    compile_and_validate(
179
1
        "(let ((f (lambda (acc x) (+ acc 1)))) \
180
1
           (setf f (progn (fold f 0 (get-input-entities)) f)) \
181
1
           0)",
182
1
    );
183
1
}
184

            
185
/// A let-bound STRING predicate closure: param-usage inference pins `s` to
186
/// `StringRef` (it's a `string=` argument), so the closure emits with `s:
187
/// StringRef` — matching the string-list element — instead of the default
188
/// Ratio that `string=` would reject at emission.
189
#[test]
190
1
fn filter_let_closure_string_predicate_compiles() {
191
1
    compile_and_validate(
192
1
        "(let ((p (lambda (s) (string= s \"x\")))) (filter p (cons \"a\" (cons \"b\" nil))))",
193
1
    );
194
1
}
195

            
196
/// Carried-in fix (ADR-0033 Phase 3): a lambda compiled to a real wasm helper
197
/// function (a let-bound closure VALUE, not an inlined `(funcall (lambda…))`)
198
/// used to inherit the enclosing function's `block_labels`, so a
199
/// `return-from` naming an outer `BLOCK` computed its `br` depth against the
200
/// wrong emitter ("inconsistent block-depth tracking"). A helper is a
201
/// SEPARATE wasm function — `br` cannot cross that boundary — so the outer
202
/// block must be unreachable from inside it, surfaced as a clean "no such
203
/// BLOCK" error instead of a bogus depth-tracking one.
204
#[test]
205
1
fn return_from_inside_a_lambda_helper_targeting_an_outer_block_is_a_clean_error() {
206
1
    let err = compile_expect_error(
207
1
        "(block outer (let ((f (lambda () (return-from outer 42)))) (funcall f)) 1)",
208
1
    );
209
1
    assert!(
210
1
        !err.contains("inconsistent block-depth tracking"),
211
        "got: {err}"
212
    );
213
1
    assert!(err.contains("OUTER"), "got: {err}");
214
1
}