1
//! ADR-0033: list consumers accept a bare `AnyRef` operand (not just a
2
//! statically-typed `PairRef(elem)` chain) — null becomes `nil`, a `$pair`
3
//! passes `ref.test` and is cast, anything else throws a catchable
4
//! `TYPE-ERROR`. Covers CAR / CDR / LENGTH / REVERSE / APPEND / FOLD / MAP /
5
//! FILTER / DOLIST, each fed a genuinely `AnyRef`-typed operand — an `IF`
6
//! whose arms disagree widens to `AnyRef` (`(list 1)` vs `"a"`).
7

            
8
use super::common::{compile_and_validate, wrap_with_runtime_i32};
9

            
10
9
fn anyref_or_nil(then_form: &str) -> String {
11
9
    wrap_with_runtime_i32(&format!("(let* ((V (if (> IDX 0) {then_form} \"a\"))) V)"))
12
9
}
13

            
14
#[test]
15
1
fn car_and_cdr_over_an_anyref_operand() {
16
1
    let v = anyref_or_nil("(list 1)");
17
1
    compile_and_validate(&format!("(let* ((V {v})) (car V))"));
18
1
    compile_and_validate(&format!("(let* ((V {v})) (cdr V))"));
19
1
}
20

            
21
#[test]
22
1
fn length_over_an_anyref_operand() {
23
1
    compile_and_validate(&format!("(length {})", anyref_or_nil("(list 1 2)")));
24
1
}
25

            
26
#[test]
27
1
fn reverse_over_an_anyref_operand() {
28
1
    compile_and_validate(&format!("(reverse {})", anyref_or_nil("(list 1 2)")));
29
1
}
30

            
31
#[test]
32
1
fn append_over_an_anyref_operand() {
33
1
    compile_and_validate(&format!(
34
1
        "(append {} (list 3))",
35
1
        anyref_or_nil("(list 1 2)")
36
1
    ));
37
1
}
38

            
39
#[test]
40
1
fn fold_over_an_anyref_operand() {
41
1
    compile_and_validate(&wrap_with_runtime_i32(&format!(
42
1
        "(fold (lambda (acc x) (cons x acc)) nil {})",
43
1
        anyref_or_nil("(list 1 2)")
44
1
    )));
45
1
}
46

            
47
#[test]
48
1
fn map_over_an_anyref_operand() {
49
1
    compile_and_validate(&format!(
50
1
        "(map (lambda (x) (null? x)) {})",
51
1
        anyref_or_nil("(list 1 2)")
52
1
    ));
53
1
}
54

            
55
#[test]
56
1
fn filter_over_an_anyref_operand() {
57
1
    compile_and_validate(&format!(
58
1
        "(filter (lambda (x) (null? x)) {})",
59
1
        anyref_or_nil("(list 1 2)")
60
1
    ));
61
1
}
62

            
63
#[test]
64
1
fn dolist_over_an_anyref_operand() {
65
1
    compile_and_validate(&format!(
66
1
        "(dolist (x {}) (null? x))",
67
1
        anyref_or_nil("(list 1 2)")
68
1
    ));
69
1
}
70

            
71
#[test]
72
1
fn car_of_a_non_list_anyref_compiles_the_runtime_throw_path() {
73
    // The "not a list" TYPE-ERROR is a runtime throw, not a compile error —
74
    // this only pins that the dynamic dispatch (null / $pair / anything
75
    // else) still emits valid wasm when the operand is a non-list AnyRef.
76
1
    compile_and_validate(&format!("(car {})", anyref_or_nil("1")));
77
1
}