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 Q4-1 review rows: the car of an empty closure list is a catchable
6
//! `TYPE-ERROR` (never a null closure), and a closure in a cell keeps the
7
//! redefinition refusal of a plain closure.
8

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

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

            
14
const P: &str = "(let* ((f (lambda (x) (+ x 1))) (l (cdr (list f)))) ";
15

            
16
7
fn nil_car(body: &str) -> String {
17
7
    format!("{P}{body})")
18
7
}
19

            
20
1
fn nil_car_rows() -> Vec<Row> {
21
1
    vec![
22
1
        row(
23
1
            "N1",
24
1
            &[&nil_car("(handler-case (funcall (car l) 1) (t (e) 99))")],
25
1
        ),
26
1
        row("N2", &[&nil_car("(if (car l) 1 2)")]),
27
1
        row("N3", &[&nil_car("(when (car l) 1)")]),
28
1
        row("N4", &[&nil_car("(let ((g (car l))) (if g 1 2))")]),
29
1
        row("N5", &[&nil_car("(or (car l) 5)")]),
30
1
        row(
31
1
            "N6",
32
1
            &[&nil_car("(let ((k (car l))) (if k (funcall k 1) 0))")],
33
1
        ),
34
1
        row(
35
1
            "N7",
36
1
            &[&nil_car("(unwind-protect (funcall (car l) 1) (print 7))")],
37
1
        ),
38
1
        row(
39
1
            "N8",
40
1
            &[&format!(
41
1
                "{P}(defun h (m) (handler-case (funcall (car m) 1) (t (e) 99))) (h l))"
42
1
            )],
43
1
        ),
44
    ]
45
1
}
46

            
47
const G: &str = "(defun g (x) x) ";
48
const REDEF: &str = "(defun g (x) (* x 2)) ";
49

            
50
11
fn refused(label: &'static str, body: &str) -> Row {
51
11
    row(label, &[&format!("{G}{body}")])
52
11
}
53

            
54
1
fn refusal_rows() -> Vec<Row> {
55
1
    let seed = "(defvar *l* (let ((f (lambda (x) (g x)))) (list f))) ";
56
1
    vec![
57
1
        refused("H1", &format!("{seed}{REDEF}(funcall (car *l*) 3)")),
58
1
        refused(
59
1
            "H2",
60
1
            &format!(
61
1
                "(defvar *l* (let ((n (entity-count))) (list (lambda (x) (list n (g x)))))) \
62
1
                 {REDEF}(funcall (car *l*) 3)"
63
1
            ),
64
1
        ),
65
1
        refused(
66
1
            "H3",
67
1
            &format!("(let* ((f (lambda (x) (g x))) (l (list f))) {REDEF}(funcall (car l) 3))"),
68
1
        ),
69
1
        refused(
70
1
            "H4",
71
1
            &format!(
72
1
                "(defvar *l* (let ((f (lambda (x) (g x)))) (list f f))) {REDEF}\
73
1
                 (let ((r 0)) (dolist (h *l*) (setf r (funcall h 3))) r)"
74
1
            ),
75
1
        ),
76
1
        refused(
77
1
            "H5",
78
1
            &format!("{seed}{REDEF}(mapcar (lambda (h) (funcall h 3)) *l*)"),
79
1
        ),
80
1
        refused(
81
1
            "H6",
82
1
            &format!(
83
1
                "(defvar *l* (let ((f (lambda (x) (g x)))) (list (list f)))) {REDEF}\
84
1
                 (funcall (car (car *l*)) 3)"
85
1
            ),
86
1
        ),
87
1
        refused(
88
1
            "H8",
89
1
            &format!(
90
1
                "(defun mk () (let ((f (lambda (x) (g x)))) (list f))) (defvar *l* (mk)) \
91
1
                 {REDEF}(funcall (car *l*) 3)"
92
1
            ),
93
1
        ),
94
1
        refused(
95
1
            "H18",
96
1
            &format!("{seed}(defun run () (funcall (car *l*) 3)) {REDEF}(run)"),
97
1
        ),
98
1
        refused(
99
1
            "H19",
100
1
            &format!(
101
1
                "(defun app (l) (funcall (car l) 3)) (let ((f (lambda (x) (g x)))) \
102
1
                 (let ((l (list f))) {REDEF}(app l)))"
103
1
            ),
104
1
        ),
105
1
        refused(
106
1
            "H21",
107
1
            &format!(
108
1
                "(defvar *l* (let ((f (lambda (x) (funcall #'g x)))) (list f))) {REDEF}\
109
1
                 (funcall (car *l*) 3)"
110
1
            ),
111
1
        ),
112
1
        refused(
113
1
            "H23",
114
1
            &format!(
115
1
                "(defvar *l* (let ((f (lambda (x) (funcall 'g x)))) (list f))) {REDEF}\
116
1
                 (funcall (car *l*) 3)"
117
1
            ),
118
1
        ),
119
    ]
