-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathexternal_traits.rs
451 lines (389 loc) · 11.5 KB
/
external_traits.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
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
test_verify_one_file! {
#[test] test_trait verus_code! {
trait Tr {
fn foo();
fn bar();
}
struct X { }
#[verifier::external]
impl Tr for X {
fn foo() { }
fn bar() { }
}
#[verifier::external_fn_specification]
fn ex_foo() {
X::foo()
}
} => Err(err) => assert_vir_error_msg(err, "using assume_specification for this function requires you to specify all other functions for the same trait impl, but the method `bar` is missing")
}
test_verify_one_file! {
#[test] test_trait_dupe verus_code! {
trait Tr {
fn foo();
fn bar();
}
struct X { }
#[verifier::external]
impl Tr for X {
fn foo() { }
fn bar() { }
}
#[verifier::external_fn_specification]
fn ex_foo() {
X::foo()
}
#[verifier::external_fn_specification]
fn ex_foo2() {
X::foo()
}
#[verifier::external_fn_specification]
fn ex_bar() {
X::bar()
}
} => Err(err) => assert_vir_error_msg(err, "duplicate assume_specification for this method")
}
test_verify_one_file! {
#[test] test_trait_dupe2 verus_code! {
trait Tr {
fn foo();
fn bar();
}
struct X { }
impl Tr for X {
fn foo() { }
fn bar() { }
}
#[verifier::external_fn_specification]
fn ex_foo() {
X::foo()
}
#[verifier::external_fn_specification]
fn ex_bar() {
X::bar()
}
} => Err(err) => assert_vir_error_msg(err, "duplicate specification for this trait implementation")
}
test_verify_one_file! {
#[test] test_trait_ok verus_code! {
trait Tr {
fn foo();
fn bar();
}
struct X { }
#[verifier::external]
impl Tr for X {
fn foo() { }
fn bar() { }
}
#[verifier::external_fn_specification]
fn ex_foo() {
X::foo()
}
spec fn llama() -> bool;
#[verifier::external_fn_specification]
fn ex_bar()
ensures llama(),
{
X::bar()
}
fn test() {
X::bar();
assert(llama());
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_trait_not_ext verus_code! {
trait T {
}
#[verifier::external_trait_specification]
trait Ex {
type ExternalTraitSpecificationFor: T;
}
} => Err(err) => assert_vir_error_msg(err, "duplicate specification")
}
test_verify_one_file! {
#[test] test_trait_dup verus_code! {
#[verifier::external]
trait T {
fn f();
}
#[verifier::external_trait_specification]
trait Ex1 {
type ExternalTraitSpecificationFor: T;
fn f();
}
#[verifier::external_trait_specification]
trait Ex2 {
type ExternalTraitSpecificationFor: T;
fn f();
}
} => Err(err) => assert_vir_error_msg(err, "duplicate method")
}
test_verify_one_file! {
#[test] test_trait_body verus_code! {
#[verifier::external]
trait T {
fn f() {}
}
#[verifier::external_trait_specification]
trait Ex {
type ExternalTraitSpecificationFor: T;
fn f() {}
}
} => Err(err) => assert_vir_error_msg(err, "`external_trait_specification` functions cannot have bodies")
}
test_verify_one_file! {
#[test] test_trait_no_assoc verus_code! {
#[verifier::external]
trait T {
fn f() {}
}
#[verifier::external_trait_specification]
trait Ex {
fn f() {}
}
} => Err(err) => assert_vir_error_msg(err, "trait must declare a type ExternalTraitSpecificationFor")
}
test_verify_one_file! {
#[test] test_trait_different_generics1 verus_code! {
#[verifier::external]
trait T<A, B> {
}
#[verifier::external_trait_specification]
trait Ex<A, B> {
type ExternalTraitSpecificationFor: T<B, A>;
}
} => Err(err) => assert_vir_error_msg(err, "expected generics to match")
}
test_verify_one_file! {
#[test] test_trait_different_generics2 verus_code! {
#[verifier::external]
trait T<A> {
}
#[verifier::external_trait_specification]
trait Ex<A: Copy> {
type ExternalTraitSpecificationFor: T<A>;
}
} => Err(err) => assert_vir_error_msg(err, "external_trait_specification trait bound mismatch")
}
test_verify_one_file! {
#[test] test_trait1 verus_code! {
#[verifier::external]
trait T {
fn f(&self, q: &Self, b: bool) -> usize;
type X;
}
#[verifier::external_trait_specification]
trait Ex {
type ExternalTraitSpecificationFor: T;
fn f(&self, q: &Self, b: bool) -> (r: usize)
requires b
ensures r > 7
;
type X;
}
fn test<A: T>(a: &A) {
let i = a.f(a, true);
assert(i > 7);
let i = a.f(a, false); // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_trait2 verus_code! {
#[verifier::external]
trait T {
fn f(&self, q: &Self, b: bool) -> usize;
type X;
}
#[verifier::external_trait_specification(ExternalTraitSpecificationForAlt)]
trait Ex {
type ExternalTraitSpecificationForAlt: T;
fn f(&self, q: &Self, b: bool) -> (r: usize)
requires b
ensures r > 7
;
type X;
}
fn test<A: T>(a: &A) {
let i = a.f(a, true);
assert(i > 7);
let i = a.f(a, false); // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_trait3 verus_code! {
#[verifier::external]
trait T {
fn f(&self, q: &Self, b: bool) -> usize;
type X;
}
#[verifier::external_trait_specification]
trait Ex {
type ExternalTraitSpecificationFor: T;
fn f(&self, q: &Self, b: bool) -> (r: usize)
requires b
ensures r > 7 // TRAIT
;
type X;
}
impl T for u8 {
fn f(&self, q: &Self, b: bool) -> (r: usize) {
assert(b);
8
}
type X = u16;
}
impl T for u32 {
fn f(&self, q: &Self, b: bool) -> (r: usize) {
assert(b);
6 // FAILS
}
type X = u16;
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file_with_options! {
#[test] test_trait4 ["--disable-internal-test-mode"] => verus_code! {
#[verifier::external_trait_specification]
pub trait ExIntoIterator {
type ExternalTraitSpecificationFor: core::iter::IntoIterator;
}
#[verifier::external_trait_specification]
pub trait ExPartialEq<Rhs: ?Sized> {
type ExternalTraitSpecificationFor: core::cmp::PartialEq<Rhs>;
}
#[verifier::external_trait_specification]
pub trait ExEq: PartialEq {
type ExternalTraitSpecificationFor: core::cmp::Eq;
}
#[verifier::external_trait_specification]
pub trait ExPartialOrd<Rhs: ?Sized>: PartialEq<Rhs> {
type ExternalTraitSpecificationFor: core::cmp::PartialOrd<Rhs>;
}
#[verifier::external_trait_specification]
pub trait ExOrd: Eq + PartialOrd {
type ExternalTraitSpecificationFor: Ord;
}
#[verifier::external_type_specification]
pub struct ExOrdering(core::cmp::Ordering);
#[verifier::external_type_specification]
#[verifier::external_body]
#[verifier::reject_recursive_types_in_ground_variants(I)]
pub struct ExPeekable<I: Iterator>(core::iter::Peekable<I>);
#[verifier::external_trait_specification]
pub trait ExIterator1 {
type ExternalTraitSpecificationFor: core::iter::Iterator;
type Item;
fn count(self) -> usize where Self: Sized;
fn cmp<I>(self, other: I) -> core::cmp::Ordering where Self: core::iter::Iterator, I: core::iter::IntoIterator<Item = <Self as core::iter::Iterator>::Item>, <Self as core::iter::Iterator>::Item: Ord, Self: Sized;
}
#[verifier::external_trait_specification]
pub trait ExIterator2 {
type ExternalTraitSpecificationFor: core::iter::Iterator;
type Item;
fn peekable(self) -> core::iter::Peekable<Self> where Self: Sized, Self: core::iter::Iterator requires false;
}
fn test2<A: Iterator>(a: A) {
let x = a.count();
}
fn test3<A: Iterator>(a: A) {
let y = a.peekable(); // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_trait5 verus_code! {
#[verifier::external]
trait T {
type X;
fn f() -> Self::X;
}
#[verifier::external_trait_specification]
trait ExT {
type ExternalTraitSpecificationFor: T;
type X;
fn f() -> Self::X;
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_trait_auto_import verus_code! {
#[verifier::external]
trait T {}
#[verifier::external]
impl T for bool {}
#[verifier::external_trait_specification]
trait ExT {
type ExternalTraitSpecificationFor: T;
}
trait U {
type X: T;
}
impl U for u8 {
type X = S;
}
impl U for u16 {
type X = [S; 3];
}
struct S;
#[verifier::external]
impl T for S where bool: T {}
#[verifier::external]
impl<A: T, const N: usize> T for [A; N] {
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_trait_defaults verus_code! {
#[verifier::external]
trait T {
fn d(u: u32) -> u32 { u }
fn f(u: u32) -> u32;
}
#[verifier::external]
impl T for bool {
fn f(u: u32) -> u32 { u }
}
#[verifier::external_trait_specification]
trait ExT {
type ExternalTraitSpecificationFor: T;
fn d(u: u32) -> (r: u32) requires u >= 100;
fn f(u: u32) -> (r: u32) requires u >= 100;
}
impl T for u8 {
fn f(u: u32) -> u32 { u }
}
fn test() {
<bool as T>::d(100);
<u8 as T>::d(99); // FAILS
}
} => Err(e) => assert_one_fails(e)
}
test_verify_one_file! {
#[test] test_trait_default_external_body_issue1307 verus_code! {
#[verifier::external]
fn some_external_fn() { }
trait T {
#[verifier(external_body)]
fn f1() -> (ret: bool)
ensures
!ret
{
some_external_fn();
false
}
fn f2() -> bool {
Self::f1()
}
}
struct S;
impl T for S { }
} => Ok(())
}