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 Q1 rows: a DEFUN, DEFMACRO or DEFSTRUCT that follows a form that
6
//! may leave its BLOCK, TAGBODY or HANDLER-CASE is refused at compile time.
7

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

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

            
13
const ALWAYS: &str = "(> (entity-count) -1)";
14
const NEVER: &str = "(< (entity-count) 0)";
15

            
16
3
fn leave_b(test: &str) -> String {
17
3
    format!("(when {test} (return-from b 1))")
18
3
}
19

            
20
2
fn leave_tag(test: &str) -> String {
21
2
    format!("(when {test} (go end))")
22
2
}
23

            
24
1
fn refused() -> Vec<Row> {
25
1
    let (exit, miss, jump) = (leave_b(ALWAYS), leave_b(NEVER), leave_tag(ALWAYS));
26
1
    vec![
27
1
        row(
28
1
            "X1a",
29
1
            &[&format!("(block b {exit} (defun f () 2) 0)"), "(f)"],
30
1
        ),
31
1
        row(
32
1
            "X1b",
33
1
            &[&format!("(block b {exit} (defmacro m () 2) 0)"), "(m)"],
34
1
        ),
35
1
        row(
36
1
            "X1c",
37
1
            &[&format!("(block b {exit} (defstruct pt x) 0)"), "(pt-p 1)"],
38
1
        ),
39
1
        row(
40
1
            "X1d",
41
1
            &[&format!("(block b {miss} (defun f () 2) 0)"), "(f)"],
42
1
        ),
43
1
        row(
44
1
            "X1e",
45
1
            &[&format!("(block b {exit} (progn (defun f () 2)) 0)"), "(f)"],
46
1
        ),
47
1
        row(
48
1
            "X3a",
49
1
            &[&format!("(tagbody {jump} (defun f () 2) end)"), "(f)"],
50
1
        ),
51
1
        row(
52
1
            "X3b",
53
1
            &[&format!("(tagbody {jump} (defmacro m () 2) end)"), "(m)"],
54
1
        ),
55
1
        row(
56
1
            "X3c",
57
1
            &[
58
1
                &format!("(tagbody {jump} (defstruct pt x) end)"),
59
1
                "(pt-p 1)",
60
1
            ],
61
1
        ),
62
1
        row(
63
1
            "X4a",
64
1
            &[
65
1
                "(handler-case (progn (print \"go\") (defun f () 2) 0) (t (e) 0))",
66
1
                "(f)",
67
1
            ],
68
1
        ),
69
1
        row(
70
1
            "X4b",
71
1
            &[
72
1
                "(handler-case (progn (print \"go\") (defmacro m () 2) 0) (t (e) 0))",
73
1
                "(m)",
74
1
            ],
75
1
        ),
76
1
        row(
77
1
            "X4c",
78
1
            &[
79
1
                "(handler-case (progn (print \"go\") (defstruct pt x) 0) (t (e) 0))",
80
1
                "(pt-p 1)",
81
1
            ],
82
1
        ),
83
1
        row(
84
1
            "X5a",
85
1
            &[&format!(
86
1
                "(block a (when {ALWAYS} (return-from a 1)) (block b (defun f () 2) 0))"
87
1
            )],
88
1
        ),
89
1
        row(
90
1
            "X5b",
91
1
            &[&format!("(block a (block b {exit} (defun f () 2) 0))")],
92
1
        ),
93
    ]
94
1
}
95

            
96
1
fn kept() -> Vec<Row> {
97
1
    let (exit, jump) = (leave_b(ALWAYS), leave_tag(ALWAYS));
98
1
    vec![
99
1
        row(
100
1
            "X2a",
101
1
            &[&format!("(block b (defun f () 2) {exit} 0)"), "(f)"],
102
1
        ),
103
1
        row(
104
1
            "X2b",
105
1
            &[&format!("(block b (defmacro m () 2) {exit} 0)"), "(m)"],
106
1
        ),
107
1
        row(
108
1
            "X2c",
109
1
            &[&format!("(block b (defstruct pt x) {exit} 0)"), "(pt-p 1)"],
110
1
        ),
111
1
        row("X2d", &["(block b (defun f () 2) 0)", "(f)"]),
112
1
        row(
113
1
            "X2e",
114
1
            &[
115
1
                &format!("(block b (defun f () 2) {exit} 0) (defun g () (f))"),
116
1
                "(g)",
117
1
            ],
118
1
        ),
119
1
        row(
120
1
            "X3d",
121
1
            &[&format!("(tagbody (defun f () 2) {jump} end)"), "(f)"],
122
1
        ),
123
1
        row(
124
1
            "X3e",
125
1
            &[
126
1
                &format!("(tagbody (defstruct pt x) {jump} end)"),
127
1
                "(pt-p 1)",
128
1
            ],
129
1
        ),
130
1
        row(
131
1
            "X4d",
132
1
            &[
133
1
                "(handler-case (progn (defun f () 2) (print \"go\") 0) (t (e) 0))",
134
1
                "(f)",
135
1
            ],
136
1
        ),
137
1
        row(
138
1
            "X4e",
139
1
            &[
140
1
                "(handler-case (progn (defstruct pt x) (print \"go\") 0) (t (e) 0))",
141
1
                "(pt-p 1)",
142
1
            ],
143
1
        ),
144
1
        row(
145
1
            "X5c",
146
1
            &[
147
1
                &format!("(block a (block b (defun f () 2) {exit} 0) (defun g () 3) 0)"),
148
1
                "(list (f) (g))",
149
1
            ],
150
1
        ),
151
1
        row(
152
1
            "X5d",
153
1
            &[
154
1
                &format!(
155
1
                    "(block a (block b (defun f () 2) 0) (when {ALWAYS} (return-from a 1)) 0)"
156
1
                ),
157
1
                "(f)",
158
1
            ],
159
1
        ),
160
1
        row(
161
1
            "X6a",
162
1
            &[&format!("(block b {exit} (defparameter p 3) 0)"), "p"],
163
1
        ),
164
1
        row("X6b", &[&format!("(block b {exit} (defvar z 3) 0)"), "z"]),
165
1
        row(
166
1
            "X6c",
167
1
            &[
168
1
                "(defvar z 1)",
169
1
                &format!("(block b {exit} (setf z 3) 0)"),
170
1
                "z",
171
1
            ],
172
1
        ),
173
1
        row("X6d", &[&format!("(block b (defvar z 3) {exit} 0)"), "z"]),
174
    ]
175
1
}
176

            
177
/// CL defines these functions wherever the exit does not run; here a
178
/// definition after a form that may leave its region is refused when the text
179
/// is compiled, taken exit or not (X1d), and the failed unit defines nothing.
180
const REFUSED: &[(&str, &[&str])] = &[
181
    (
182
        "X1a",
183
        &[
184
            "err: Compile error: DEFUN F follows a form that may leave this BLOCK; move ",
185
            "err: Undefined symbol: F",
186
        ],
187
    ),
188
    (
189
        "X1b",
190
        &[
191
            "err: Compile error: DEFMACRO M follows a form that may leave this BLOCK; mo",
192
            "err: Undefined symbol: M",
193
        ],
194
    ),
195
    (
196
        "X1c",
197
        &[
198
            "err: Compile error: DEFSTRUCT PT follows a form that may leave this BLOCK; ",
199
            "err: Undefined symbol: PT-P",
200
        ],
201
    ),
202
    (
203
        "X1d",
204
        &[
205
            "err: Compile error: DEFUN F follows a form that may leave this BLOCK; move ",
206
            "err: Undefined symbol: F",
207
        ],
208
    ),
209
    (
210
        "X1e",
211
        &[
212
            "err: Compile error: DEFUN F follows a form that may leave this BLOCK; move ",
213
            "err: Undefined symbol: F",
214
        ],
215
    ),
216
    (
217
        "X3a",
218
        &[
219
            "err: Compile error: DEFUN F follows a form that may leave this TAGBODY; mov",
220
            "err: Undefined symbol: F",
221
        ],
222
    ),
223
    (
224
        "X3b",
225
        &[
226
            "err: Compile error: DEFMACRO M follows a form that may leave this TAGBODY; ",
227
            "err: Undefined symbol: M",
228
        ],
229
    ),
230
    (
231
        "X3c",
232
        &[
233
            "err: Compile error: DEFSTRUCT PT follows a form that may leave this TAGBODY",
234
            "err: Undefined symbol: PT-P",
235
        ],
236
    ),
237
    (
238
        "X4a",
239
        &[
240
            "err: Compile error: DEFUN F follows a form that may leave this HANDLER-CASE",
241
            "err: Undefined symbol: F",
242
        ],
243
    ),
244
    (
245
        "X4b",
246
        &[
247
            "err: Compile error: DEFMACRO M follows a form that may leave this HANDLER-C",
248
            "err: Undefined symbol: M",
249
        ],
250
    ),
251
    (
252
        "X4c",
253
        &[
254
            "err: Compile error: DEFSTRUCT PT follows a form that may leave this HANDLER",
255
            "err: Undefined symbol: PT-P",
256
        ],
257
    ),
258
    (
259
        "X5a",
260
        &["err: Compile error: DEFUN F follows a form that may leave this BLOCK; move "],
261
    ),
262
    (
263
        "X5b",
264
        &["err: Compile error: DEFUN F follows a form that may leave this BLOCK; move "],
265
    ),
266
];
267

            
268
/// The rest is as before: a definition before the exit, a DEFVAR or a SETF.
269
const KEPT: &[(&str, &[&str])] = &[
270
    ("X2a", &["1", "2"]),
271
    ("X2b", &["1", "2"]),
272
    ("X2c", &["1", "nil"]),
273
    ("X2d", &["0", "2"]),
274
    ("X2e", &["g", "2"]),
275
    ("X3d", &["-", "2"]),
276
    ("X3e", &["-", "nil"]),
277
    ("X4d", &["0", "2"]),
278
    ("X4e", &["0", "nil"]),
279
    ("X5c", &["0", "(2 3)"]),
280
    ("X5d", &["1", "2"]),
281
    (
282
        "X6a",
283
        &[
284
            "err: Compile error: DEFPARAMETER: a definition mu",
285
            "err: Undefined symbol: P",
286
        ],
287
    ),
288
    (
289
        "X6b",
290
        &[
291
            "err: Compile error: DEFVAR: a definition must be ",
292
            "err: Undefined symbol: Z",
293
        ],
294
    ),
295
    ("X6c", &["-", "1", "1"]),
296
    ("X6d", &["1", "3"]),
297
];
298

            
299
#[test]
300
1
fn a_definition_after_an_exit_is_refused_in_its_region() {
301
1
    assert_pinned_wide(&refused(), REFUSED, 70);
302
1
}
303

            
304
#[test]
305
1
fn a_definition_before_the_exit_and_a_store_after_it_are_unchanged() {
306
1
    assert_pinned(&kept(), KEPT);
307
1
}