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.
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.
11 // Reject mixing cyclic structure and Drop when using trait
12 // objects to hide the cross-references.
14 // (Compare against compile-fail/dropck_vec_cycle_checked.rs)
22 use std::sync::atomic::{AtomicUsize, Ordering};
24 static S_COUNT: AtomicUsize = AtomicUsize::new(0);
26 pub fn next_count() -> usize {
27 S_COUNT.fetch_add(1, Ordering::SeqCst) + 1
41 let c = s::next_count();
42 println!("building Id {}", c);
43 Id { orig_count: c, count: c }
45 pub fn count(&self) -> usize {
46 println!("Id::count on {} returns {}", self.orig_count, self.count);
53 println!("dropping Id {}", self.count);
60 fn count(&self) -> usize;
64 struct CheckId<T:HasId> {
68 #[allow(non_snake_case)]
69 fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } }
71 impl<T:HasId> Drop for CheckId<T> {
73 assert!(self.v.count() > 0);
77 trait Obj<'a> : HasId {
78 fn set0(&self, b: &'a Box<Obj<'a>>);
79 fn set1(&self, b: &'a Box<Obj<'a>>);
84 obj0: CheckId<Cell<Option<&'a Box<Obj<'a>>>>>,
85 obj1: CheckId<Cell<Option<&'a Box<Obj<'a>>>>>,
88 impl<'a> HasId for O<'a> {
89 fn count(&self) -> usize { self.id.count() }
93 fn new() -> Box<O<'a>> {
96 obj0: CheckId(Cell::new(None)),
97 obj1: CheckId(Cell::new(None)),
102 impl<'a> HasId for Cell<Option<&'a Box<Obj<'a>>>> {
103 fn count(&self) -> usize {
106 Some(c) => c.count(),
111 impl<'a> Obj<'a> for O<'a> {
112 fn set0(&self, b: &'a Box<Obj<'a>>) {
113 self.obj0.v.set(Some(b))
115 fn set1(&self, b: &'a Box<Obj<'a>>) {
116 self.obj1.v.set(Some(b))
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