-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathmodules.rs
More file actions
408 lines (348 loc) · 10.1 KB
/
Copy pathmodules.rs
File metadata and controls
408 lines (348 loc) · 10.1 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
const M1: &str = verus_code_str! {
mod M1 {
#[derive(PartialEq, Eq)]
pub struct Car {
pub four_doors: bool,
}
pub open spec fn is_four_doors(c: Car) -> bool {
c.four_doors
}
}
};
test_verify_one_file! {
#[test] test_mod_adt_0 M1.to_string() + verus_code_str! {
mod M2 {
use crate::M1::Car;
use verus_builtin::*;
fn mod_adt_0() {
assert(!Car { four_doors: false }.four_doors);
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_mod_adt_1 M1.to_string() + verus_code_str! {
mod M2 {
use crate::M1::{is_four_doors, Car};
use verus_builtin::*;
fn test() {
let c = Car { four_doors: true };
assert(is_four_doors(c));
}
}
} => Ok(())
}
const M1_OPAQUE: &str = verus_code_str! {
mod M1 {
#[derive(PartialEq, Eq)]
pub struct Car {
pub four_doors: bool,
}
#[verifier(opaque_outside_module)] /* vattr */
pub open spec fn is_four_doors(c: Car) -> bool {
c.four_doors
}
}
};
test_verify_one_file! {
#[test] test_mod_fn_publish_opaque_no_reveal M1_OPAQUE.to_string() + verus_code_str! {
mod M2 {
use crate::M1::{is_four_doors, Car};
use verus_builtin::*;
fn test() {
let c = Car { four_doors: true };
assert(is_four_doors(c)); // FAILS
}
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_mod_fn_publish_opaque_reveal M1_OPAQUE.to_string() + verus_code_str! {
mod M2 {
use crate::M1::{is_four_doors, Car};
use verus_builtin::*;
fn test() {
let c = Car { four_doors: true };
proof {
reveal(is_four_doors);
}
assert(is_four_doors(c));
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_mod_adt_no_verify verus_code! {
#[verifier(external_body)] /* vattr */
#[derive(PartialEq, Eq)]
pub struct Car {
pub four_doors: bool,
}
fn mod_adt_no_verify() {
assert(!Car { four_doors: false }.four_doors);
}
} => Err(err) => assert_vir_error_msg(err, "disallowed: field expression for an opaque datatype")
}
test_verify_one_file! {
#[test] test_mod_child_ok verus_code! {
spec fn f() -> bool {
true
}
mod M1 {
fn test()
ensures crate::f()
{
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_mod_sibling_fail verus_code! {
mod M0 {
pub closed spec fn f() -> bool {
true
}
}
mod M1 {
fn test()
ensures crate::M0::f() // FAILS
{
}
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_requires_private verus_code! {
mod M1 {
spec fn f() -> bool { true }
pub fn g()
requires f()
{
}
}
mod M2 {
fn h() {
crate::M1::g();
}
}
} => Err(err) => assert_vir_error_msg(err, "in 'requires' clause of public function, cannot refer to private function")
}
test_verify_one_file! {
#[test] test_publish_but_not_marked_pub verus_code! {
open spec fn bar() -> u64 {
7
}
} => Err(err) => assert_vir_error_msg(err, "function is marked `open` but not marked `pub`")
}
test_verify_one_file! {
#[test] publish_proof_fail verus_code! {
pub open proof fn bar() {
}
} => Err(err) => assert_vir_error_msg(err, "only `spec` functions can be marked `open`, `closed`, or `uninterp`")
}
test_verify_one_file! {
#[test] publish_exec_fail verus_code! {
pub open fn bar() {
}
} => Err(err) => assert_vir_error_msg(err, "only `spec` functions can be marked `open`, `closed`, or `uninterp`")
}
test_verify_one_file! {
#[test] main_proof_fail verus_code! {
pub proof fn main() {
}
} => Err(err) => assert_vir_error_msg(err, "`main` function should be #[verifier::exec]")
}
test_verify_one_file! {
#[test] main_spec_fail verus_code! {
pub closed spec fn main() {
()
}
} => Err(err) => assert_vir_error_msg(err, "`main` function should be #[verifier::exec]")
}
test_verify_one_file! {
#[test] open_fn_refers_to_private_const_fail verus_code! {
mod A {
spec const X: usize = 1;
pub open spec fn f() -> usize {
X
}
}
mod B {
use crate::A;
pub open spec fn g() -> bool {
A::f() == 1
}
}
} => Err(err) => assert_vir_error_msg(err, "in pub open spec function, cannot refer to private const")
}
test_verify_one_file! {
#[test] open_qualified_refers_to_private verus_code! {
mod m {
pub mod n {
use verus_builtin::*;
spec fn stuff() -> bool { true }
pub open(in crate::m) spec fn foo() -> bool {
stuff()
}
}
}
} => Err(err) => assert_vir_error_msg(err, "in pub open spec function, cannot refer to private function")
}
test_verify_one_file! {
#[test] open_crate verus_code! {
mod m {
pub mod n {
use verus_builtin::*;
pub open(crate) spec fn foo() -> bool {
true
}
proof fn test() {
assert(foo() == true);
}
}
proof fn test2() {
assert(n::foo() == true);
}
}
proof fn test3() {
assert(m::n::foo() == true);
}
} => Ok(())
}
test_verify_one_file! {
#[test] open_super verus_code! {
mod m {
pub mod n {
use verus_builtin::*;
pub open(super) spec fn foo() -> bool {
true
}
proof fn test() {
assert(foo() == true);
}
}
proof fn test2() {
assert(n::foo() == true);
}
}
proof fn test3() {
assert(m::n::foo() == true); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] open_path verus_code! {
mod m {
pub mod n {
use verus_builtin::*;
pub open(in crate::m) spec fn foo() -> bool {
true
}
proof fn test() {
assert(foo() == true);
}
}
proof fn test2() {
assert(n::foo() == true);
}
}
proof fn test3() {
assert(m::n::foo() == true); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] open_more_public_than_function verus_code! {
mod m {
pub mod n {
use verus_builtin::*;
pub(in crate::m) open(crate) spec fn foo() -> bool {
true
}
}
}
} => Err(err) => assert_vir_error_msg(err, "the function body is declared 'open' to a wider scope than the function itself")
}
test_verify_one_file! {
#[test] uninterp_exec_fail verus_code! {
pub uninterp fn bar() {
}
} => Err(err) => assert_vir_error_msg(err, "only `spec` functions can be marked `open`, `closed`, or `uninterp`")
}
test_verify_one_file! {
#[test] uninterp_spec_body_free_fail verus_code! {
pub uninterp spec fn bar() -> bool {
true
}
} => Err(err) => assert_vir_error_msg(err, "function is marked `uninterp` but it has a body")
}
test_verify_one_file! {
#[test] uninterp_spec_body_assoc_fail verus_code! {
struct G {
v: bool,
}
impl G {
pub uninterp spec fn bar(&self) -> bool {
self.v
}
}
} => Err(err) => assert_vir_error_msg(err, "function is marked `uninterp` but it has a body")
}
test_verify_one_file! {
#[test] uninterp_spec_body_trait_fail verus_code! {
trait T {
uninterp spec fn bar(&self) -> bool {
true
}
}
} => Err(err) => assert_vir_error_msg(err, "function is marked `uninterp` but it has a body")
}
test_verify_one_file! {
#[test] uninterp_spec_body_trait_impl_fail verus_code! {
trait T {
uninterp spec fn bar(&self) -> bool;
#[verifier::external_body]
proof fn a(&self)
ensures self.bar()
{
}
}
impl T for bool {
spec fn bar(&self) -> bool { // this should be rejected
*self
}
}
proof fn a() {
let t = false;
t.a();
assert(t.bar());
}
} => Err(err) => assert_vir_error_msg(err, "trait method implementation cannot be marked as `uninterp`")
}
test_verify_one_file! {
// TODO reject this code
#[ignore] #[test] closed_spec_trait_fail verus_code! {
trait T {
closed spec fn bar(&self) -> bool {
true
}
}
} => Err(_err) => todo!()
}
test_verify_one_file! {
#[ignore] #[test] uninterp_spec_fn_warning verus_code! {
spec fn bar(i: nat) -> bool;
} => Ok(err) => {
assert!(err.errors.is_empty());
assert!(err.warnings.iter().find(|w| w.message.contains("uninterpreted functions")).iter().next().is_some());
}
}
test_verify_one_file! {
#[test] uninterp_open_fail verus_code! {
pub uninterp open fn bar();
} => Err(err) => assert_vir_error_msg(err, "expected `fn`")
}