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 AD rows: a definition after a call of a function that may leave
6
//! the region is refused, and a kept definition is the form itself, not any
7
//! form with the same operands.
8

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

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

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

            
16
4
fn leaving(target: &str) -> String {
17
4
    format!("(defun g () (when {ALWAYS} {target}))")
18
4
}
19

            
20
2
fn rows() -> Vec<Row> {
21
2
    let exit_b = leaving("(return-from b 1)");
22
2
    let go_end = leaving("(go end)");
23
2
    vec![
24
2
        row(
25
2
            "F1",
26
2
            &[
27
2
                "(defun f () 9)",
28
2
                &format!("(block b (progn {exit_b} (g) (defun f () 2)) 0)"),
29
2
                "(f)",
30
2
            ],
31
2
        ),
32
2
        row(
33
2
            "F2",
34
2
            &[
35
2
                &format!("(block b (progn {exit_b} (g) (defun f () 2)) 0)"),
36
2
                "(f)",
37
2
            ],
38
2
        ),
39
2
        row(
40
2
            "F3",
41
2
            &[
42
2
                "(defun f () 9)",
43
2
                &format!("(block b (let ((x 1)) {exit_b} (g) (defun f () x)) 0)"),
44
2
                "(f)",
45
2
            ],
46
2
        ),
47
2
        row(
48
2
            "F4",
49
2
            &[
50
2
                "(defun f () 9)",
51
2
                &format!("(tagbody (progn {go_end} (g) (defun f () 2)) end)"),
52
2
                "(f)",
53
2
            ],
54
2
        ),
55
2
        row(
56
2
            "F5",
57
2
            &[
58
2
                &format!("(block b (progn {exit_b} (g) (defmacro f () 2)) 0)"),
59
2
                "(f)",
60
2
            ],
61
2
        ),
62
2
        row(
63
2
            "F6",
64
2
            &[
65
2
                &format!("(block b (progn {exit_b} (g) (defstruct pt x)) 0)"),
66
2
                "(pt-x (make-pt 1))",
67
2
            ],
68
2
        ),
69
2
        row(
70
2
            "G1",
71
2
            &[
72
2
                &format!(
73
2
                    "(block b (progn (let ((x 1)) (defun f () x)) (when {ALWAYS} (return-from b 1)) (let ((x 2)) (defun f () x))) 0)"
74
2
                ),
75
2
                "(f)",
76
2
            ],
77
2
        ),
78
2
        row(
79
2
            "G2",
80
2
            &[
81
2
                &format!(
82
2
                    "(block b (progn (defun f () (+ 2 1)) (when {ALWAYS} (return-from b 1)) (defmacro f () (+ 2 1))) 0)"
83
2
                ),
84
2
                "(f)",
85
2
            ],
86
2
        ),
87
2
        row(
88
2
            "G3",
89
2
            &[
90
2
                &format!(
91
2
                    "(block b (progn (defun f () 1) (defun f () 1) (when {ALWAYS} (return-from b 1))) 0)"
92
2
                ),
93
2
                "(f)",
94
2
            ],
95
2
        ),
96
2
        row(
97
2
            "D13",
98
2
            &[
99
2
                &format!("(block b (progn (defvar *v* 1) (when {ALWAYS} (return-from b 1))) 0)"),
100
2
                "*v*",
101
2
            ],
102
2
        ),
103
2
        row(
104
2
            "D14",
105
2
            &[
106
2
                &format!(
107
2
                    "(block b (let ((x 1)) (defparameter *w* 2) (when {ALWAYS} (return-from b x))) 0)"
108
2
                ),
109
2
                "*w*",
110
2
            ],
111
2
        ),
112
2
        row(
113
2
            "D15",
114
2
            &[
115
2
                &format!("(block b (progn (when {ALWAYS} (return-from b 1)) (defvar *v* 1)) 0)"),
116
2
                "*v*",
117
2
            ],
118
2
        ),
119
2
        row(
120
2
            "H1",
121
2
            &[
122
2
                "(handler-case (progn (defun a () 0) (progn (defun f () 1) (print \"go\")) 0) (t (e) 0))",
123
2
                "(f)",
124
2
            ],
125
2
        ),
126
    ]
127
2
}
128

            
129
const UNDEFINED_F: &str = "err: Undefined symbol: F";
130
const BLOCK: &str = "err: Compile error: DEFUN F follows a form that may leave this BLOCK; move ";
131
const TAGBODY: &str = "err: Compile error: DEFUN F follows a form that may leave this TAGBODY; mov";
132
const MACRO: &str = "err: Compile error: DEFMACRO F follows a form that may leave this BLOCK; mo";
133
const STRUCT: &str = "err: Compile error: DEFSTRUCT PT follows a form that may leave this BLOCK; ";
134
const CELL: &str = "err: Compile error: DEFVAR: a definition must be statically executable; thi";
135

            
136
const REFUSED: &[(&str, &[&str])] = &[
137
    ("F1", &["f", BLOCK, "9"]),
138
    ("F2", &[BLOCK, UNDEFINED_F]),
139
    ("F3", &["f", BLOCK, "9"]),
140
    ("F4", &["f", TAGBODY, "9"]),
141
    ("F5", &[MACRO, UNDEFINED_F]),
142
    ("F6", &[STRUCT, "err: Undefined symbol: PT-X"]),
143
    ("G1", &[BLOCK, UNDEFINED_F]),
144
    ("G2", &[MACRO, UNDEFINED_F]),
145
    ("D15", &[CELL, "err: Undefined symbol: *V*"]),
146
];
147

            
148
const KEPT: &[(&str, &[&str])] = &[
149
    ("G3", &["1", "1"]),
150
    ("D13", &["1", "1"]),
151
    ("D14", &["1", "2"]),
152
    ("H1", &["0", "1"]),
153
];
154

            
155
#[test]
156
1
fn a_definition_after_a_call_that_may_leave_is_refused() {
157
1
    let rows = rows();
158
1
    let refused: Vec<Row> = rows
159
1
        .iter()
160
81
        .filter(|(label, _)| REFUSED.iter().any(|(l, _)| l == label))
161
        .cloned()
162
        .collect();
163
1
    assert_pinned_wide(&refused, REFUSED, 70);
164
1
}
165

            
166
#[test]
167
1
fn a_kept_definition_is_the_form_itself() {
168
1
    let rows = rows();
169
1
    let kept: Vec<Row> = rows
170
1
        .iter()
171
46
        .filter(|(label, _)| KEPT.iter().any(|(l, _)| l == label))
172
        .cloned()
173
        .collect();
174
1
    assert_pinned(&kept, KEPT);
175
1
}