-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathimpl.rs
330 lines (282 loc) · 8.03 KB
/
impl.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
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
const STRUCT: &str = verus_code_str! {
#[derive(PartialEq, Eq)]
struct Bike {
hard_tail: bool,
}
};
test_verify_one_file! {
#[test] test_impl_1 STRUCT.to_string() + verus_code_str! {
impl Bike {
pub closed spec fn is_hard_tail(&self) -> bool {
self.hard_tail
}
}
fn test_impl_1(b: Bike)
requires b.is_hard_tail()
{
assert(b.hard_tail);
}
fn test_impl_2(b: &Bike)
requires b.is_hard_tail()
{
assert(b.hard_tail);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_impl_no_self STRUCT.to_string() + verus_code_str! {
impl Bike {
fn new() -> Bike {
ensures(|result: Bike| result.hard_tail);
Bike { hard_tail: true }
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_impl_no_self_fail_pub_private STRUCT.to_string() + verus_code_str! {
impl Bike {
pub fn new() -> Bike {
ensures(|result: Bike| result.hard_tail);
Bike { hard_tail: true }
}
}
} => Err(err) => assert_vir_error_msg(err, "in 'ensures' clause of public function, cannot access any field of a datatype where one or more fields are private")
}
test_verify_one_file! {
#[test] test_impl_mod_1 verus_code! {
mod M1 {
use builtin::*;
#[derive(PartialEq, Eq)]
pub struct Bike {
pub hard_tail: bool,
}
impl Bike {
pub open spec fn is_hard_tail(&self) -> bool {
self.hard_tail
}
pub fn new() -> Bike {
ensures(|result: Bike| result.hard_tail);
Bike { hard_tail: true }
}
}
}
mod M2 {
use super::M1::Bike;
use builtin::*;
fn test_impl_1(b: Bike)
requires b.is_hard_tail()
{
assert(b.is_hard_tail());
}
fn test_impl_2() {
let b = Bike::new();
assert(b.is_hard_tail());
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_impl_mod_priv_field verus_code! {
mod M1 {
#[derive(PartialEq, Eq)]
pub struct Bike {
hard_tail: bool,
}
impl Bike {
pub closed spec fn is_hard_tail(&self) -> bool {
self.hard_tail
}
}
}
mod M2 {
use super::M1::Bike;
use builtin::*;
fn test_impl_1(b: Bike)
requires b.is_hard_tail()
{
assert(b.is_hard_tail());
}
}
} => Ok(())
}
const IMPL_GENERIC_SHARED: &str = verus_code_str! {
#[derive(PartialEq, Eq)]
struct Wrapper<A> {
v: A,
}
impl<A> Wrapper<A> {
pub closed spec fn take(self) -> A {
self.v
}
}
};
test_verify_one_file! {
#[test] test_impl_generic_pass IMPL_GENERIC_SHARED.to_string() + verus_code_str! {
proof fn test_impl_1(a: int) {
let w = Wrapper { v: a };
assert(w.take() == a);
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_impl_generic_fail IMPL_GENERIC_SHARED.to_string() + verus_code_str! {
proof fn test_impl_1(a: int) {
let w = Wrapper { v: a };
assert(w.take() != a); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test] test_impl_generic_param verus_code! {
#[derive(PartialEq, Eq, Structural)]
struct Two<A, B> {
a: A,
b: B,
}
#[derive(PartialEq, Eq)]
struct Wrapper<A> {
v: A,
}
impl<A> Wrapper<A> {
pub closed spec fn take<B>(self, b: B) -> Two<A, B> {
Two { a: self.v, b: b }
}
}
proof fn test_impl_1(a: int) {
let w = Wrapper { v: a };
assert(w.take(12i32) == Two { a: a, b: 12i32 });
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_impl_with_self verus_code! {
#[derive(PartialEq, Eq, Structural)]
struct Bike {
hard_tail: bool,
}
impl Bike {
spec fn id(self) -> Self {
self
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_illegal_trait_impl verus_code! {
#[derive(PartialEq, Eq)]
struct V {
one: nat,
}
impl std::ops::Index<int> for V {
type Output = nat;
open spec fn index(&self, idx: int) -> &nat {
if idx == 0 {
&self.one
} else {
vstd::pervasive::arbitrary()
}
}
}
} => Err(err) => assert_vir_error_msg(err, "function for external trait must have mode 'exec'")
}
test_verify_one_file! {
#[test] test_illegal_trait_impl_2 verus_code! {
struct V {
}
impl std::ops::Index<usize> for V {
type Output = bool;
fn index(&self, #[verifier::spec]idx: usize) -> &bool { &true }
}
} => Err(err) => assert_vir_error_msg(err, "function for external trait must have all parameters have mode 'exec'")
}
test_verify_one_file! {
#[test] test_generic_struct verus_code! {
#[derive(PartialEq, Eq)]
struct TemplateCar<V> {
four_doors: bool,
passengers: u64,
the_v: V,
}
impl<V> TemplateCar<V> {
fn template_new(v: V) -> (result: TemplateCar<V>)
ensures
equal(result.passengers, 205),
equal(result.the_v, v),
{
TemplateCar::<V> { four_doors: false, passengers: 205, the_v: v }
}
fn template_get_passengers(&self) -> (result: u64)
ensures result == self.passengers
{
self.passengers
}
fn template_get_v(self) -> (result: V)
ensures equal(result, self.the_v)
{
self.the_v
}
}
fn test1() {
let c3 = TemplateCar::<u64>::template_new(5);
let p3 = c3.template_get_passengers();
assert(p3 == 205);
let v = c3.template_get_v();
assert(v == 5);
}
} => Ok(())
}
test_verify_one_file! {
#[ignore] #[test] test_erased_assoc_type_param verus_code! {
struct Foo<V> {
v: V
}
impl<V> Foo<V> {
fn bar<F: Fn(V) -> bool>(f: Ghost<F>, v: Ghost<V>) -> Ghost<bool> {
Ghost(f.requires((v,)))
}
fn bar2<F: Fn(V) -> bool>(self, f: Ghost<F>) -> Ghost<bool> {
Ghost(f.requires((self.v,)))
}
}
fn test() {
let x: Ghost<u64> = Ghost(0u64);
let z = |y: u64| true;
Foo::<u64>::bar(Ghost(z), x);
let f = Foo::<u64> { v: 17 };
let w = |y: u64| true;
let b: Ghost<bool> = f.bar2(Ghost(w));
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_resolve_self_ty verus_code! {
struct Foo<V> {
x: V,
}
impl<V> Foo<V> {
fn bar(self) -> (s: Self)
ensures equal(self, s)
{
self
}
fn bar2(self) -> (s: Self)
ensures equal(self, s)
{
Self::bar(self)
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] unsafe_impl_fail verus_code! {
struct Foo {
x: u32,
}
unsafe impl Send for Foo { }
} => Err(e) => assert_vir_error_msg(e, "the verifier does not support `unsafe` here")
}