120
1
}
121

            
122
1
fn repl_rows() -> Vec<Row> {
123
1
    let seed = "(defvar *l* (let ((f (lambda (x) (g x)))) (list f)))";
124
1
    vec![
125
1
        row(
126
1
            "H10",
127
1
            &[&format!("{G}{seed}"), REDEF, "(funcall (car *l*) 3)"],
128
1
        ),
129
1
        row(
130
1
            "H14",
131
1
            &[
132
1
                &format!("{G}{seed}"),
133
1
                "(funcall (car *l*) 3)",
134
1
                REDEF,
135
1
                "(funcall (car *l*) 3)",
136
1
            ],
137
1
        ),
138
1
        row("H16", &[&format!("{G}{seed}"), "(funcall (car *l*) 3)"]),
139
    ]
140
1
}
141

            
142
const NIL_CAR: &[(&str, &[&str])] = &[
143
    ("N1", &["99"]),
144
    (
145
        "N2",
146
        &["err: TYPE-ERROR: car of an empty list has no value"],
147
    ),
148
    (
149
        "N3",
150
        &["err: TYPE-ERROR: car of an empty list has no value"],
151
    ),
152
    (
153
        "N4",
154
        &["err: TYPE-ERROR: car of an empty list has no value"],
155
    ),
156
    (
157
        "N5",
158
        &["err: TYPE-ERROR: car of an empty list has no value"],
159
    ),
160
    (
161
        "N6",
162
        &["err: TYPE-ERROR: car of an empty list has no value"],
163
    ),
164
    (
165
        "N7",
166
        &["err: TYPE-ERROR: car of an empty list has no value"],
167
    ),
168
    ("N8", &["99"]),
169
];
170
const REFUSALS: &[(&str, &[&str])] = &[
171
    (
172
        "H1",
173
        &[
174
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
175
        ],
176
    ),
177
    (
178
        "H2",
179
        &[
180
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
181
        ],
182
    ),
183
    (
184
        "H3",
185
        &[
186
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
187
        ],
188
    ),
189
    (
190
        "H4",
191
        &[
192
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
193
        ],
194
    ),
195
    (
196
        "H5",
197
        &[
198
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
199
        ],
200
    ),
201
    (
202
        "H6",
203
        &[
204
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
205
        ],
206
    ),
207
    (
208
        "H8",
209
        &[
210
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
211
        ],
212
    ),
213
    (
214
        "H18",
215
        &[
216
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
217
        ],
218
    ),
219
    (
220
        "H19",
221
        &[
222
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
223
        ],
224
    ),
225
    (
226
        "H21",
227
        &[
228
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
229
        ],
230
    ),
231
    (
232
        "H23",
233
        &[
234
            "err: Compile error: G: redefined after a closure that calls it was created or used in this unit; redefine",
235
        ],
236
    ),
237
];
238
const REPL: &[(&str, &[&str])] = &[
239
    ("H10", &["-", "g", "6"]),
240
    ("H14", &["-", "3", "g", "6"]),
241
    ("H16", &["-", "3"]),
242
];
243

            
244
#[test]
245
1
fn the_car_of_an_empty_closure_list_throws() {
246
1
    assert_pinned_wide(&nil_car_rows(), NIL_CAR, 100);
247
1
}
248

            
249
#[test]
250
1
fn a_closure_in_a_cell_refuses_a_late_redefinition() {
251
1
    assert_pinned_wide(&refusal_rows(), REFUSALS, 100);
252
1
}
253

            
254
#[test]
255
1
fn a_closure_list_global_follows_a_redefinition_in_a_later_unit() {
256
1
    assert_pinned(&repl_rows(), REPL);
257
1
}