2014-10-27 06:58:30 -05:00
|
|
|
// Reject mixing cyclic structure and Drop when using fixed length
|
|
|
|
// arrays.
|
|
|
|
//
|
2020-12-28 11:15:16 -06:00
|
|
|
// (Compare against ui/span/dropck_vec_cycle_checked.rs)
|
2014-10-27 06:58:30 -05:00
|
|
|
|
2017-11-26 16:19:18 -06:00
|
|
|
|
2015-05-29 08:42:32 -05:00
|
|
|
|
2014-10-27 06:58:30 -05:00
|
|
|
use std::cell::Cell;
|
|
|
|
use id::Id;
|
|
|
|
|
|
|
|
mod s {
|
2015-05-27 03:18:36 -05:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2014-10-27 06:58:30 -05:00
|
|
|
|
2015-05-27 03:18:36 -05:00
|
|
|
static S_COUNT: AtomicUsize = AtomicUsize::new(0);
|
2014-10-27 06:58:30 -05: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct B<'a> {
|
|
|
|
id: Id,
|
|
|
|
a: [CheckId<Cell<Option<&'a B<'a>>>>; 2]
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> HasId for Cell<Option<&'a B<'a>>> {
|
|
|
|
fn count(&self) -> usize {
|
|
|
|
match self.get() {
|
|
|
|
None => 1,
|
|
|
|
Some(b) => b.id.count(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> B<'a> {
|
|
|
|
fn new() -> B<'a> {
|
|
|
|
B { id: Id::new(), a: [CheckId(Cell::new(None)), CheckId(Cell::new(None))] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let (b1, b2, b3);
|
|
|
|
b1 = B::new();
|
|
|
|
b2 = B::new();
|
|
|
|
b3 = B::new();
|
2016-10-14 10:55:45 -05:00
|
|
|
b1.a[0].v.set(Some(&b2));
|
2017-12-13 19:27:23 -06:00
|
|
|
//~^ ERROR `b2` does not live long enough
|
2016-10-14 10:55:45 -05:00
|
|
|
b1.a[1].v.set(Some(&b3));
|
2017-12-13 19:27:23 -06:00
|
|
|
//~^ ERROR `b3` does not live long enough
|
2016-10-14 10:55:45 -05:00
|
|
|
b2.a[0].v.set(Some(&b2));
|
|
|
|
b2.a[1].v.set(Some(&b3));
|
|
|
|
b3.a[0].v.set(Some(&b1));
|
2017-12-13 19:27:23 -06:00
|
|
|
//~^ ERROR `b1` does not live long enough
|
2016-10-14 10:55:45 -05:00
|
|
|
b3.a[1].v.set(Some(&b2));
|
2014-10-27 06:58:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
f();
|
|
|
|
}
|