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