]> git.lizzy.rs Git - rust.git/blob - src/test/ui/span/vec-must-not-hide-type-from-dropck.rs
improve "Doesn't live long enough" error
[rust.git] / src / test / ui / span / vec-must-not-hide-type-from-dropck.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 // Checking that `Vec<T>` cannot hide lifetimes within `T` when `T`
12 // implements `Drop` and might access methods of values that have
13 // since been deallocated.
14 //
15 // In this case, the values in question hold (non-zero) unique-ids
16 // that zero themselves out when dropped, and are wrapped in another
17 // type with a destructor that asserts that the ids it references are
18 // indeed non-zero (i.e., effectively checking that the id's are not
19 // dropped while there are still any outstanding references).
20 //
21 // However, the values in question are also formed into a
22 // cyclic-structure, ensuring that there is no way for all of the
23 // conditions above to be satisfied, meaning that if the dropck is
24 // sound, it should reject this code.
25
26 #![feature(const_fn)]
27
28 use std::cell::Cell;
29 use id::Id;
30
31 mod s {
32     use std::sync::atomic::{AtomicUsize, Ordering};
33
34     static S_COUNT: AtomicUsize = AtomicUsize::new(0);
35
36     /// generates globally unique count (global across the current
37     /// process, that is)
38     pub fn next_count() -> usize {
39         S_COUNT.fetch_add(1, Ordering::SeqCst) + 1
40     }
41 }
42
43 mod id {
44     use s;
45
46     /// Id represents a globally unique identifier (global across the
47     /// current process, that is). When dropped, it automatically
48     /// clears its `count` field, but leaves `orig_count` untouched,
49     /// so that if there are subsequent (erroneous) invocations of its
50     /// method (which is unsound), we can observe it by seeing that
51     /// the `count` is 0 while the `orig_count` is non-zero.
52     #[derive(Debug)]
53     pub struct Id {
54         orig_count: usize,
55         count: usize,
56     }
57
58     impl Id {
59         /// Creates an `Id` with a globally unique count.
60         pub fn new() -> Id {
61             let c = s::next_count();
62             println!("building Id {}", c);
63             Id { orig_count: c, count: c }
64         }
65         /// returns the `count` of self; should be non-zero if
66         /// everything is working.
67         pub fn count(&self) -> usize {
68             println!("Id::count on {} returns {}", self.orig_count, self.count);
69             self.count
70         }
71     }
72
73     impl Drop for Id {
74         fn drop(&mut self) {
75             println!("dropping Id {}", self.count);
76             self.count = 0;
77         }
78     }
79 }
80
81 trait HasId {
82     fn count(&self) -> usize;
83 }
84
85 #[derive(Debug)]
86 struct CheckId<T:HasId> {
87     v: T
88 }
89
90 #[allow(non_snake_case)]
91 fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } }
92
93 impl<T:HasId> Drop for CheckId<T> {
94     fn drop(&mut self) {
95         assert!(self.v.count() > 0);
96     }
97 }
98
99 #[derive(Debug)]
100 struct C<'a> {
101     id: Id,
102     v: Vec<CheckId<Cell<Option<&'a C<'a>>>>>,
103 }
104
105 impl<'a> HasId for Cell<Option<&'a C<'a>>> {
106     fn count(&self) -> usize {
107         match self.get() {
108             None => 1,
109             Some(c) => c.id.count(),
110         }
111     }
112 }
113
114 impl<'a> C<'a> {
115     fn new() -> C<'a> {
116         C { id: Id::new(), v: Vec::new() }
117     }
118 }
119
120 fn f() {
121     let (mut c1, mut c2);
122     c1 = C::new();
123     c2 = C::new();
124
125     c1.v.push(CheckId(Cell::new(None)));
126     c2.v.push(CheckId(Cell::new(None)));
127     c1.v[0].v.set(Some(&c2));
128     c2.v[0].v.set(Some(&c1));
129 }
130 //~^ ERROR `c2` does not live long enough
131 //~| ERROR `c1` does not live long enough
132
133 fn main() {
134     f();
135 }