]> git.proxmox.com Git - rustc.git/blame - src/test/ui/span/dropck_arr_cycle_checked.rs
New upstream version 1.36.0+dfsg1
[rustc.git] / src / test / ui / span / dropck_arr_cycle_checked.rs
CommitLineData
85aaf69f
SL
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
ff7c6d11 6
62682a34 7
85aaf69f
SL
8use std::cell::Cell;
9use id::Id;
10
11mod s {
62682a34 12 use std::sync::atomic::{AtomicUsize, Ordering};
85aaf69f 13
62682a34 14 static S_COUNT: AtomicUsize = AtomicUsize::new(0);
85aaf69f
SL
15
16 pub fn next_count() -> usize {
17 S_COUNT.fetch_add(1, Ordering::SeqCst) + 1
18 }
19}
20
21mod 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
49trait HasId {
50 fn count(&self) -> usize;
51}
52
53#[derive(Debug)]
54struct CheckId<T:HasId> {
55 v: T
56}
57
58#[allow(non_snake_case)]
59fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } }
60
85aaf69f
SL
61impl<T:HasId> Drop for CheckId<T> {
62 fn drop(&mut self) {
63 assert!(self.v.count() > 0);
64 }
65}
66
67#[derive(Debug)]
68struct B<'a> {
69 id: Id,
70 a: [CheckId<Cell<Option<&'a B<'a>>>>; 2]
71}
72
73impl<'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
82impl<'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
88fn f() {
89 let (b1, b2, b3);
90 b1 = B::new();
91 b2 = B::new();
92 b3 = B::new();
c30ab7b3 93 b1.a[0].v.set(Some(&b2));
ff7c6d11 94 //~^ ERROR `b2` does not live long enough
c30ab7b3 95 b1.a[1].v.set(Some(&b3));
ff7c6d11 96 //~^ ERROR `b3` does not live long enough
c30ab7b3
SL
97 b2.a[0].v.set(Some(&b2));
98 b2.a[1].v.set(Some(&b3));
99 b3.a[0].v.set(Some(&b1));
ff7c6d11 100 //~^ ERROR `b1` does not live long enough
c30ab7b3 101 b3.a[1].v.set(Some(&b2));
85aaf69f
SL
102}
103
104fn main() {
105 f();
106}