-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathboundary_suggestions.rs
More file actions
438 lines (404 loc) · 13.9 KB
/
Copy pathboundary_suggestions.rs
File metadata and controls
438 lines (404 loc) · 13.9 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
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
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
// Tests for assume_specification
test_verify_one_file! {
#[test] test_two_assume_specification_suggestions_made code! {
use vstd::prelude::*;
pub fn foo() {}
pub fn bar() {}
verus! {
pub fn main() {
foo();
bar();
}
}
} => Err(err) => assert_vir_error_msgs(err, &["foo", "bar"])
}
test_verify_one_file! {
#[test] test_assume_specification_simple_suggestion_made code! {
use vstd::prelude::*;
pub fn foo() {}
verus! {
pub fn main() {
foo();
}
}
} => Err(err) => assert_help_error_msg(err, "pub assume_specification [crate::foo] ();")
}
test_verify_one_file! {
#[test] test_assume_specification_simple_suggestion_correct code! {
use vstd::prelude::*;
pub fn foo() {}
verus! {
pub assume_specification [crate::foo] ();
pub fn main() {
foo();
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_assume_specification_generics_suggestion_made code! {
use vstd::prelude::*;
pub fn foo<X, Y: Eq, Z>(x: X, y: Y) -> Z
where Z: PartialEq,
{ panic!() }
verus! {
pub fn bar<X, Y: Eq, Z: PartialEq>(x: X, y: Y) {
let z: Z = foo(x, y);
}
}
} => Err(err) => assert_help_error_msg(err, "pub assume_specification<X, Y, Z> [crate::foo] (_0: X, _1: Y) -> Z
where
Y: std::cmp::Eq,
Z: std::cmp::PartialEq,;")
}
test_verify_one_file! {
#[test] test_assume_specification_generics_suggestion_correct code! {
use vstd::prelude::*;
pub fn foo<X, Y: Eq, Z>(x: X, y: Y) -> Z
where Z: PartialEq,
{ panic!() }
verus! {
pub assume_specification<X, Y, Z> [crate::foo] (_0: X, _1: Y) -> Z
where
Y: std::cmp::Eq,
Z: std::cmp::PartialEq,;
pub fn bar<X, Y: Eq, Z: PartialEq>(x: X, y: Y) {
let z: Z = foo(x, y);
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_assume_specification_self_suggestion_made code! {
use vstd::prelude::*;
verus! {
pub struct A { x: usize }
}
impl A {
pub fn foo(&self) -> A {
A { x: self.x + 1 }
}
}
verus! {
pub fn bar(a: A) {
let a2 = a.foo();
}
}
} => Err(err) => assert_help_error_msg(err, "pub assume_specification [crate::A::foo] (_0: &crate::A) -> crate::A;")
}
test_verify_one_file! {
#[test] test_assume_specification_self_suggestion_correct code! {
use vstd::prelude::*;
verus! {
pub struct A { x: usize }
}
impl A {
pub fn foo(&self) -> A {
A { x: self.x + 1 }
}
}
verus! {
pub assume_specification [crate::A::foo] (_0: &crate::A) -> crate::A;
pub fn bar(a: A) {
let a2 = a.foo();
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_assume_specification_foreign_suggestion_made code! {
use vstd::prelude::*;
verus! {
pub fn bar<T>(o: Option<T>)-> Option<T> {
o.inspect(|_x: &T| {})
}
}
} => Err(err) => assert_help_error_msg(err, "pub assume_specification<T, F> [std::option::Option::<T>::inspect] (_0: std::option::Option<T>, _1: F) -> std::option::Option<T>")
}
test_verify_one_file! {
#[test] test_assume_specification_foreign_suggestion_correct code! {
use vstd::prelude::*;
verus! {
#[verifier::external_trait_specification]
pub trait ExDestruct : core::marker::PointeeSized {
type ExternalTraitSpecificationFor: core::marker::Destruct;
}
pub assume_specification<T, F> [std::option::Option::<T>::inspect] (_0: std::option::Option<T>, _1: F) -> std::option::Option<T>
where
F: std::ops::FnOnce(&T,) -> () + core::marker::Destruct,;
pub fn bar<T>(o: Option<T>)-> Option<T> {
o.inspect(|_x: &T| {})
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_assume_specification_const_generics_suggestion_made code! {
use vstd::prelude::*;
fn foo<A, const N: usize, const M: usize>(inputs: &[A; N]) -> [A; M] {
panic!()
}
verus! {
pub fn bar<A>(inputs: &[A; 1]) {
let a = foo::<A, 1, 2>(inputs);
}
}
} => Err(err) => assert_help_error_msg(err, "assume_specification<A, const N: usize, const M: usize> [crate::foo] (_0: &[A; N]) -> [A; M];")
}
test_verify_one_file! {
#[test] test_assume_specification_const_generics_suggestion_correct code! {
use vstd::prelude::*;
fn foo<A, const N: usize, const M: usize>(inputs: &[A; N]) -> [A; M] {
panic!()
}
verus! {
assume_specification<A, const N: usize, const M: usize> [crate::foo] (_0: &[A; N]) -> [A; M];
pub fn bar<A>(inputs: &[A; 1]) {
let a = foo::<A, 1, 2>(inputs);
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] test_assume_specification_region_outlives_suggestion_made code! {
fn foo<'a, 'b, 'c, A, B>(a: &'a A, b: &'b B) -> &'c A
where 'c: 'a + 'b {
panic!()
}
verus! {
pub fn bar<'a, 'b, 'c, A, B>(a: &'a A, b: &'b B) -> &'c A {
foo(a, b)
}
}
} => Err(e) => assert_help_error_msg(e, "assume_specification<'a, 'b, 'c, A, B> [crate::foo] (_0: &'a A, _1: &'b B) -> &'c A
where
'c: 'a + 'b,;")
}
test_verify_one_file! {
#[test] test_assume_specification_region_outlives_correct code! {
fn foo<'a, 'b, 'c, A, B>(a: &'a A, b: &'b B) -> &'c A
where 'c: 'a + 'b {
panic!()
}
verus! {
assume_specification<'a, 'b, 'c, A, B> [crate::foo] (_0: &'a A, _1: &'b B) -> &'c A
where
'c: 'a + 'b,;
pub fn bar<'a, 'b, 'c, A, B>(a: &'a A, b: &'b B) -> &'c A {
foo(a, b)
}
}
} => Ok(())
}
// The impl header has an anonymous early-bound lifetime (`S<'_>`) that the
// method inherits, and which also appears in the method's `Self: Bound`
// where-clause. The RegionRenamer must rename that anonymous lifetime
// consistently in *both* the fn signature and the where-clause predicates.
// If the predicates are not folded, the fn signature reads `&crate::S<'a>`
// but the where-clause reads `crate::S<'_>: crate::Bound`, producing an
// inconsistent (uncompilable) suggestion.
test_verify_one_file! {
#[test] test_assume_specification_anon_early_bound_in_where_suggestion_made code! {
use vstd::prelude::*;
pub trait Bound { }
pub struct S<'a> { pub x: &'a u8 }
impl Bound for S<'_> { }
impl S<'_> {
pub fn go(&self) where Self: Bound { }
}
verus! {
pub fn bar<'a>(s: S<'a>) {
s.go()
}
}
} => Err(e) => assert_help_error_msg(e, "(_0: &crate::S<'a>)
where
crate::S<'a>: crate::Bound,;")
}
// Tests for external_type_specification
test_verify_one_file! {
#[test] error_msg_two_external_types verus_code! {
#[verifier(external)]
struct X { }
#[verifier(external)]
struct Y { }
#[verifier::external_body]
fn stuff(y: Y) -> X {
panic!()
}
} => Err(err) => assert_help_error_msgs(err, &["cannot use type `test_crate::X` which is ignored", "cannot use type `test_crate::Y` which is ignored"])
}
// These two together form a 'smoke test' that the suggestions provided are correct.
// A more elegant strategy might be to extract the suggestion from the error message and
// put that directly into the code! macro.
test_verify_one_file! {
#[test] two_external_type_suggestions code! {
struct X { }
pub struct Y { }
verus!{
#[verifier::external_body]
fn stuff(y: Y) -> X {
X {}
}
}
} => Err(err) => assert_help_error_msgs(err, &["#[verifier::external_type_specification]
struct ExX(crate::X);", "#[verifier::external_type_specification]
pub struct ExY(crate::Y);"])
}
test_verify_one_file! {
#[test] two_external_type_suggestions_are_correct code! {
struct X { }
pub struct Y { }
verus!{
#[verifier::external_type_specification]
struct ExX(crate::X);
#[verifier::external_type_specification]
pub struct ExY(crate::Y);
fn stuff(y: Y) -> X {
X {}
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] external_type_generics_suggestion_made code! {
// Using view here because Add, etc caused lifetime errors and ill-typed AIR
use vstd::prelude::*;
verus! {
trait T { }
}
struct X<'a, 'b, A: View + T ,B,C: View<V = A>, const N: usize>
where B: View<V = C> + T{
a: A,
b: &'b B,
c: C,
l1: &'a std::marker::PhantomData<A>,
}
verus! {
fn stuff<'a, 'b, A: View + T, B, C: View<V = A>, const N: usize>(x: X<'a, 'b, A, B, C, N>) -> X<'a, 'b, A, B, C, N>
where B: View<V = C> + T{
x
}
}
} => Err(err) => assert_help_error_msg(err,
"#[verifier::reject_recursive_types(A)]
#[verifier::reject_recursive_types(B)]
#[verifier::reject_recursive_types(C)]
#[verifier::external_type_specification]
struct ExX<'a, 'b, A, B, C, const N: usize>(crate::X<'a, 'b, A, B, C, N>)
where
A: vstd::string::View + crate::T,
B: vstd::string::View<V = C> + crate::T,
C: vstd::string::View<V = A>,;")
}
// TODO: Test generic params not in lexographical order
test_verify_one_file! {
#[test] external_type_generics_suggestion_correct code! {
// Using view here because Add, etc caused lifetime errors and ill-typed AIR
use vstd::prelude::*;
verus! {
trait T { }
}
struct X<'a, 'b, A: View + T , B, C: View<V = A>, const N: usize>
where B: View<V = C> + T{
a: A,
b: &'b B,
c: C,
l1: &'a std::marker::PhantomData<A>,
}
verus! {
#[verifier::reject_recursive_types(A)]
#[verifier::reject_recursive_types(B)]
#[verifier::reject_recursive_types(C)]
#[verifier::external_type_specification]
struct ExX<'a, 'b, A, B, C, const N: usize>(crate::X<'a, 'b, A, B, C, N>)
where
A: vstd::string::View + crate::T,
B: vstd::string::View<V = C> + crate::T,
C: vstd::string::View<V = A>,;
fn stuff<'a, 'b, A: View + T, B, C: View<V = A>, const N: usize>(x: X<'a, 'b, A, B, C, N>) -> X<'a, 'b, A, B, C, N>
where B: View<V = C> + T{
x
}
}
} => Ok(())
}
test_verify_one_file! {
#[test] external_type_assoc_type_suggestion_made code! {
use vstd::prelude::*;
verus! {
trait T {
type AssocT;
}
}
struct X<'a, 'b, A: View + T ,B,C: View<V = A>, const N: usize>
where B: View<V = C> + T,
A::AssocT: View<V = A>,
B::AssocT: 'b,
{
a: A,
b: B,
c: &'b C,
l1: &'a std::marker::PhantomData<A>,
}
verus! {
fn stuff<'a, 'b, A: View + T, B, C: View<V = A>, const N: usize>(x: X<'a, 'b, A, B, C, N>) -> X<'a, 'b, A, B, C, N>
where B: View<V = C> + T,
B::AssocT: 'b,
A::AssocT: View<V = A>{
x
}
}
} => Err(e) => assert_help_error_msg(e, "#[verifier::reject_recursive_types(A)]
#[verifier::reject_recursive_types(B)]
#[verifier::reject_recursive_types(C)]
#[verifier::external_type_specification]
struct ExX<'a, 'b, A, B, C, const N: usize>(crate::X<'a, 'b, A, B, C, N>)
where
<A as T>::AssocT: vstd::string::View<V = A>,
A: vstd::string::View + crate::T,
B: vstd::string::View<V = C> + crate::T,
C: vstd::string::View<V = A>,;")
}
test_verify_one_file! {
#[test] external_type_assoc_type_suggestion_correct code! {
use vstd::prelude::*;
verus! {
trait T {
type AssocT;
}
}
struct X<'a, 'b, A: View + T ,B,C: View<V = A>, const N: usize>
where B: View<V = C> + T,
A::AssocT: View<V = A>,
B::AssocT: 'b,
{
a: A,
b: B,
c: &'b C,
l1: &'a std::marker::PhantomData<A>,
}
verus! {
#[verifier::reject_recursive_types(A)]
#[verifier::reject_recursive_types(B)]
#[verifier::reject_recursive_types(C)]
#[verifier::external_type_specification]
struct ExX<'a, 'b, A, B, C, const N: usize>(crate::X<'a, 'b, A, B, C, N>)
where
<A as T>::AssocT: vstd::string::View<V = A>,
A: vstd::string::View + crate::T,
B: vstd::string::View<V = C> + crate::T,
C: vstd::string::View<V = A>,;
fn stuff<'a, 'b, A: View + T, B, C: View<V = A>, const N: usize>(x: X<'a, 'b, A, B, C, N>) -> X<'a, 'b, A, B, C, N>
where B: View<V = C> + T,
B::AssocT: 'b,
A::AssocT: View<V = A>{
x
}
}
} => Ok(())
}