]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/dropck_trait_cycle_checked.rs
rustdoc: pretty-print Unevaluated expressions in types.
[rust.git] / src / test / compile-fail / dropck_trait_cycle_checked.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Reject mixing cyclic structure and Drop when using trait
12 // objects to hide the cross-references.
13 //
14 // (Compare against compile-fail/dropck_vec_cycle_checked.rs)
15
16 #![feature(const_fn)]
17
18 use std::cell::Cell;
19 use id::Id;
20
21 mod s {
22     use std::sync::atomic::{AtomicUsize, Ordering};
23
24     static S_COUNT: AtomicUsize = AtomicUsize::new(0);
25
26     pub fn next_count() -> usize {
27         S_COUNT.fetch_add(1, Ordering::SeqCst) + 1
28     }
29 }
30
31 mod id {
32     use s;
33     #[derive(Debug)]
34     pub struct Id {
35         orig_count: usize,
36         count: usize,
37     }
38
39     impl Id {
40         pub fn new() -> Id {
41             let c = s::next_count();
42             println!("building Id {}", c);
43             Id { orig_count: c, count: c }
44         }
45         pub fn count(&self) -> usize {
46             println!("Id::count on {} returns {}", self.orig_count, self.count);
47             self.count
48         }
49     }
50
51     impl Drop for Id {
52         fn drop(&mut self) {
53             println!("dropping Id {}", self.count);
54             self.count = 0;
55         }
56     }
57 }
58
59 trait HasId {
60     fn count(&self) -> usize;
61 }
62
63 #[derive(Debug)]
64 struct CheckId<T:HasId> {
65     v: T
66 }
67
68 #[allow(non_snake_case)]
69 fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } }
70
71 impl<T:HasId> Drop for CheckId<T> {
72     fn drop(&mut self) {
73         assert!(self.v.count() > 0);
74     }
75 }
76
77 trait Obj<'a> : HasId {
78     fn set0(&self, b: &'a Box<Obj<'a>>);
79     fn set1(&self, b: &'a Box<Obj<'a>>);
80 }
81
82 struct O<'a> {
83     id: Id,
84     obj0: CheckId<Cell<Option<&'a Box<Obj<'a>>>>>,
85     obj1: CheckId<Cell<Option<&'a Box<Obj<'a>>>>>,
86 }
87
88 impl<'a> HasId for O<'a> {
89     fn count(&self) -> usize { self.id.count() }
90 }
91
92 impl<'a> O<'a> {
93     fn new() -> Box<O<'a>> {
94         Box::new(O {
95             id: Id::new(),
96             obj0: CheckId(Cell::new(None)),
97             obj1: CheckId(Cell::new(None)),
98         })
99     }
100 }
101
102 impl<'a> HasId for Cell<Option<&'a Box<Obj<'a>>>> {
103     fn count(&self) -> usize {
104         match self.get() {
105             None => 1,
106             Some(c) => c.count(),
107         }
108     }
109 }
110
111 impl<'a> Obj<'a> for O<'a> {
112     fn set0(&self, b: &'a Box<Obj<'a>>) {
113         self.obj0.v.set(Some(b))
114     }
115     fn set1(&self, b: &'a Box<Obj<'a>>) {
116         self.obj1.v.set(Some(b))
117     }
118 }
119
120
121 fn f() {
122     let (o1, o2, o3): (Box<Obj>, Box<Obj>, Box<Obj>) = (O::new(), O::new(), O::new());
123     o1.set0(&o2); //~ ERROR `o2` does not live long enough
124     o1.set1(&o3); //~ ERROR `o3` does not live long enough
125     o2.set0(&o2); //~ ERROR `o2` does not live long enough
126     o2.set1(&o3); //~ ERROR `o3` does not live long enough
127     o3.set0(&o1); //~ ERROR `o1` does not live long enough
128     o3.set1(&o2); //~ ERROR `o2` does not live long enough
129 }
130
131 fn main() {
132     f();
133 }