]>
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 | // A simple example of an unsound mixing of cyclic structure and Drop. | |
12 | // | |
13 | // Each `D` has a name and an optional reference to another `D` | |
14 | // sibling, but also implements a drop method that prints out its own | |
15 | // name as well as the name of its sibling. | |
16 | // | |
17 | // By setting up a cyclic structure, the drop code cannot possibly | |
18 | // work. Therefore this code must be rejected. | |
19 | // | |
20 | // (As it turns out, essentially any attempt to install a sibling here | |
21 | // will be rejected, regardless of whether it forms a cyclic | |
22 | // structure or not. This is because the use of the same lifetime | |
23 | // `'a` in `&'a D<'a>` cannot be satisfied when `D<'a>` implements | |
24 | // `Drop`.) | |
25 | ||
85aaf69f SL |
26 | use std::cell::Cell; |
27 | ||
28 | struct D<'a> { | |
29 | name: String, | |
30 | p: Cell<Option<&'a D<'a>>>, | |
31 | } | |
32 | ||
33 | impl<'a> D<'a> { | |
34 | fn new(name: String) -> D<'a> { D { name: name, p: Cell::new(None) } } | |
35 | } | |
36 | ||
85aaf69f SL |
37 | impl<'a> Drop for D<'a> { |
38 | fn drop(&mut self) { | |
39 | println!("dropping {} whose sibling is {:?}", | |
40 | self.name, self.p.get().map(|d| &d.name)); | |
41 | } | |
42 | } | |
43 | ||
44 | fn g() { | |
45 | let (d1, d2) = (D::new(format!("d1")), D::new(format!("d2"))); | |
46 | d1.p.set(Some(&d2)); //~ ERROR `d2` does not live long enough | |
47 | d2.p.set(Some(&d1)); //~ ERROR `d1` does not live long enough | |
48 | } | |
49 | ||
50 | fn main() { | |
51 | g(); | |
52 | } |