]>
Commit | Line | Data |
---|---|---|
1 | // Reject mixing cyclic structure and Drop when using fixed length | |
2 | // arrays. | |
3 | // | |
4 | // (Compare against compile-fail/dropck_vec_cycle_checked.rs) | |
5 | ||
6 | ||
7 | ||
8 | use std::cell::Cell; | |
9 | use id::Id; | |
10 | ||
11 | mod s { | |
12 | use std::sync::atomic::{AtomicUsize, Ordering}; | |
13 | ||
14 | static S_COUNT: AtomicUsize = AtomicUsize::new(0); | |
15 | ||
16 | pub fn next_count() -> usize { | |
17 | S_COUNT.fetch_add(1, Ordering::SeqCst) + 1 | |
18 | } | |
19 | } | |
20 | ||
21 | mod id { | |
22 | use s; | |
23 | #[derive(Debug)] | |
24 | pub struct Id { | |
25 | orig_count: usize, | |
26 | count: usize, | |
27 | } | |
28 | ||
29 | impl Id { | |
30 | pub fn new() -> Id { | |
31 | let c = s::next_count(); | |
32 | println!("building Id {}", c); | |
33 | Id { orig_count: c, count: c } | |
34 | } | |
35 | pub fn count(&self) -> usize { | |
36 | println!("Id::count on {} returns {}", self.orig_count, self.count); | |
37 | self.count | |
38 | } | |
39 | } | |
40 | ||
41 | impl Drop for Id { | |
42 | fn drop(&mut self) { | |
43 | println!("dropping Id {}", self.count); | |
44 | self.count = 0; | |
45 | } | |
46 | } | |
47 | } | |
48 | ||
49 | trait HasId { | |
50 | fn count(&self) -> usize; | |
51 | } | |
52 | ||
53 | #[derive(Debug)] | |
54 | struct CheckId<T:HasId> { | |
55 | v: T | |
56 | } | |
57 | ||
58 | #[allow(non_snake_case)] | |
59 | fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } } | |
60 | ||
61 | impl<T:HasId> Drop for CheckId<T> { | |
62 | fn drop(&mut self) { | |
63 | assert!(self.v.count() > 0); | |
64 | } | |
65 | } | |
66 | ||
67 | #[derive(Debug)] | |
68 | struct B<'a> { | |
69 | id: Id, | |
70 | a: [CheckId<Cell<Option<&'a B<'a>>>>; 2] | |
71 | } | |
72 | ||
73 | impl<'a> HasId for Cell<Option<&'a B<'a>>> { | |
74 | fn count(&self) -> usize { | |
75 | match self.get() { | |
76 | None => 1, | |
77 | Some(b) => b.id.count(), | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | impl<'a> B<'a> { | |
83 | fn new() -> B<'a> { | |
84 | B { id: Id::new(), a: [CheckId(Cell::new(None)), CheckId(Cell::new(None))] } | |
85 | } | |
86 | } | |
87 | ||
88 | fn f() { | |
89 | let (b1, b2, b3); | |
90 | b1 = B::new(); | |
91 | b2 = B::new(); | |
92 | b3 = B::new(); | |
93 | b1.a[0].v.set(Some(&b2)); | |
94 | //~^ ERROR `b2` does not live long enough | |
95 | b1.a[1].v.set(Some(&b3)); | |
96 | //~^ ERROR `b3` does not live long enough | |
97 | b2.a[0].v.set(Some(&b2)); | |
98 | b2.a[1].v.set(Some(&b3)); | |
99 | b3.a[0].v.set(Some(&b1)); | |
100 | //~^ ERROR `b1` does not live long enough | |
101 | b3.a[1].v.set(Some(&b2)); | |
102 | } | |
103 | ||
104 | fn main() { | |
105 | f(); | |
106 | } |