2015-05-08 13:22:25 +00:00
|
|
|
// Reject mixing cyclic structure and Drop when using trait
|
2015-10-07 22:11:25 +00:00
|
|
|
// objects to hide the cross-references.
|
2015-05-08 13:22:25 +00:00
|
|
|
//
|
2020-12-28 17:15:16 +00:00
|
|
|
// (Compare against ui/span/dropck_vec_cycle_checked.rs)
|
2015-05-08 13:22:25 +00:00
|
|
|
|
|
|
|
use std::cell::Cell;
|
|
|
|
use id::Id;
|
|
|
|
|
|
|
|
mod s {
|
2015-05-27 08:18:36 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2015-05-08 13:22:25 +00:00
|
|
|
|
2015-05-27 08:18:36 +00:00
|
|
|
static S_COUNT: AtomicUsize = AtomicUsize::new(0);
|
2015-05-08 13:22:25 +00:00
|
|
|
|
|
|
|
pub fn next_count() -> usize {
|
|
|
|
S_COUNT.fetch_add(1, Ordering::SeqCst) + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod id {
|
|
|
|
use s;
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Id {
|
|
|
|
orig_count: usize,
|
|
|
|
count: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Id {
|
|
|
|
pub fn new() -> Id {
|
|
|
|
let c = s::next_count();
|
|
|
|
println!("building Id {}", c);
|
|
|
|
Id { orig_count: c, count: c }
|
|
|
|
}
|
|
|
|
pub fn count(&self) -> usize {
|
|
|
|
println!("Id::count on {} returns {}", self.orig_count, self.count);
|
|
|
|
self.count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Id {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
println!("dropping Id {}", self.count);
|
|
|
|
self.count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait HasId {
|
|
|
|
fn count(&self) -> usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct CheckId<T:HasId> {
|
|
|
|
v: T
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn CheckId<T:HasId>(t: T) -> CheckId<T> { CheckId{ v: t } }
|
|
|
|
|
|
|
|
impl<T:HasId> Drop for CheckId<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
assert!(self.v.count() > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Obj<'a> : HasId {
|
2019-05-28 18:46:13 +00:00
|
|
|
fn set0(&self, b: &'a Box<dyn Obj<'a>>);
|
|
|
|
fn set1(&self, b: &'a Box<dyn Obj<'a>>);
|
2015-05-08 13:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct O<'a> {
|
|
|
|
id: Id,
|
2019-05-28 18:46:13 +00:00
|
|
|
obj0: CheckId<Cell<Option<&'a Box<dyn Obj<'a>>>>>,
|
|
|
|
obj1: CheckId<Cell<Option<&'a Box<dyn Obj<'a>>>>>,
|
2015-05-08 13:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> HasId for O<'a> {
|
|
|
|
fn count(&self) -> usize { self.id.count() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> O<'a> {
|
|
|
|
fn new() -> Box<O<'a>> {
|
|
|
|
Box::new(O {
|
|
|
|
id: Id::new(),
|
|
|
|
obj0: CheckId(Cell::new(None)),
|
|
|
|
obj1: CheckId(Cell::new(None)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:46:13 +00:00
|
|
|
impl<'a> HasId for Cell<Option<&'a Box<dyn Obj<'a>>>> {
|
2015-05-08 13:22:25 +00:00
|
|
|
fn count(&self) -> usize {
|
|
|
|
match self.get() {
|
|
|
|
None => 1,
|
|
|
|
Some(c) => c.count(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Obj<'a> for O<'a> {
|
2019-05-28 18:46:13 +00:00
|
|
|
fn set0(&self, b: &'a Box<dyn Obj<'a>>) {
|
2015-05-08 13:22:25 +00:00
|
|
|
self.obj0.v.set(Some(b))
|
|
|
|
}
|
2019-05-28 18:46:13 +00:00
|
|
|
fn set1(&self, b: &'a Box<dyn Obj<'a>>) {
|
2015-05-08 13:22:25 +00:00
|
|
|
self.obj1.v.set(Some(b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn f() {
|
2019-05-28 18:46:13 +00:00
|
|
|
let (o1, o2, o3): (Box<dyn Obj>, Box<dyn Obj>, Box<dyn Obj>) = (O::new(), O::new(), O::new());
|
2015-05-08 13:22:25 +00:00
|
|
|
o1.set0(&o2); //~ ERROR `o2` does not live long enough
|
|
|
|
o1.set1(&o3); //~ ERROR `o3` does not live long enough
|
|
|
|
o2.set0(&o2); //~ ERROR `o2` does not live long enough
|
|
|
|
o2.set1(&o3); //~ ERROR `o3` does not live long enough
|
|
|
|
o3.set0(&o1); //~ ERROR `o1` does not live long enough
|
|
|
|
o3.set1(&o2); //~ ERROR `o2` does not live long enough
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
f();
|
|
|
|
}
|