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