-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathstd.rs
545 lines (443 loc) · 13.1 KB
/
std.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
test_verify_one_file! {
#[test] rc_arc verus_code! {
use std::rc::Rc;
use std::sync::Arc;
use vstd::*;
fn foo() {
let x = Rc::new(5);
assert(*x == 5) by {}
let x1 = x.clone();
assert(*x1 == 5) by {}
let y = Arc::new(5);
assert(*y == 5) by {}
let y1 = y.clone();
assert(*y1 == 5) by {}
}
} => Ok(())
}
test_verify_one_file! {
#[test] ref_clone verus_code! {
struct X { }
fn test2(x: &X) { }
fn test(x: &X) {
// Since X is not clone, Rust resolves this to a clone of the reference type &X
// So this is basically the same as `let y = x;`
let y = x.clone();
assert(x == y);
test2(y);
}
} => Ok(())
}
test_verify_one_file! {
#[test] external_clone_fail verus_code! {
// Make sure the support for &X clone doesn't mistakenly trigger in other situations
struct X { }
#[verifier(external)]
impl Clone for X {
fn clone(&self) -> Self {
X { }
}
}
fn foo(x: &X) {
let y = x.clone();
}
} => Err(err) => assert_vir_error_msg(err, "cannot use function `crate::X::clone` which is ignored")
}
test_verify_one_file! {
#[test] box_new verus_code! {
use vstd::*;
fn foo() {
let x: Box<u32> = Box::new(5);
assert(*x == 5);
}
} => Ok(())
}
// Indexing into vec
test_verify_one_file! {
#[test] index_vec_out_of_bounds verus_code! {
use vstd::*;
fn stuff<T>(v: Vec<T>) {
let x = &v[0]; // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] index_vec_out_of_bounds2 verus_code! {
use vstd::*;
fn stuff<T>(v: &Vec<T>) {
let x = &v[0]; // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] index_vec_out_of_bounds3 verus_code! {
use vstd::*;
fn stuff<T>(v: &Vec<T>) {
let x = v[0]; // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] index_vec_in_bounds verus_code! {
use vstd::*;
fn stuff(v: &Vec<u8>)
requires v.len() > 0,
{
let a = v[0] < v[0];
assert(a == false);
}
} => Ok(())
}
test_verify_one_file! {
#[test] index_vec_in_bounds2 verus_code! {
use vstd::prelude::*;
fn stuff(v: &mut Vec<u8>)
requires old(v).len() > 0,
{
let a = v[0];
assert(a == v.view().index(0));
}
} => Ok(())
}
test_verify_one_file! {
#[test] index_vec_in_bounds3 verus_code! {
use vstd::prelude::*;
fn stuff(v: &mut Vec<u8>)
requires old(v).len() > 0,
{
let a = &v[0];
assert(*a == v.view().index(0));
}
} => Ok(())
}
test_verify_one_file! {
#[test] index_vec_move_error verus_code! {
use vstd::*;
fn stuff<T>(v: Vec<T>) {
let x = v[0];
}
} => Err(err) => assert_rust_error_msg(err, "cannot move out of index of `std::vec::Vec<T>`")
}
test_verify_one_file! {
#[test] index_vec_move_error2 verus_code! {
use vstd::*;
fn stuff<T>(v: &mut Vec<T>) {
let x = v[0];
}
} => Err(err) => assert_rust_error_msg(err, "cannot move out of index of `std::vec::Vec<T>`")
}
test_verify_one_file! {
#[test] index_vec_mut_error verus_code! {
use vstd::*;
fn foo(t: &mut u8) { }
fn stuff(v: Vec<u8>) {
foo(&mut v[0]);
}
} => Err(err) => assert_vir_error_msg(err, "index for &mut not supported")
}
test_verify_one_file! {
#[test] signed_wrapping_mul verus_code! {
use vstd::*;
fn test() {
let i = (1000 as i64).wrapping_mul(2000);
assert(i == 2000000);
let i = (1000 as i64).wrapping_mul(-2000);
assert(i == -2000000);
let i = (12345678901 as i64).wrapping_mul(45678912301);
assert(i == -7911882469911038895);
let i = (92345678901 as i64).wrapping_mul(175678912301);
assert(i == 8500384234389190737);
let i = (12 as i64).wrapping_mul(2305843009213693952);
assert(i == -9223372036854775808);
assert(false); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] question_mark_option verus_code! {
use vstd::*;
fn test() -> (res: Option<u32>)
ensures res.is_none()
{
let x: Option<u8> = None;
let y = x?;
assert(false);
return None;
}
fn test2() -> (res: Option<u32>)
{
let x: Option<u8> = Some(5);
let y = x?;
assert(false); // FAILS
return None;
}
fn test3() -> (res: Option<u32>)
ensures res.is_some(),
{
let x: Option<u8> = None;
let y = x?; // FAILS
return Some(13);
}
fn test4() -> (res: Option<u32>)
ensures false,
{
let x: Option<u8> = Some(12);
let y = x?;
assert(y == 12);
loop { }
}
} => Err(err) => assert_fails(err, 2)
}
test_verify_one_file! {
#[test] question_mark_result verus_code! {
use vstd::*;
fn test() -> (res: Result<u32, bool>)
ensures res === Err(false),
{
let x: Result<u8, bool> = Err(false);
let y = x?;
assert(false);
return Err(true);
}
fn test2() -> (res: Result<u32, bool>)
{
let x: Result<u8, bool> = Ok(5);
let y = x?;
assert(false); // FAILS
return Err(false);
}
fn test3() -> (res: Result<u32, bool>)
ensures res.is_ok(),
{
let x: Result<u8, bool> = Err(false);
let y = x?; // FAILS
return Ok(13);
}
fn test4() -> (res: Result<u32, bool>)
ensures false,
{
let x: Result<u8, bool> = Ok(12);
let y = x?;
assert(y == 12);
loop { }
}
} => Err(err) => assert_fails(err, 2)
}
test_verify_one_file! {
#[test] clone_for_std_types verus_code! {
use vstd::*;
use vstd::prelude::*;
fn test_bool(v: bool) {
let w = v.clone();
assert(w == v);
}
fn test_bool_vec(v: Vec<bool>) {
let w = v.clone();
assert(w@ =~= v@);
}
fn test_char(v: char) {
let w = v.clone();
assert(w == v);
}
fn test_char_vec(v: Vec<char>) {
let w = v.clone();
assert(w@ =~= v@);
}
struct Y { }
fn test_vec_ref(v: Vec<&Y>) {
let w = v.clone();
assert(w@ =~= v@);
}
struct X { i: u64 }
impl Clone for X {
fn clone(&self) -> Self { X { i: 0 } }
}
fn test_vec_fail(v: Vec<X>)
requires v.len() >= 1,
{
let w = v.clone();
assert(v[0] == w[0]); // FAILS
}
fn test_vec_deep_view() {
let mut v1: Vec<u8> = Vec::new();
v1.push(3);
v1.push(4);
let c1 = v1.clone();
let ghost g1 = c1@ == v1@;
assert(g1);
let mut v2: Vec<Vec<u8>> = Vec::new();
v2.push(v1.clone());
v2.push(v1.clone());
let c2 = v2.clone();
let ghost g2 = c2.deep_view() == v2.deep_view();
assert(g2);
assert(c2@ == v2@); // FAILS
}
fn test_vec_deep_view_char() {
let mut v1: Vec<char> = Vec::new();
v1.push('a');
v1.push('b');
let c1 = v1.clone();
let ghost g1 = c1@ == v1@;
assert(g1);
let mut v2: Vec<Vec<char>> = Vec::new();
v2.push(v1.clone());
v2.push(v1.clone());
let c2 = v2.clone();
let ghost g2 = c2.deep_view() == v2.deep_view();
assert(g2);
assert(c2@ == v2@); // FAILS
}
fn test_slice_deep_view(a1: &[Vec<u8>], a2: &[Vec<u8>])
requires
a1.len() == 1,
a2.len() == 1,
a1[0].len() == 1,
a2[0].len() == 1,
a1[0][0] == 10,
a2[0][0] == 10,
ensures
a1.deep_view() == a2.deep_view(),
{
assert(a1.deep_view() =~~= a2.deep_view()); // TODO: get rid of this?
}
fn test_array_deep_view(a1: &[Vec<u8>; 1], a2: &[Vec<u8>; 1])
requires
a1[0].len() == 1,
a2[0].len() == 1,
a1[0][0] == 10,
a2[0][0] == 10,
ensures
a1.deep_view() == a2.deep_view(),
{
assert(a1.deep_view() =~~= a2.deep_view()); // TODO: get rid of this?
}
} => Err(err) => assert_fails(err, 3)
}
test_verify_one_file! {
#[test] vec_macro verus_code! {
use vstd::*;
use vstd::prelude::*;
fn test() {
let v = vec![7, 8, 9];
assert(v.len() == 3);
assert(v[0] == 7);
assert(v[1] == 8);
assert(v[2] == 9);
}
} => Ok(())
}
test_verify_one_file! {
#[test] box_globals_no_trait_conflict verus_code! {
use vstd::*;
use core::alloc::Allocator;
trait Tr {
spec fn some_int() -> int;
}
spec fn some_int0<A: Allocator>() -> int;
spec fn some_int<A: Allocator>(b: Box<u8, A>) -> int {
some_int0::<A>()
}
proof fn test<A: Allocator>(b: Box<u8, A>, c: Box<u8>)
requires b == c
{
assert(some_int(b) == some_int(c)); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] phantom_data_is_unit verus_code! {
use core::marker::PhantomData;
use vstd::prelude::*;
proof fn stuff(a: PhantomData<u64>, b: PhantomData<u64>) {
assert(a == b);
assert(a == PhantomData::<u64>);
}
fn stuff2(a: PhantomData<u64>, b: PhantomData<u64>) {
assert(a == b);
let z = PhantomData::<u64>;
assert(a == z);
}
} => Ok(())
}
test_verify_one_file! {
#[test] box_clone verus_code! {
use vstd::*;
fn test(a: Box<u8>) {
let b = a.clone();
assert(a == b);
}
fn test2<T: Clone>(a: Box<T>) {
let b = a.clone();
assert(a == b); // FAILS
}
fn test3<T: Clone>(a: Box<T>) {
let b = a.clone();
assert(call_ensures(T::clone, (&*a,), *b) || a == b);
}
fn test3_fails<T: Clone>(a: Box<T>) {
let b = a.clone();
assert(call_ensures(T::clone, (&*a,), *b)); // FAILS
}
pub struct X { pub i: u64 }
impl Clone for X {
fn clone(&self) -> (res: Self)
ensures res == (X { i: 5 }),
{
X { i: 5 }
}
}
fn test4(a: Box<X>) {
let b = a.clone();
assert(a == b); // FAILS
}
fn test5(a: Box<X>) {
let b = a.clone();
assert(b == X { i: 5 } || b == a);
}
} => Err(err) => assert_fails(err, 3)
}
test_verify_one_file! {
#[test] tuple_clone_not_supported verus_code! {
use vstd::*;
fn stuff(a: (u8, u8)) {
let b = a.clone();
assert(a == b); // FAILS
}
} => Err(err) => assert_vir_error_msg(err, "The verifier does not yet support the following Rust feature: instance")
}
test_verify_one_file! {
#[test] derive_copy verus_code! {
// When an auto-derived impl is produce, it doesn't get the verus_macro attribute.
// However, this test case does not use --external-by-default, so verus will
// process the derived impls anyway. This currently causes verus to fail,
// but should probably be fixed.
#[derive(Clone, Copy)]
struct X {
u: u64,
}
fn test(x: X) {
let a = x;
let b = x;
}
} => Err(err) => assert_vir_error_msg(err, "let-pattern declaration must have an initializer")
}
test_verify_one_file_with_options! {
#[test] derive_copy_external_by_default ["--external-by-default"] => verus_code! {
// When an auto-derived impl is produce, it doesn't get the verus_macro attribute.
// Since this test case uses --external-by-default, these derived impls do not
// get processed.
#[derive(Clone, Copy)]
struct X {
u: u64,
}
fn test(x: X) {
let a = x;
let b = x;
}
} => Ok(())
}