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.)
23 use arena
::TypedArena
;
29 use std
::sync
::atomic
::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}
;
31 static S_COUNT
: AtomicUsize
= ATOMIC_USIZE_INIT
;
33 pub fn next_count() -> usize {
34 S_COUNT
.fetch_add(1, Ordering
::SeqCst
) + 1
48 let c
= s
::next_count();
49 println
!("building Id {}", c
);
50 Id { orig_count: c, count: c }
52 pub fn count(&self) -> usize {
53 println
!("Id::count on {} returns {}", self.orig_count
, self.count
);
60 println
!("dropping Id {}", self.count
);
67 fn count(&self) -> usize;
71 struct CheckId
<T
:HasId
> {
75 #[allow(non_snake_case)]
76 fn CheckId
<T
:HasId
>(t
: T
) -> CheckId
<T
> { CheckId{ v: t }
}
78 impl<T
:HasId
> Drop
for CheckId
<T
> {
80 assert
!(self.v
.count() > 0);
87 v
: Vec
<CheckId
<Cell
<Option
<&'a C
<'a
>>>>>,
90 impl<'a
> HasId
for Cell
<Option
<&'a C
<'a
>>> {
91 fn count(&self) -> usize {
94 Some(c
) => c
.id
.count(),
101 C { id: Id::new(), v: Vec::new() }
105 fn f
<'a
>(arena
: &'a TypedArena
<C
<'a
>>) {
106 let c1
= arena
.alloc(C
::new());
107 let c2
= arena
.alloc(C
::new());
108 let c3
= arena
.alloc(C
::new());
110 c1
.v
.push(CheckId(Cell
::new(None
)));
111 c1
.v
.push(CheckId(Cell
::new(None
)));
112 c2
.v
.push(CheckId(Cell
::new(None
)));
113 c2
.v
.push(CheckId(Cell
::new(None
)));
114 c3
.v
.push(CheckId(Cell
::new(None
)));
115 c3
.v
.push(CheckId(Cell
::new(None
)));
117 c1
.v
[0].v
.set(Some(c2
));
118 c1
.v
[1].v
.set(Some(c3
));
119 c2
.v
[0].v
.set(Some(c2
));
120 c2
.v
[1].v
.set(Some(c3
));
121 c3
.v
[0].v
.set(Some(c1
));
122 c3
.v
[1].v
.set(Some(c2
));
126 let arena
= TypedArena
::new();
127 f(&arena
); //~ ERROR `arena` does not live long enough