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 Vec.
13 // (Compare against compile-fail/dropck_arr_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);
80 v
: Vec
<CheckId
<Cell
<Option
<&'a C
<'a
>>>>>,
83 impl<'a
> HasId
for Cell
<Option
<&'a C
<'a
>>> {
84 fn count(&self) -> usize {
87 Some(c
) => c
.id
.count(),
94 C { id: Id::new(), v: Vec::new() }
99 let (mut c1
, mut c2
, mut c3
);
104 c1
.v
.push(CheckId(Cell
::new(None
)));
105 c1
.v
.push(CheckId(Cell
::new(None
)));
106 c2
.v
.push(CheckId(Cell
::new(None
)));
107 c2
.v
.push(CheckId(Cell
::new(None
)));
108 c3
.v
.push(CheckId(Cell
::new(None
)));
109 c3
.v
.push(CheckId(Cell
::new(None
)));
111 c1
.v
[0].v
.set(Some(&c2
)); //~ ERROR `c2` does not live long enough
112 c1
.v
[1].v
.set(Some(&c3
)); //~ ERROR `c3` does not live long enough
113 c2
.v
[0].v
.set(Some(&c2
)); //~ ERROR `c2` does not live long enough
114 c2
.v
[1].v
.set(Some(&c3
)); //~ ERROR `c3` does not live long enough
115 c3
.v
[0].v
.set(Some(&c1
)); //~ ERROR `c1` does not live long enough
116 c3
.v
[1].v
.set(Some(&c2
)); //~ ERROR `c2` does not live long enough