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 Q46-0 rows: calling a closure out of a mixed (`any`) list, pinned
6
//! at the REPL value with the SBCL value next to each row. Q46-1 flipped the
7
//! macro-body rows (K4, K5, Q16, M3), Q46-2 the calls of an `any` value;
8
//! Q46-3 flips the carried closure globals (Q20, Q21).
9

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

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

            
15
const NARROW_WITH_THE: &str = "err: Compile error: + expects numeric arguments, got an any \
16
     value; narrow it with THE (index, scalar or money)";
17
const THROUGH_LAMBDA: &str = "err: Compile error: the function operand is an any value; call it \
18
     through (lambda (x) (funcall f x))";
19
const COMPUTED: &str = "err: Compile error: the function operand is computed at runtime; \
20
     it must be a lambda or a function name";
21

            
22
struct Case {
23
    label: &'static str,
24
    units: &'static [&'static str],
25
    pinned: &'static [&'static str],
26
}
27

            
28
const fn case(
29
    label: &'static str,
30
    units: &'static [&'static str],
31
    pinned: &'static [&'static str],
32
) -> Case {
33
    Case {
34
        label,
35
        units,
36
        pinned,
37
    }
38
}
39

            
40
const CASES: &[Case] = &[
41
    // SBCL 2
42
    case(
43
        "A2",
44
        &[
45
            "(defvar *p* (list :f (lambda () 1) :g (lambda () 2)))",
46
            "(funcall (getf *p* :g))",
47
        ],
48
        &["-", "2"],
49
    ),
50
    // SBCL 1
51
    case(
52
        "K3",
53
        &["(let ((l (list (lambda () 1) nil))) (funcall (car l)))"],
54
        &["1"],
55
    ),
56
    // SBCL 7
57
    case(
58
        "N4",
59
        &["(funcall (car (car (list (list (lambda () 7))))))"],
60
        &["7"],
61
    ),
62
    // SBCL 1
63
    case(
64
        "O10",
65
        &["(let ((l (list (lambda () 1) (lambda (x) x)))) (funcall (car l)))"],
66
        &["1"],
67
    ),
68
    // SBCL 8
69
    case(
70
        "P4",
71
        &[
72
            "(defun f (n acc) (if (= n 0) (funcall (car acc)) (f (- n 1) acc))) \
73
           (f 3 (list (lambda () 8)))",
74
        ],
75
        &["8"],
76
    ),
77
    // SBCL 1
78
    case(
79
        "G7",
80
        &[
81
            "(let ((f (lambda () 1))) (defvar *p* (list :a f :b f)))",
82
            "(funcall (getf *p* :b))",
83
        ],
84
        &["-", "1"],
85
    ),
86
    // SBCL 1
87
    case(
88
        "G8",
89
        &[
90
            "(let ((f (lambda () 1))) (defvar *a* (list (list 'k f))))",
91
            "(funcall (car (cdr (assoc 'k *a*))))",
92
        ],
93
        &["-", "1"],
94
    ),
95
    // SBCL 7
96
    case(
97
        "G9",
98
        &[
99
            "(let ((f (lambda () 7))) (defvar *l* (list (list (list f)))))",
100
            "(funcall (car (car (car *l*))))",
101
        ],
102
        &["-", "7"],
103
    ),
104
    // SBCL 99
105
    case(
106
        "N13",
107
        &["(let ((f (lambda (x) (+ x 1)))) \
108
           (handler-case (funcall (getf (list :a f) :b) 1) (t (e) 99)))"],
109
        &["99"],
110
    ),
111
    // SBCL 3
112
    case(
113
        "Q2",
114
        &[
115
            "(let ((r 0)) (dolist (f (list (lambda () 1) nil (lambda () 2))) \
116
           (when f (setf r (+ r (the index (funcall f)))))) r)",
117
        ],
118
        &["3"],
119
    ),
120
    // SBCL 5
121
    case(
122
        "Q3",
123
        &["(funcall (getf (list :inc (lambda (x) (+ x 1)) :k 1) :inc) 4)"],
124
        &["5"],
125
    ),
126
    // SBCL 1
127
    case(
128
        "Q4",
129
        &["(funcall (getf (list :inc (lambda (x) (+ x 1)) :k 1) :inc) (entity-count))"],
130
        &["1"],
131
    ),
132
    // SBCL 99
133
    case(
134
        "Q5",
135
        &["(handler-case (funcall (car (list \"s\" (lambda () 1)))) (type-error (e) 99))"],
136
        &["99"],
137
    ),
138
    // SBCL 99
139
    case(
140
        "Q6",
141
        &["(handler-case (funcall (car (cdr (list (lambda () 1) nil)))) (t (e) 99))"],
142
        &["99"],
143
    ),
144
    // SBCL 99
145
    case(
146
        "Q7",
147
        &["(handler-case (funcall (car (list (lambda (x) x) nil))) (t (e) 99))"],
148
        &["99"],
149
    ),
150
    // SBCL 2
151
    case(
152
        "Q8b",
153
        &["(+ 1 (the index (funcall (car (list (lambda () 1) nil)))))"],
154
        &["2"],
155
    ),
