-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathassert_forall_by.rs
293 lines (255 loc) · 6.99 KB
/
assert_forall_by.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
test_verify_one_file! {
#[test] test_assertby1 verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn assertby_test() {
assert(f1(3) > 3) by {
reveal(f1);
}
assert(f1(3) > 3);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_assertby1_fail1 verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn assertby_test() {
assert(f1(3) > 4) by { // FAILS
reveal(f1);
}
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_assertby1_fail2 verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn assertby_test() {
assert(f1(3) > 3) by {
reveal(f1);
}
assert(f1(3) == 4); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_assertby1_fail3 verus_code! {
proof fn consume(tracked x: bool) {
}
proof fn assertby_proof_var_disallowed(tracked x: bool) {
assert(true) by {
consume(x)
}
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode proof")
}
test_verify_one_file! {
#[test] test_forallstmt1 verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn forallstmt_test() {
assert forall|x: int| f1(x) > x by {
reveal(f1);
}
assert(f1(3) > 3);
}
fn forallstmt_test_inference() {
assert forall|x| f1(x) > x by {
reveal(f1);
}
assert(f1(3) > 3);
}
proof fn no_consume(x: bool) {
}
fn forallstmt_proof_var_allowed_as_spec(#[verifier::proof] x: bool) {
assert forall|i: int| f1(i) == f1(i) by {
no_consume(x);
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_forallstmt1_fail1 verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn forallstmt_test() {
assert forall|x: int| f1(x) < x by { // FAILS
reveal(f1);
}
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_forallstmt1_fail2 verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn forallstmt_test() {
assert forall|x: int| f1(x) > x by {
reveal(f1);
}
assert(f1(3) == 4); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_forallstmt1_fail3 verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
proof fn consume(tracked x: bool) {
}
proof fn forallstmt_proof_var_disallowed(tracked x: bool) {
assert forall|i: int| f1(i) == f1(i) by {
consume(x);
}
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode proof")
}
test_verify_one_file! {
#[test] test_forallstmt2 verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn forallstmt_test() {
assert forall|x: int| 0 <= x implies 1 <= f1(x) by {
reveal(f1);
}
assert(f1(3) > 0);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_forallstmt2_fails1 verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn forallstmt_test() {
assert forall|x: int| 0 <= x implies 1 <= f1(x) by {
reveal(f1);
}
assert(f1(-3) > 0); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_forallstmt2_fails2 verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn forallstmt_test() {
assert forall|x: int| 1 <= f1(x) by { // FAILS
reveal(f1);
}
assert(f1(3) > 0);
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_scope verus_code! {
spec fn f(j: int) -> bool { true }
fn scope(b: bool, i: u64) {
if b {
let i = 5;
assert forall|i: int| f(i) by {}
assert forall|j: int| f(j) by {}
} else {
let i = 6;
assert forall|i: int| f(i) by {}
assert forall|j: int| f(j) by {}
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_forallstmt_typ verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn forallstmt_test() {
assert forall|x: nat| 1 <= f1(x as int) by {
reveal(f1);
}
assert(f1(3) > 0);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_forallstmt_typ_fails verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn forallstmt_test() {
assert forall|x: nat| 1 <= f1(x as int) by {} // FAILS
assert(f1(3) > 0);
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_assertby1_let verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn assertby_test() {
assert({let a = 3; f1(a) > a}) by {
reveal(f1);
}
assert(f1(3) > 3);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_forallstmt_let verus_code! {
#[verifier(opaque)]
spec fn f1(i: int) -> int {
i + 1
}
fn forallstmt_test() {
assert forall|x: nat| { let a: int = 1; a <= #[trigger] f1(x as int) } by {
reveal(f1);
}
assert(f1(3) > 0);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_forallstmt_implies verus_code! {
spec fn f(x: int) -> bool;
spec fn g(x: int) -> bool;
proof fn x()
requires forall |x: int| f(x) ==> g(x)
{
assert forall |x: int| f(x) ==> g(x) by {
}
}
} => Ok(err) => {
assert!(err.errors.is_empty());
let mut warns = err.warnings.iter();
assert!(warns.next().expect("one warning").message.contains("using ==> in `assert forall` does not currently assume the antecedent in the body"));
assert!(warns.next().is_some());
assert!(warns.next().is_none());
}
}