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