]>
Commit | Line | Data |
---|---|---|
85aaf69f SL |
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 Vec. | |
12 | // | |
13 | // (Compare against compile-fail/dropck_arr_cycle_checked.rs) | |
14 | ||
85aaf69f SL |
15 | use std::cell::Cell; |
16 | use id::Id; | |
17 | ||
18 | mod s { | |
19 | #![allow(unstable)] | |
c34b1796 | 20 | use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; |
85aaf69f | 21 | |
c34b1796 | 22 | static S_COUNT: AtomicUsize = ATOMIC_USIZE_INIT; |
85aaf69f SL |
23 | |
24 | pub fn next_count() -> usize { | |
25 | S_COUNT.fetch_add(1, Ordering::SeqCst) + 1 | |
26 | } | |
27 | } | |
28 | ||
29 | mod id { | |
30 | use s; | |
31 | #[derive(Debug)] | |
32 | pub struct Id { | |
33 | orig_count: usize, | |
34 | count: usize, | |
35 | } | |
36 | ||
37 | impl Id { | |
38 | pub fn new() -> Id { | |
39 | let c = s::next_count(); | |
40 | println!("building Id {}", c); | |
41 | Id { orig_count: c, count: c } | |
42 | } | |
43 | pub fn count(&self) -> usize { | |
44 | println!("Id::count on {} returns {}", self.orig_count, self.count); | |
45 | self.count | |
46 | } | |
47 | } | |
48 | ||
49 | impl Drop for Id { | |
50 | fn drop(&mut self) { | |
51 | println!("dropping Id {}", self.count); | |
52 | self.count = 0; | |
53 | } | |
54 | } | |
55 | } | |
56 | ||
57 | trait HasId { | |
58 | fn count(&self) -> usize; | |
59 | } | |
60 | ||
61 | #[derive(Debug)] | |
62 | struct CheckId<T:HasId> { | |
63 | v: T | |
64 | } | |
65 | ||
66 | #[allow(non_snake_case)] | |
67 | fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } } | |
68 | ||
85aaf69f SL |
69 | impl<T:HasId> Drop for CheckId<T> { |
70 | fn drop(&mut self) { | |
71 | assert!(self.v.count() > 0); | |
72 | } | |
73 | } | |
74 | ||
75 | #[derive(Debug)] | |
76 | struct C<'a> { | |
77 | id: Id, | |
78 | v: Vec<CheckId<Cell<Option<&'a C<'a>>>>>, | |
79 | } | |
80 | ||
81 | impl<'a> HasId for Cell<Option<&'a C<'a>>> { | |
82 | fn count(&self) -> usize { | |
83 | match self.get() { | |
84 | None => 1, | |
85 | Some(c) => c.id.count(), | |
86 | } | |
87 | } | |
88 | } | |
89 | ||
90 | impl<'a> C<'a> { | |
91 | fn new() -> C<'a> { | |
92 | C { id: Id::new(), v: Vec::new() } | |
93 | } | |
94 | } | |
95 | ||
96 | fn f() { | |
97 | let (mut c1, mut c2, mut c3); | |
98 | c1 = C::new(); | |
99 | c2 = C::new(); | |
100 | c3 = C::new(); | |
101 | ||
102 | c1.v.push(CheckId(Cell::new(None))); | |
103 | c1.v.push(CheckId(Cell::new(None))); | |
104 | c2.v.push(CheckId(Cell::new(None))); | |
105 | c2.v.push(CheckId(Cell::new(None))); | |
106 | c3.v.push(CheckId(Cell::new(None))); | |
107 | c3.v.push(CheckId(Cell::new(None))); | |
108 | ||
109 | c1.v[0].v.set(Some(&c2)); //~ ERROR `c2` does not live long enough | |
110 | c1.v[1].v.set(Some(&c3)); //~ ERROR `c3` does not live long enough | |
111 | c2.v[0].v.set(Some(&c2)); //~ ERROR `c2` does not live long enough | |
112 | c2.v[1].v.set(Some(&c3)); //~ ERROR `c3` does not live long enough | |
113 | c3.v[0].v.set(Some(&c1)); //~ ERROR `c1` does not live long enough | |
114 | c3.v[1].v.set(Some(&c2)); //~ ERROR `c2` does not live long enough | |
115 | } | |
116 | ||
117 | fn main() { | |
118 | f(); | |
119 | } |