-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathmodes.rs
1501 lines (1295 loc) · 39.6 KB
/
modes.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
test_verify_one_file! {
#[test] struct1 code! {
struct S {
#[verifier::spec] i: bool,
j: bool,
}
fn test1(i: bool, j: bool) {
let s = S { i, j };
}
fn test2(#[verifier::spec] i: bool, j: bool) {
let s = S { i, j };
}
fn test3(i: bool, #[verifier::spec] j: bool) {
#[verifier::spec] let s = S { i, j };
#[verifier::spec] let jj = s.j;
}
} => Ok(())
}
test_verify_one_file! {
#[test] verus_struct1 verus_code! {
use vstd::modes::*;
struct S {
i: Ghost<bool>,
j: bool,
}
fn test1(i: bool, j: bool) {
let s = S { i: Ghost(i), j };
}
fn test2(i: Ghost<bool>, j: bool) {
let s = S { i, j };
}
fn test3(i: bool, j: Ghost<bool>) {
let s: Ghost<S> = Ghost(S { i: Ghost::new(i), j: j@ });
let jj: Ghost<bool> = Ghost([email protected]);
}
} => Ok(())
}
test_verify_one_file! {
#[test] struct_fails1 code! {
struct S {
#[verifier::spec] i: bool,
j: bool,
}
fn test(i: bool, #[verifier::spec] j: bool) {
let s = S { i, j };
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] verus_struct_fails1 verus_code! {
use vstd::modes::*;
struct S {
i: Ghost<bool>,
j: bool,
}
fn test(i: bool, j: Ghost<bool>) {
let s = S { i: Ghost(i), j: j@ };
}
} => Err(err) => assert_vir_error_msg(err, "cannot perform operation with mode spec")
}
test_verify_one_file! {
#[test] struct_fails1b code! {
struct S {
#[verifier::spec] i: bool,
j: bool,
}
fn test(i: bool, #[verifier::spec] j: bool) {
let s = S { j, i };
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] struct_fails2 code! {
struct S {
#[verifier::spec] i: bool,
j: bool,
}
fn test(i: bool, j: bool) {
let s = S { i, j };
let ii = s.i;
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] struct_fails3 code! {
struct S {
#[verifier::spec] i: bool,
j: bool,
}
fn test(i: bool, #[verifier::spec] j: bool) {
#[verifier::spec] let s = S { i, j };
let jj = s.j;
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] struct_fails4a verus_code! {
struct S {
ghost i: bool,
j: bool,
}
fn test(s: Ghost<S>) -> bool {
}
} => Err(err) => assert_vir_error_msg(err, "cannot perform operation with mode spec")
}
test_verify_one_file! {
#[test] struct_fails4b verus_code! {
struct S {
ghost i: bool,
j: bool,
}
fn test(s: &Ghost<S>) -> bool {
}
} => Err(err) => assert_vir_error_msg(err, "cannot perform operation with mode spec")
}
test_verify_one_file! {
#[test] struct_fails4c verus_code! {
struct S {
ghost i: bool,
j: bool,
}
fn test(s: Ghost<&S>) -> bool {
}
} => Err(err) => assert_vir_error_msg(err, "cannot perform operation with mode spec")
}
test_verify_one_file! {
#[test] struct_fails5a verus_code! {
struct S {
ghost i: bool,
j: bool,
}
impl S {
spec fn get_j(self) -> bool {
self.j
}
}
fn test(s: Ghost<S>) -> bool {
}
} => Err(err) => assert_vir_error_msg(err, "cannot call function `crate::S::get_j` with mode spec")
}
test_verify_one_file! {
#[test] struct_fails5b verus_code! {
struct S {
ghost i: bool,
j: bool,
}
impl S {
spec fn get_j(self) -> bool {
self.j
}
}
fn test(s: &Ghost<S>) -> bool {
}
} => Err(err) => assert_vir_error_msg(err, "cannot call function `crate::S::get_j` with mode spec")
}
test_verify_one_file! {
#[test] struct_fails5c verus_code! {
struct S {
ghost i: bool,
j: bool,
}
impl S {
spec fn get_j(self) -> bool {
self.j
}
}
fn test(s: Ghost<&S>) -> bool {
}
} => Err(err) => assert_vir_error_msg(err, "cannot call function `crate::S::get_j` with mode spec")
}
test_verify_one_file! {
#[test] tuple1 code! {
fn test1(i: bool, j: bool) {
let s = (i, j);
}
fn test3(i: bool, #[verifier::spec] j: bool) {
#[verifier::spec] let s = (i, j);
#[verifier::spec] let ii = s.0;
#[verifier::spec] let jj = s.1;
}
} => Ok(())
}
test_verify_one_file! {
#[test] tuple_fails1 code! {
fn test(i: bool, #[verifier::spec] j: bool) {
let s = (i, j);
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] tuple_fails2 code! {
fn test(i: bool, j: bool) {
#[verifier::spec] let s = (i, j);
let ii = s.0;
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] tuple_fails3 code! {
fn test(i: bool, #[verifier::spec] j: bool) {
#[verifier::spec] let s = (i, j);
let jj = s.0;
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] spec_struct_not_exec verus_code! {
ghost struct Set<A> {
pub dummy: A,
}
fn set_exec() {
let a: Set<u64> = Set { dummy: 3 }; // FAILS
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] spec_enum_not_exec verus_code! {
ghost enum E {
A,
B,
}
fn set_exec() {
let e: E = E::A; // FAILS
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] eq_mode code! {
fn eq_mode(#[verifier::spec] i: u128) {
#[verifier::spec] let b: bool = i == 13;
}
} => Ok(_)
}
test_verify_one_file! {
#[test] if_spec_cond code! {
fn if_spec_cond(#[verifier::spec] i: u128) -> u64 {
let mut a: u64 = 2;
if i == 3 {
a = a + 1; // ERROR
}
a
}
} => Err(err) => assert_vir_error_msg(err, "cannot assign to exec variable from proof mode")
}
test_verify_one_file! {
#[test] if_spec_cond_proof code! {
#[verifier::proof]
fn if_spec_cond_proof(i: u128) -> u64 {
let mut a: u64 = 2;
if i == 3 {
a = a + 1;
}
a
}
} => Ok(())
}
test_verify_one_file! {
#[test] regression_int_if code! {
use vstd::pervasive::*;
fn int_if() {
#[verifier::spec] let a: u128 = 3;
if a == 4 {
assert(false);
}; // TODO not require the semicolon here?
}
#[verifier::spec]
fn int_if_2(a: u128) -> u128 {
if a == 2 {
3
} else if a == 3 {
4
} else {
vstd::pervasive::arbitrary()
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] ret_mode code! {
#[verifier::returns(spec)] /* vattr */
fn ret_spec() -> u128 {
ensures(|i: u128| i == 3);
#[verifier::spec] let a: u128 = 3;
a
}
fn test_ret() {
#[verifier::spec] let x = ret_spec();
builtin::assert_(x == 3);
}
} => Ok(())
}
test_verify_one_file! {
#[test] ret_mode_fail2 code! {
#[verifier::returns(spec)] /* vattr */
fn ret_spec() -> u128 {
ensures(|i: u128| i == 3);
#[verifier::spec] let a: u128 = 3;
a
}
fn test_ret() {
let x = ret_spec();
builtin::assert_(x == 3);
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] ret_mode_fail_requires code! {
fn f() {
requires({while false {}; true});
}
} => Err(err) => assert_vir_error_msg(err, "expected pure mathematical expression")
}
test_verify_one_file! {
#[test] spec_let_decl_init_fail code! {
#[verifier::spec]
fn test1() -> u64 {
let x: u64;
x = 23;
x
}
} => Err(err) => assert_vir_error_msg(err, "delayed assignment to non-mut let not allowed for spec variables")
}
test_verify_one_file! {
#[test] let_spec_pass code! {
fn test1() {
#[verifier::spec] let x: u64 = 2;
builtin::assert_(x == 2);
}
} => Ok(())
}
test_verify_one_file! {
#[test] decl_init_let_spec_fail code! {
fn test1() {
#[verifier::spec] let x: u64;
x = 2;
x = 3;
builtin::assert_(false); // FAILS
}
} => Err(err) => assert_vir_error_msg(err, "delayed assignment to non-mut let not allowed for spec variables")
}
const FIELD_UPDATE: &str = code_str! {
#[derive(PartialEq, Eq, Structural)]
struct S {
#[verifier::spec] a: u64,
b: bool,
}
};
test_verify_one_file! {
#[test] test_field_update_fail FIELD_UPDATE.to_string() + code_str! {
fn test() {
let mut s = S { a: 5, b: false };
#[verifier::spec] let b = true;
s.b = b;
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode exec")
}
test_verify_one_file! {
#[test] test_mut_ref_field_fail FIELD_UPDATE.to_string() + code_str! {
fn muts_exec(a: &mut u64) {
requires(*old(a) < 30);
ensures(*a == *old(a) + 1);
*a = *a + 1;
}
fn test() {
let mut s = S { a: 5, b: false };
muts_exec(&mut s.a);
}
} => Err(err) => assert_vir_error_msg(err, "expected mode exec, &mut argument has mode spec")
}
const PROOF_FN_COMMON: &str = code_str! {
#[verifier::proof]
struct Node {
v: u32,
}
};
test_verify_one_file! {
#[test] test_mut_arg_fail1 code! {
#[verifier::proof]
fn f(#[verifier::proof] x: &mut bool, #[verifier::proof] b: bool) {
requires(b);
ensures(*x);
*x = b;
}
fn g(#[verifier::proof] b: bool) {
requires(b);
#[verifier::spec] let tr = true;
let mut e = false;
if tr {
f(&mut e, b); // should fail: exec <- proof out assign
}
builtin::assert_(e);
}
} => Err(err) => assert_vir_error_msg(err, "expected mode proof, &mut argument has mode exec")
}
test_verify_one_file! {
#[test] test_mut_arg_fail2 verus_code! {
proof fn f(x: &mut bool)
ensures *x
{
*x = true;
}
fn g() {
let mut e = false;
proof {
f(&mut e); // fails, exec <- ghost out assign
}
assert(e);
}
} => Err(err) => assert_vir_error_msg(err, "expected mode spec, &mut argument has mode proof")
}
test_verify_one_file! {
#[test] test_mut_arg_fail3 verus_code! {
struct S {
ghost g: bool,
}
fn f(x: &mut bool) {}
fn g(e: S) {
let mut e = e;
f(&mut e.g); // fails, exec <- ghost assign
}
} => Err(err) => assert_vir_error_msg(err, "expected mode exec, &mut argument has mode spec")
}
test_verify_one_file! {
#[test] test_mut_arg_fail4 verus_code! {
struct S {
e: bool,
}
proof fn f(tracked x: &mut bool) {}
proof fn g(g: S) {
let mut g = g;
f(&mut g.e); // fails, tracked <- ghost assign
}
} => Err(err) => assert_vir_error_msg(err, "expected mode proof, &mut argument has mode spec")
}
test_verify_one_file! {
#[test] test_mut_arg_fail5 verus_code! {
struct S {
e: bool,
}
proof fn f(x: &mut bool) {}
fn g(e: S) {
let mut e = e;
proof {
f(&mut e.e); // fails, exec <- ghost out assign
}
}
} => Err(err) => assert_vir_error_msg(err, "expected mode spec, &mut argument has mode proof")
}
test_verify_one_file! {
#[test] test_mut_arg_fail6 verus_code! {
struct S {
tracked t: bool,
}
proof fn f(x: &mut bool) {}
fn g(e: S) {
let mut e = e;
proof {
f(&mut e.t); // fails, tracked <- ghost out assign
}
}
} => Err(err) => assert_vir_error_msg(err, "expected mode spec, &mut argument has mode proof")
}
test_verify_one_file! {
// TODO (erasure-todo)
// This needs to be ported to verus_code
#[ignore] #[test] test_proof_fn_call_fail PROOF_FN_COMMON.to_string() + code_str! {
#[verifier::proof]
fn lemma(#[verifier::proof] node: Node) {
requires(node.v < 10);
ensures(node.v * 2 < 20);
}
#[verifier::proof]
fn other(#[verifier::proof] node: Node) {
builtin::assume_(node.v < 10);
lemma(node);
lemma(node);
}
} => Err(err) => assert_rust_error_msg(err, "error[E0382]: use of moved value: `node`")
}
test_verify_one_file! {
#[test] test_associated_proof_fn_call_pass PROOF_FN_COMMON.to_string() + code_str! {
impl Node {
#[verifier::proof]
fn lemma(&self) {
requires(self.v < 10);
ensures(self.v * 2 < 20);
}
#[verifier::proof]
fn other(&self, other_node: Node) {
builtin::assume_(other_node.v < 10);
other_node.lemma();
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_associated_proof_fn_call_fail_1 PROOF_FN_COMMON.to_string() + verus_code_str! {
impl Node {
proof fn lemma(tracked self) {
requires(self.v < 10);
ensures(self.v * 2 < 20);
}
proof fn other(tracked self) {
assume(other_node.v < 10);
self.lemma();
self.lemma();
}
}
} => Err(err) => assert_rust_error_msg(err, "cannot find value `other_node`")
}
test_verify_one_file! {
#[test] test_associated_proof_fn_call_fail_2_regression_124 PROOF_FN_COMMON.to_string() + verus_code_str! {
struct Token {}
impl Node {
proof fn lemma(self, tracked t: Token) {}
proof fn other(self, tracked t: Token) {
self.lemma(t);
self.lemma(t);
}
}
} => Err(err) => assert_vir_error_msg(err, "use of moved value: `t`")
}
test_verify_one_file! {
#[test] assign_from_proof code! {
fn myfun(#[verifier::spec] a: bool) -> bool {
let mut b = false;
if a {
b = true;
}
b
}
} => Err(err) => assert_vir_error_msg(err, "cannot assign to exec variable from proof mode")
}
test_verify_one_file! {
// TODO(main_new) this should be passing; the issue may be due to the changes in the lifetime checker
#[ignore] #[test] tracked_double_deref code! {
use vstd::modes::*;
fn foo<V>(x: Tracked<V>) {
let y = &x;
builtin::assert_(equal((*y).view(), x.view()));
}
} => Ok(())
}
test_verify_one_file! {
#[test] ghost_wrapper_assign_fail1 verus_code! {
use vstd::modes::*;
fn f() {
let g: Ghost<bool> = Ghost(true);
proof {
let tracked t: bool = g@; // fails: tracked <- ghost assign
}
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode proof")
}
test_verify_one_file! {
#[test] ghost_wrapper_assign_fail2 verus_code! {
use vstd::modes::*;
fn f() {
let g: Ghost<bool> = Ghost(true);
let e: bool = g@; // fails: exec <- ghost assign
}
} => Err(err) => assert_vir_error_msg(err, "cannot perform operation with mode spec")
}
test_verify_one_file! {
#[test] ghost_wrapper_assign_fail3 verus_code! {
use vstd::modes::*;
fn f() {
let mut e: bool = false;
proof {
e = true; // fails: exec assign from proof mode
}
}
} => Err(err) => assert_vir_error_msg(err, "cannot assign to exec variable from proof mode")
}
test_verify_one_file! {
#[test] ghost_wrapper_assign_fail4 verus_code! {
use vstd::modes::*;
fn f(t: Tracked<bool>) {
let g: Ghost<bool> = Ghost(true);
let mut t = t;
proof {
t@ = g@; // fails: tracked <- ghost assign
}
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode proof")
}
test_verify_one_file! {
#[test] ghost_wrapper_call_fail1 verus_code! {
use vstd::modes::*;
fn f(x: bool) {
}
fn g(g: Ghost<bool>) {
f(g@); // fails, exec <- ghost assign
}
} => Err(err) => assert_vir_error_msg(err, "cannot perform operation with mode spec")
}
test_verify_one_file! {
#[test] ghost_wrapper_call_fail2 verus_code! {
use vstd::modes::*;
fn f(x: bool) {
}
fn g(t: Tracked<bool>) {
f(t@); // fails, exec <- tracked assign
}
} => Err(err) => assert_vir_error_msg(err, "cannot perform operation with mode spec")
}
test_verify_one_file! {
#[test] ghost_wrapper_call_fail3 verus_code! {
use vstd::modes::*;
proof fn f(tracked x: bool) {
}
fn g(g: Ghost<bool>) {
proof {
f(g@); // fails, tracked <- ghost assign
}
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode proof")
}
test_verify_one_file! {
#[test] ghost_wrapper_call_mut_fail1 verus_code! {
use vstd::modes::*;
fn f(x: &mut bool) {
}
fn g(g: Ghost<bool>) {
let mut g = g;
f(g.borrow_mut()); // fails, exec <- ghost assign
}
} => Err(err) => assert_vir_error_msg(err, "cannot perform operation with mode proof")
}
test_verify_one_file! {
#[test] ghost_wrapper_call_mut_fail2 verus_code! {
use vstd::modes::*;
fn f(x: &mut bool) {
}
fn g(t: Tracked<bool>) {
let mut t = t;
f(t.borrow_mut()); // fails, exec <- tracked assign
}
} => Err(err) => assert_vir_error_msg(err, "cannot perform operation with mode proof")
}
test_verify_one_file! {
#[test] ghost_wrapper_call_mut_fail3 verus_code! {
use vstd::modes::*;
proof fn f(tracked x: &mut bool) {
}
fn g(g: Ghost<bool>) {
let mut g = g;
proof {
f(g.borrow_mut()); // fails, tracked <- ghost assign
}
}
} => Err(err) => assert_vir_error_msg(err, "expected mode proof, &mut argument has mode spec")
}
test_verify_one_file! {
#[test] ghost_wrapper_call_mut_fail4 verus_code! {
use vstd::modes::*;
proof fn f(x: &mut bool) {
}
fn g(t: Tracked<bool>) {
let mut t = t;
proof {
f(t.borrow_mut()); // fails, tracked <- ghost out assign
}
}
} => Err(err) => assert_vir_error_msg(err, "expected mode spec, &mut argument has mode proof")
}
test_verify_one_file! {
#[test] ghost_wrapper_assign_struct_fail1 verus_code! {
use vstd::modes::*;
struct S {
e: bool,
}
fn f(g: Ghost<S>) {
proof {
let tracked t: bool = [email protected]; // fails: tracked <- ghost assign
}
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode proof")
}
test_verify_one_file! {
#[test] ghost_wrapper_assign_struct_fail2 verus_code! {
use vstd::modes::*;
struct S {
e: bool,
}
fn f(g: Ghost<S>) {
let e: bool = [email protected]; // fails: exec <- ghost assign
}
} => Err(err) => assert_vir_error_msg(err, "cannot perform operation with mode spec")
}
test_verify_one_file! {
#[test] ghost_wrapper_assign_struct_fail3 verus_code! {
use vstd::modes::*;
struct S {
e: bool,
}
fn f(t: Tracked<S>) {
let g: Ghost<bool> = Ghost(true);
let mut t = t;
proof {
[email protected] = g@; // fails: tracked <- ghost assign
}
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode proof")
}
const TRACKED_TYP_PARAMS_COMMON: &str = verus_code_str! {
use vstd::modes::*;
tracked struct Tok {
v: nat,
}
tracked struct B<T> {
t: T,
}
};
test_verify_one_file! {
#[test] tracked_ghost_typ_params_make verus_code! {
use vstd::modes::*;
tracked struct Tok {
ghost v: nat,
}
struct B<T> {
t: T,
}
proof fn make_tracked_proof() {
let tracked t2: B<Tok> = B { t: Tok { v: 12 } };
}
fn make_tracked_exec() {
let t: Tracked<Tok> = Tracked({
let v = 12nat;
Tok { v: v }
});
let b: B<Tracked<Tok>> = B { t: t };
}
// This isn't currently possible
// proof fn make_ghost_proof() {
// let tracked t2: B<Ghost<Tok>> = Tracked(B { t: Ghost::new(Tok { v: 12 }) });
// }
fn make_ghost_exec() {
let g: Ghost<Tok> = Ghost(Tok { v: 12nat });
let b: B<Ghost<Tok>> = B { t: g };
}
} => Ok(())
}
test_verify_one_file! {
#[test] tracked_tracked_typ_params_misc TRACKED_TYP_PARAMS_COMMON.to_owned() + verus_code_str! {
proof fn identity(tracked b: B<Tracked<Tok>>) -> (tracked out: B<Tracked<Tok>>) {
b
}
fn foo_exec(tok: Tracked<Tok>) -> Tracked<Tok> {
let b: Tracked<B<Tracked<Tok>>> = Tracked(B { t: tok });
let t = Tracked({
let tracked B { t } = b.get();
t.get()
});
t
}
proof fn foo_proof(tracked tok: Tracked<Tok>) -> (tracked out: B<Tracked<Tok>>) {
let tracked b1: B<Tracked<Tok>> = B { t: tok };
let tracked b2 = identity(b1);
b2
}
fn caller(tok: Tracked<Tok>) -> Tracked<B<Tracked<Tok>>> {
let b: Tracked<B<Tracked<Tok>>> = Tracked(B { t: tok });
let b1 = Tracked({
identity(b.get())
});
b1
}
} => Ok(())
}
test_verify_one_file! {
#[test] tracked_ghost_typ_params_misc TRACKED_TYP_PARAMS_COMMON.to_owned() + verus_code_str! {
use vstd::modes::*;
proof fn identity(tracked b: B<Ghost<Tok>>) -> (tracked out: B<Ghost<Tok>>) {
b
}
fn foo_exec() -> Ghost<Tok> {
let g: Ghost<Tok> = Ghost(Tok { v: 12nat });
// The exec->tracked coercion may be removed
let b: Tracked<B<Ghost<Tok>>> = Tracked(B { t: g });
let t = Ghost({
let tracked B { t } = b.get();
t@
});
t
}
proof fn foo_proof(tracked tok: Ghost<Tok>) -> tracked Ghost<Tok> {
let tracked b: B<Ghost<Tok>> = B { t: tok };
let tracked t = {
let tracked B { t } = b;
t
};
t
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_or_pattern_mode_inconsistent verus_code! {
enum Foo {
Bar(#[verifier::spec] u64),
Qux(#[verifier::proof] u64),
}
proof fn blah(foo: Foo) {
#[verifier::proof] let (Foo::Bar(x) | Foo::Qux(x)) = foo;
}
} => Err(err) => assert_vir_error_msg(err, "variable `x` has different modes across alternatives")
}
test_verify_one_file! {
#[test] test_or_pattern_mode_inconsistent2 verus_code! {
enum Foo {
Bar(#[verifier::spec] u64, #[verifier::proof] u64),
}
proof fn blah(foo: Foo) {
#[verifier::proof] let (Foo::Bar(x, y) | Foo::Bar(y, x)) = foo;
}
} => Err(err) => assert_vir_error_msg(err, "variable `x` has different modes across alternatives")
}
test_verify_one_file! {
#[test] test_struct_pattern_fields_out_of_order_fail_issue_348 verus_code! {
tracked struct Foo {
ghost a: u64,
tracked b: u64,
}
proof fn some_call(#[verifier::proof] y: u64) { }
proof fn t() {
let tracked foo = Foo { a: 5, b: 6 };
let tracked Foo { b, a } = foo;
// Variable 'a' has the mode of field 'a' (that is, spec)
// some_call requires 'proof'
// So this should fail
some_call(a);
}
} => Err(err) => assert_vir_error_msg(err, "expression has mode spec, expected mode proof")
}
test_verify_one_file! {
#[test] test_struct_pattern_fields_out_of_order_success_issue_348 verus_code! {
struct X { }
struct Foo {
#[verifier::spec] a: u64,
#[verifier::proof] b: X,
}
proof fn some_call(#[verifier::proof] y: X) { }
proof fn t(#[verifier::proof] x: X) {
#[verifier::proof] let foo = Foo { a: 5, b: x };
#[verifier::proof] let Foo { b, a } = foo;
// This should succeed, 'b' has mode 'proof'
some_call(b);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_struct_pattern_fields_numeric_out_of_order_fail verus_code! {
tracked struct Foo(ghost u64, tracked u64);
proof fn some_call(tracked y: u64) { }
proof fn t() {