1 // Regression test for Issue 25199: Check that one cannot hide a
2 // destructor's access to borrowed data behind a boxed trait object.
4 // Prior to fixing Issue 25199, this example was able to be compiled
5 // with rustc, and thus when you ran it, you would see the `Drop` impl
6 // for `Test` accessing state that had already been dropping (which is
7 // marked explicitly here with checking code within the `Drop` impl
8 // for `VecHolder`, but in the general case could just do unsound
9 // things like accessing memory that has been freed).
11 // Note that I would have liked to encode my go-to example of cyclic
12 // structure that accesses its neighbors in drop (and thus is
13 // fundamentally unsound) via this trick, but the closest I was able
14 // to come was dropck_trait_cycle_checked.rs, which is not quite as
15 // "good" as this regression test because the encoding of that example
16 // was forced to attach a lifetime to the trait definition itself
17 // (`trait Obj<'a>`) while *this* example is solely
19 use std::cell::RefCell;
24 v: Vec<(bool, &'static str)>,
27 impl Drop for VecHolder {
29 println!("Dropping Vec");
31 self.v[30].1 = "invalid access: VecHolder dropped already";
35 struct Container<'a> {
37 d: RefCell<Vec<Box<dyn Obj+'a>>>,
40 impl<'a> Container<'a> {
41 fn new() -> Container<'a> {
43 d: RefCell::new(Vec::new()),
45 v: vec![(true, "valid"); 100]
50 fn store<T: Obj+'a>(&'a self, val: T) {
51 self.d.borrow_mut().push(Box::new(val));
56 test: &'a Container<'a>,
59 impl<'a> Obj for Test<'a> { }
60 impl<'a> Drop for Test<'a> {
62 for e in &self.test.v.v {
69 let container = Container::new();
70 let test = Test{test: &container};
71 //~^ ERROR `container` does not live long enough
72 println!("container.v[30]: {:?}", container.v.v[30]);
73 container.store(test);