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