156
    // SBCL 99: THE on a mismatching runtime type is a catchable TYPE-ERROR
157
    case(
158
        "Q8c",
159
        &[
160
            "(handler-case (+ 1 (the index (funcall (car (list (lambda () \"s\") nil))))) \
161
           (type-error (e) 99))",
162
        ],
163
        &["99"],
164
    ),
165
    // SBCL (1 10)
166
    case(
167
        "Q10",
168
        &["(let ((l (list (lambda () 1) (lambda (x) (* x 2))))) \
169
           (list (funcall (car l)) (funcall (car (cdr l)) 5)))"],
170
        &["(1 10)"],
171
    ),
172
    // SBCL 7
173
    case(
174
        "Q11",
175
        &["(let ((n (entity-count))) \
176
           (let ((l (list (lambda () (+ n 7)) nil))) (funcall (car l))))"],
177
        &["7"],
178
    ),
179
    // SBCL T
180
    case(
181
        "Q12",
182
        &[
183
            "(let ((l (list (lambda (s) (string= s \"a\")) nil)) (d (list \"a\" 1))) \
184
           (funcall (car l) (car d)))",
185
        ],
186
        &["t"],
187
    ),
188
    // SBCL (1 2)
189
    case(
190
        "Q13",
191
        &["(apply (car (list (lambda (a b) (list a b)) nil)) (list 1 2))"],
192
        &["(1 2)"],
193
    ),
194
    // SBCL (1 2)
195
    case(
196
        "Q14b",
197
        &["(let ((g (car (list (lambda (x) x) nil)))) \
198
           (mapcar (lambda (y) (funcall g y)) (list 1 2)))"],
199
        &["(1 2)"],
200
    ),
201
    // SBCL 1
202
    case(
203
        "Q15",
204
        &[
205
            "(defvar *l* (list (lambda () 1) nil))",
206
            "(funcall (car *l*))",
207
        ],
208
        &["-", "1"],
209
    ),
210
    // SBCL 3
211
    case(
212
        "Q17",
213
        &["(let ((l (list (lambda (x) x) nil))) (funcall (car l) (funcall (car l) 3)))"],
214
        &["3"],
215
    ),
216
    // SBCL 1
217
    case(
218
        "Q18",
219
        &["(let ((g (car (list (lambda () 1) nil)))) (funcall g))"],
220
        &["1"],
221
    ),
222
    // SBCL 4
223
    case(
224
        "Q19",
225
        &["(defun h (fs) (funcall (car fs))) (h (list (lambda () 4) :k))"],
226
        &["4"],
227
    ),
228
    // SBCL 42
229
    case(
230
        "Q22",
231
        &[
232
            "(defvar *l* (list (lambda (x) (* x 2)) :k))",
233
            "(funcall (car *l*) 21)",
234
        ],
235
        &["-", "42"],
236
    ),
237
    // SBCL 0: a typed closure call keeps working
238
    case(
239
        "Q23",
240
        &["(let ((f (lambda (x) x))) (funcall f (entity-count)))"],
241
        &["0"],
242
    ),
243
    // SBCL (1 2)
244
    case("Q24", &["(mapcar (lambda (y) y) (list 1 2))"], &["(1 2)"]),
