Skip to content

Commit 0c08a03

Browse files
committed
add: tests for annotations in traits
1 parent 51084c6 commit 0c08a03

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

tests/ui/fail/annot_preds_trait.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//@error-in-other-file: Unsat
2+
//@compile-flags: -Adead_code -C debug-assertions=off
3+
4+
// A is represented as Tuple<Int> in SMT-LIB2 format.
5+
struct A {
6+
x: i64,
7+
}
8+
9+
trait Double {
10+
// Support annotations in trait definitions
11+
#[thrust::predicate]
12+
fn is_double(self, doubled: Self) -> bool;
13+
14+
// This annotations are applied to all implementors of the `Double` trait.
15+
#[thrust::requires(true)]
16+
#[thrust::ensures(is_double(*self, ^self))]
17+
fn double(&mut self);
18+
}
19+
20+
impl Double for A {
21+
// Write concrete definitions for predicates in `impl` blocks
22+
#[thrust::predicate]
23+
fn is_double(self, doubled: Self) -> bool {
24+
// (tuple_proj<Int>.0 self) is equivalent to self.x
25+
// self.x * 3 == doubled.x (this isn't actually doubled!) is written as following:
26+
"(=
27+
(* (tuple_proj<Int>.0 self) 3)
28+
(tuple_proj<Int>.0 doubled)
29+
)"; true // This definition does not comply with annotations in trait!
30+
}
31+
32+
// Check if this method complies with annotations in
33+
// trait definition.
34+
fn double(&mut self) {
35+
self.x += self.x;
36+
}
37+
}
38+
39+
fn main() {
40+
let mut a = A { x: 3 };
41+
a.double();
42+
assert!(a.x == 6);
43+
}

tests/ui/pass/annot_preds_trait.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//@check-pass
2+
//@compile-flags: -Adead_code -C debug-assertions=off
3+
4+
// A is represented as Tuple<Int> in SMT-LIB2 format.
5+
struct A {
6+
x: i64,
7+
}
8+
9+
trait Double {
10+
// Support annotations in trait definitions
11+
#[thrust::predicate]
12+
fn is_double(self, doubled: Self) -> bool;
13+
14+
// This annotations are applied to all implementors of the `Double` trait.
15+
#[thrust::requires(true)]
16+
#[thrust::ensures(is_double(*self, ^self))]
17+
fn double(&mut self);
18+
}
19+
20+
impl Double for A {
21+
// Write concrete definitions for predicates in `impl` blocks
22+
#[thrust::predicate]
23+
fn is_double(self, doubled: Self) -> bool {
24+
// (tuple_proj<Int>.0 self) is equivalent to self.x
25+
// self.x * 2 == doubled.x is written as following:
26+
"(=
27+
(* (tuple_proj<Int>.0 self) 2)
28+
(tuple_proj<Int>.0 doubled)
29+
)"; true
30+
}
31+
32+
// Check if this method complies with annotations in
33+
// trait definition.
34+
fn double(&mut self) {
35+
self.x += self.x;
36+
}
37+
}
38+
39+
fn main() {
40+
let mut a = A { x: 3 };
41+
a.double();
42+
assert!(a.x == 6);
43+
}

0 commit comments

Comments
 (0)