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 TypedArena.
13 // (Compare against compile-fail/dropck_vec_cycle_checked.rs)
15 // (Also compare against compile-fail/dropck_tarena_unsound_drop.rs,
16 // which is a reduction of this code to more directly show the reason
17 // for the error message we see here.)
19 #![feature(rustc_private)]
23 use arena::TypedArena;
28 use std::sync::atomic::{AtomicUsize, Ordering};
30 static S_COUNT: AtomicUsize = AtomicUsize::new(0);
32 pub fn next_count() -> usize {
33 S_COUNT.fetch_add(1, Ordering::SeqCst) + 1
47 let c = s::next_count();
48 println!("building Id {}", c);
49 Id { orig_count: c, count: c }
51 pub fn count(&self) -> usize {
52 println!("Id::count on {} returns {}", self.orig_count, self.count);
59 println!("dropping Id {}", self.count);
66 fn count(&self) -> usize;
70 struct CheckId<T:HasId> {
74 #[allow(non_snake_case)]
75 fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } }
77 impl<T:HasId> Drop for CheckId<T> {
79 assert!(self.v.count() > 0);
86 v: Vec<CheckId<Cell<Option<&'a C<'a>>>>>,
89 impl<'a> HasId for Cell<Option<&'a C<'a>>> {
90 fn count(&self) -> usize {
93 Some(c) => c.id.count(),
100 C { id: Id::new(), v: Vec::new() }
104 fn f<'a>(arena: &'a TypedArena<C<'a>>) {
105 let c1 = arena.alloc(C::new());
106 let c2 = arena.alloc(C::new());
107 let c3 = arena.alloc(C::new());
109 c1.v.push(CheckId(Cell::new(None)));
110 c1.v.push(CheckId(Cell::new(None)));
111 c2.v.push(CheckId(Cell::new(None)));
112 c2.v.push(CheckId(Cell::new(None)));
113 c3.v.push(CheckId(Cell::new(None)));
114 c3.v.push(CheckId(Cell::new(None)));
116 c1.v[0].v.set(Some(c2));
117 c1.v[1].v.set(Some(c3));
118 c2.v[0].v.set(Some(c2));
119 c2.v[1].v.set(Some(c3));
120 c3.v[0].v.set(Some(c1));
121 c3.v[1].v.set(Some(c2));
125 let arena = TypedArena::default();
127 } //~^ ERROR `arena` does not live long enough