245
];
246

            
247
/// Rows whose pinned error text is longer than the 44 characters of [`assert_pinned`].
248
const WIDE_CASES: &[Case] = &[
249
    // SBCL 1
250
    case(
251
        "K4",
252
        &["(defmacro m () (length (list (lambda () 1))))", "(m)"],
253
        &["m", "1"],
254
    ),
255
    // SBCL 1
256
    case(
257
        "K5",
258
        &["(defmacro m () (if (list (lambda () 1)) 1 2))", "(m)"],
259
        &["m", "1"],
260
    ),
261
    // SBCL (7 10): a built-in designator among closures is not a callable any
262
    // value yet (real function objects for built-ins are a later slice)
263
    case(
264
        "m-list-mix",
265
        &["(defun mul (a b) (* a b)) (mapcar (lambda (g) (funcall g 5 2)) (list #'+ #'mul))"],
266
        &["err: TYPE-ERROR: a built-in function designator is not callable as an any value yet"],
267
    ),
268
    // SBCL 1; a script or -e run reads the field as an any value instead
269
    case(
270
        "T5",
271
        &[
272
            "(defstruct box fn) (let ((f (lambda () 1))) (defvar *b* (make-box :fn f)))",
273
            "(funcall (box-fn *b*))",
274
        ],
275
        &["-", COMPUTED],
276
    ),
277
    // SBCL 2
278
    case(
279
        "Q8",
280
        &["(+ 1 (funcall (car (list (lambda () 1) nil))))"],
281
        &[NARROW_WITH_THE],
282
    ),
283
    // SBCL (1 2)
284
    case(
285
        "Q14",
286
        &["(mapcar (car (list (lambda (x) x) nil)) (list 1 2))"],
287
        &[THROUGH_LAMBDA],
288
    ),
289
    // SBCL 1
290
    case(
291
        "Q1",
292
        &["(funcall (if (< (entity-count) 1) (lambda () 1) (lambda (x) x)))"],
293
        &["1"],
294
    ),
295
    // SBCL 3
296
    case(
297
        "Q16",
298
        &[
299
            "(defmacro m () (funcall (car (list (lambda () 3)))))",
300
            "(m)",
301
        ],
302
        &["m", "3"],
303
    ),
304
    // SBCL 1; a script or -e run says "not callable: #<global:3:any>"
305
    case(
306
        "Q20",
307
        &[
308
            "(defvar *g* (car (list (lambda () 1) nil)))",
309
            "(funcall *g*)",
310
        ],
311
        &[
312
            "-",
313
            "err: Compile error: *G*: its closure value does not survive the form",
314
        ],
315
    ),
316
    // SBCL #<function>; a script or -e run prints #<closure:0>
317
    case(
318
        "Q21",
319
        &["(defvar *g* (car (list (lambda () 1) nil)))", "*g*"],
320
        &[
321
            "-",
322
            "err: Compile error: *G*: its closure value does not survive the form",
323
        ],
324
    ),
325
    // SBCL 1: the plan predicted a refusal, but the datum's lambda is called
326
    // with its expansion-time captures, which is the SBCL value
327
    case(
328
        "M3",
329
        &[
330
            "(defmacro m () (list 'quote (list (lambda () 1))))",
331
            "(funcall (car (m)))",
332
        ],
333
        &["m", "1"],
334
    ),
335
    // SBCL 1: a quoted datum holding a lambda never reaches a stack value
336
    case(
337
        "M3d",
338
        &[
339
            "(defmacro m () (list 'quote (list (lambda () 1))))",
340
            "(length (m))",
341
        ],
342
        &[
343
            "m",
344
            "err: Compile error: cannot compile to WASM stack value: '(LAMBDA () 1)",
345
        ],
346
    ),
347
    // SBCL 1: the CONS twin of K4
348
    case(
349
        "K4c",
350
        &["(defmacro m () (length (cons (lambda () 1) nil)))", "(m)"],
351
        &["m", "1"],
352
    ),
353
    // SBCL 3: the CONS twin of Q16
354
    case(
355
        "Q16c",
356
        &[
357
            "(defmacro m () (funcall (car (cons (lambda () 3) nil))))",
358
            "(m)",
359
        ],
360
        &["m", "3"],
361
    ),
362
    // SBCL (t 7): a Bool and an index closure are distinct signatures
363
    case(
364
        "C25",
365
        &[
366
            "(let ((l (list (lambda () (> 2 1)) (lambda () 7) nil))) (list (funcall (car l)) (funcall (car (cdr l)))))",
367
        ],
368
        &["(t 7)"],
369
    ),
370
    // SBCL (7 t)
371
    case(
372
        "C25b",
373
        &[
374
            "(let ((l (list (lambda () 7) (lambda () (> 2 1)) nil))) (list (funcall (car l)) (funcall (car (cdr l)))))",
375
        ],
376
        &["(7 t)"],
377
    ),
378
    // SBCL (0 nil)
379
    case(
380
        "C25c",
381
        &[
382
            "(let ((l (list (lambda () 0) (lambda () (< 2 1)) nil))) (list (funcall (car l)) (funcall (car (cdr l)))))",
383
        ],
384
        &["(0 nil)"],
385
    ),
386
    // SBCL (nil 0)
387
    case(
388
        "D5",
389
        &[
390
            "(let ((l (list (lambda () (< 2 1)) (lambda () 0) nil))) (list (funcall (car l)) (funcall (car (cdr l)))))",
391
        ],
392
        &["(nil 0)"],
393
    ),
394
    // SBCL 1: a count of 0 is never nil
395
    case(
396
        "D7",
397
        &[
398
            "(let ((r 0)) (dolist (f (list (lambda () 0) (lambda () (< 2 1)))) (when (funcall f) (setf r (+ r 1)))) r)",
399
        ],
400
        &["1"],
401
    ),
402
    // SBCL 1
403
    case(
404
        "D7b",
405
        &[
406
            "(let ((r 0)) (dolist (f (list (lambda () (< 2 1)) (lambda () 0))) (when (funcall f) (setf r (+ r 1)))) r)",
407
        ],
408
        &["1"],
409
    ),
410
];
411

            
412
2
fn rows(cases: &[Case]) -> Vec<Row> {
413
49
    cases.iter().map(|c| row(c.label, c.units)).collect()
414
2
}
415

            
416
2
fn pinned(cases: &[Case]) -> Vec<(&'static str, &'static [&'static str])> {
417
49
    cases.iter().map(|c| (c.label, c.pinned)).collect()
418
2
}
419

            
420
#[test]
421
1
fn any_call_rows_read_as_pinned() {
422
1
    assert_pinned(&rows(CASES), &pinned(CASES));
423
1
}
424

            
425
#[test]
426
1
fn any_call_wide_rows_read_as_pinned() {
427
1
    assert_pinned_wide(&rows(WIDE_CASES), &pinned(WIDE_CASES), 130);
428
1
}