]>
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 fixed length | |
12 | // arrays. | |
13 | // | |
14 | // (Compare against compile-fail/dropck_vec_cycle_checked.rs) | |
15 | ||
ea8adc8c | 16 | #![feature(const_atomic_usize_new)] |
62682a34 | 17 | |
85aaf69f SL |
18 | use std::cell::Cell; |
19 | use id::Id; | |
20 | ||
21 | mod s { | |
62682a34 | 22 | use std::sync::atomic::{AtomicUsize, Ordering}; |
85aaf69f | 23 | |
62682a34 | 24 | static S_COUNT: AtomicUsize = AtomicUsize::new(0); |
85aaf69f SL |
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 | ||
85aaf69f SL |
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 B<'a> { | |
79 | id: Id, | |
80 | a: [CheckId<Cell<Option<&'a B<'a>>>>; 2] | |
81 | } | |
82 | ||
83 | impl<'a> HasId for Cell<Option<&'a B<'a>>> { | |
84 | fn count(&self) -> usize { | |
85 | match self.get() { | |
86 | None => 1, | |
87 | Some(b) => b.id.count(), | |
88 | } | |
89 | } | |
90 | } | |
91 | ||
92 | impl<'a> B<'a> { | |
93 | fn new() -> B<'a> { | |
94 | B { id: Id::new(), a: [CheckId(Cell::new(None)), CheckId(Cell::new(None))] } | |
95 | } | |
96 | } | |
97 | ||
98 | fn f() { | |
99 | let (b1, b2, b3); | |
100 | b1 = B::new(); | |
101 | b2 = B::new(); | |
102 | b3 = B::new(); | |
c30ab7b3 SL |
103 | b1.a[0].v.set(Some(&b2)); |
104 | b1.a[1].v.set(Some(&b3)); | |
105 | b2.a[0].v.set(Some(&b2)); | |
106 | b2.a[1].v.set(Some(&b3)); | |
107 | b3.a[0].v.set(Some(&b1)); | |
108 | b3.a[1].v.set(Some(&b2)); | |
85aaf69f | 109 | } |
c30ab7b3 SL |
110 | //~^ ERROR `b2` does not live long enough |
111 | //~| ERROR `b3` does not live long enough | |
112 | //~| ERROR `b2` does not live long enough | |
113 | //~| ERROR `b3` does not live long enough | |
114 | //~| ERROR `b1` does not live long enough | |
115 | //~| ERROR `b2` does not live long enough | |
85aaf69f SL |
116 | |
117 | fn main() { | |
118 | f(); | |
119 | } |