|
| 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 trait |
| 12 | +// objects to hide the the cross-references. |
| 13 | +// |
| 14 | +// (Compare against compile-fail/dropck_vec_cycle_checked.rs) |
| 15 | + |
| 16 | +use std::cell::Cell; |
| 17 | +use id::Id; |
| 18 | + |
| 19 | +mod s { |
| 20 | + use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; |
| 21 | + |
| 22 | + static S_COUNT: AtomicUsize = ATOMIC_USIZE_INIT; |
| 23 | + |
| 24 | + pub fn next_count() -> usize { |
| 25 | + S_COUNT.fetch_add(1, Ordering::SeqCst) + 1 |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +mod id { |
| 30 | + use s; |
| 31 | + #[derive(Debug)] |
| 32 | + pub struct Id { |
| 33 | + orig_count: usize, |
| 34 | + count: usize, |
| 35 | + } |
| 36 | + |
| 37 | + impl Id { |
| 38 | + pub fn new() -> Id { |
| 39 | + let c = s::next_count(); |
| 40 | + println!("building Id {}", c); |
| 41 | + Id { orig_count: c, count: c } |
| 42 | + } |
| 43 | + pub fn count(&self) -> usize { |
| 44 | + println!("Id::count on {} returns {}", self.orig_count, self.count); |
| 45 | + self.count |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + impl Drop for Id { |
| 50 | + fn drop(&mut self) { |
| 51 | + println!("dropping Id {}", self.count); |
| 52 | + self.count = 0; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +trait HasId { |
| 58 | + fn count(&self) -> usize; |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Debug)] |
| 62 | +struct CheckId<T:HasId> { |
| 63 | + v: T |
| 64 | +} |
| 65 | + |
| 66 | +#[allow(non_snake_case)] |
| 67 | +fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } } |
| 68 | + |
| 69 | +impl<T:HasId> Drop for CheckId<T> { |
| 70 | + fn drop(&mut self) { |
| 71 | + assert!(self.v.count() > 0); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +trait Obj<'a> : HasId { |
| 76 | + fn set0(&self, b: &'a Box<Obj<'a>>); |
| 77 | + fn set1(&self, b: &'a Box<Obj<'a>>); |
| 78 | +} |
| 79 | + |
| 80 | +struct O<'a> { |
| 81 | + id: Id, |
| 82 | + obj0: CheckId<Cell<Option<&'a Box<Obj<'a>>>>>, |
| 83 | + obj1: CheckId<Cell<Option<&'a Box<Obj<'a>>>>>, |
| 84 | +} |
| 85 | + |
| 86 | +impl<'a> HasId for O<'a> { |
| 87 | + fn count(&self) -> usize { self.id.count() } |
| 88 | +} |
| 89 | + |
| 90 | +impl<'a> O<'a> { |
| 91 | + fn new() -> Box<O<'a>> { |
| 92 | + Box::new(O { |
| 93 | + id: Id::new(), |
| 94 | + obj0: CheckId(Cell::new(None)), |
| 95 | + obj1: CheckId(Cell::new(None)), |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl<'a> HasId for Cell<Option<&'a Box<Obj<'a>>>> { |
| 101 | + fn count(&self) -> usize { |
| 102 | + match self.get() { |
| 103 | + None => 1, |
| 104 | + Some(c) => c.count(), |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl<'a> Obj<'a> for O<'a> { |
| 110 | + fn set0(&self, b: &'a Box<Obj<'a>>) { |
| 111 | + self.obj0.v.set(Some(b)) |
| 112 | + } |
| 113 | + fn set1(&self, b: &'a Box<Obj<'a>>) { |
| 114 | + self.obj1.v.set(Some(b)) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +fn f() { |
| 120 | + let (o1, o2, o3): (Box<Obj>, Box<Obj>, Box<Obj>) = (O::new(), O::new(), O::new()); |
| 121 | + o1.set0(&o2); //~ ERROR `o2` does not live long enough |
| 122 | + o1.set1(&o3); //~ ERROR `o3` does not live long enough |
| 123 | + o2.set0(&o2); //~ ERROR `o2` does not live long enough |
| 124 | + o2.set1(&o3); //~ ERROR `o3` does not live long enough |
| 125 | + o3.set0(&o1); //~ ERROR `o1` does not live long enough |
| 126 | + o3.set1(&o2); //~ ERROR `o2` does not live long enough |
| 127 | +} |
| 128 | + |
| 129 | +fn main() { |
| 130 | + f(); |
| 131 | +} |
0 commit comments