1
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
2
// Cranelift backend refuses to run under Miri.
3
#![cfg(not(miri))]
4

            
5
//! PERSIST SN-P rows: CL's `&key ((:keyword var) default)` lambda-list form
6
//! in DEFUN, LAMBDA, LABELS and DEFMACRO. Each SBCL value is next to its row.
7

            
8
#[path = "persist/rows.rs"]
9
mod persist;
10

            
11
use persist::{Row, assert_pinned, assert_pinned_wide, row};
12

            
13
struct Case {
14
    label: &'static str,
15
    units: &'static [&'static str],
16
    pinned: &'static [&'static str],
17
}
18

            
19
const fn case(
20
    label: &'static str,
21
    units: &'static [&'static str],
22
    pinned: &'static [&'static str],
23
) -> Case {
24
    Case {
25
        label,
26
        units,
27
        pinned,
28
    }
29
}
30

            
31
const CASES: &[Case] = &[
32
    // SBCL (1 5)
33
    case(
34
        "K1",
35
        &["(defun f (&key ((:a x) 1)) x)", "(list (f) (f :a 5))"],
36
        &["f", "(1 5)"],
37
    ),
38
    // SBCL (7 7 3): mixed with a plain &key, &optional and &rest
39
    case(
40
        "K2",
41
        &[
42
            "(defun f (o &optional (p 7) &key ((:a x) 1) (b 2)) (list p x b))",
43
            "(f 0 7 :a 7 :b 3)",
44
        ],
45
        &["f", "(7 7 3)"],
46
    ),
47
    // SBCL (5 (:A 5))
48
    case(
49
        "K3",
50
        &["(defun f (&rest r &key ((:a x) 1)) (list x r))", "(f :a 5)"],
51
        &["f", "(5 (:a 5))"],
52
    ),
53
    // SBCL (5 2)
54
    case(
55
        "K4",
56
        &[
57
            "(defun f (&key ((:a x) 1) (b 2)) (list x b))",
58
            "(f :b 2 :a 5)",
59
        ],
60
        &["f", "(5 2)"],
61
    ),
62
    // SBCL (0 5): no default is NIL
63
    case(
64
        "K5",
65
        &["(defun f (&key ((:a x))) (or x 0))", "(list (f) (f :a 5))"],
66
        &["f", "(0 5)"],
67
    ),
68
    // SBCL 5
69
    case(
70
        "K6",
71
        &["(funcall (lambda (&key ((:a x) 1)) x) :a 5)"],
72
        &["5"],
73
    ),
74
    // SBCL (1 5)
75
    case(
76
        "K8",
77
        &["(defmacro m (&key ((:a x) 1)) x)", "(list (m) (m :a 5))"],
78
        &["m", "(1 5)"],
79
    ),
80
    // SBCL 6: a store to the renamed variable
81
    case(
82
        "K16",
83
        &["(defun f (&key ((:a x) 1)) (setf x (+ x 1)) x)", "(f :a 5)"],
84
        &["f", "6"],
85
    ),
86
    // SBCL 6
87
    case(
88
        "K17",
89
        &["(funcall (lambda (&key ((:a x) 1)) (setf x (+ x 1)) x) :a 5)"],
90
        &["6"],
91
    ),
92
    // SBCL 3: a macro-time lambda reads the renamed variable
93
    case(
94
        "K18",
95
        &[
96
            "(defmacro m () (funcall (lambda () (funcall (lambda (&key ((:a x) 1)) x) :a 3))))",
97
            "(m)",
98
        ],
99
        &["m", "3"],
100
    ),
101
    // SBCL (1 5): the plain form is unchanged
102
    case(
103
        "K15",
104
        &["(defun f (&key (a 1)) a)", "(list (f) (f :a 5))"],
105
        &["f", "(1 5)"],
106
    ),
107
];
108

            
109
const ERROR_CASES: &[Case] = &[
110
    // SBCL (1 5); LABELS takes no lambda-list keywords at all
111
    case(
112
        "K7",
113
        &["(labels ((g (&key ((:a x) 1)) x)) (list (g) (g :a 5)))"],
114
        &["err: Compile error: LABELS: lambda list keywords not supported in this context"],
115
    ),
116
    // SBCL error: :x is not the keyword of (:a x)
117
    case(
118
        "K9",
119
        &["(defun f (&key ((:a x) 1)) x)", "(f :x 5)"],
120
        &["f", "err: Compile error: unknown keyword argument :X"],
121
    ),
122
    // SBCL error: the two parameters answer to :a
123
    case(
124
        "K10",
125
        &["(defun f (&key ((:a x) 1) ((:a y) 2)) (list x y))"],
126
        &["err: Compile error: DEFUN: duplicate keyword 'a'"],
127
    ),
128
    // SBCL error: plain A is the keyword :a too
129
    case(
130
        "K11",
131
        &["(defun f (&key a ((:a x) 2)) (list a x))"],
132
        &["err: Compile error: DEFUN: duplicate keyword 'a'"],
133
    ),
134
    // SBCL error: the variable is duplicated
135
    case(
136
        "K12",
137
        &["(defun f (&key ((:a x) 1) ((:b x) 2)) x)"],
138
        &["err: Compile error: DEFUN: duplicate parameter 'x'"],
139
    ),
140
    // SBCL accepts any symbol as the keyword name, (f 'a 5) gives 5; we require a keyword
141
    case(
142
        "K13",
143
        &["(defun f (&key ((a x) 1)) x)"],
144
        &["err: Compile error: DEFUN: &key parameter must be (:keyword var), got (A X)"],
145
    ),
146
    // SBCL accepts a supplied-p variable; nomiscript has none in any lambda list
147
    case(
148
        "K14",
149
        &["(defun f (&key ((:a x) 1 p)) (list x p))"],
150
        &[
151
            "err: Compile error: DEFUN: &key parameter must be ((:keyword var) default), got ((:A X) 1 P)",
152
        ],
153
    ),
154
];
155

            
156
2
fn rows(cases: &[Case]) -> Vec<Row> {
157
18
    cases.iter().map(|c| row(c.label, c.units)).collect()
158
2
}
159

            
160
2
fn pinned(cases: &[Case]) -> Vec<(&'static str, &'static [&'static str])> {
161
18
    cases.iter().map(|c| (c.label, c.pinned)).collect()
162
2
}
163

            
164
#[test]
165
1
fn key_keyword_rows_read_as_pinned() {
166
1
    assert_pinned(&rows(CASES), &pinned(CASES));
167
1
}
168

            
169
#[test]
170
1
fn key_keyword_error_rows_read_as_pinned() {
171
1
    assert_pinned_wide(&rows(ERROR_CASES), &pinned(ERROR_CASES), 120);
172
1
}