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