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