diff --git a/tests/self_cell.rs b/tests/self_cell.rs index 78b294d..420c846 100644 --- a/tests/self_cell.rs +++ b/tests/self_cell.rs @@ -9,11 +9,13 @@ use std::panic::catch_unwind; use std::process::Command; use std::rc::Rc; use std::str; +use std::sync::Mutex; use crossbeam_utils::thread; use impls::impls; +use once_cell::sync::Lazy; use once_cell::unsync::OnceCell; use self_cell::self_cell; @@ -692,3 +694,43 @@ fn try_build_manual(path: &str) { fn invalid_compile_manual() { try_build_manual("tests/invalid_manual/wrong_covariance"); } + +#[test] +fn drop_order() { + #[derive(Debug, PartialEq, Eq)] + enum Dropped { + Owner, + Dependent, + } + + struct Owner; + struct Dependent<'a>(&'a Owner); + + static DROPS: Lazy>> = Lazy::new(<_>::default); + + impl Drop for Owner { + fn drop(&mut self) { + DROPS.lock().unwrap().push(Dropped::Owner) + } + } + impl Drop for Dependent<'_> { + fn drop(&mut self) { + DROPS.lock().unwrap().push(Dropped::Dependent) + } + } + + self_cell! { struct Foo { + owner: Owner, + #[covariant] + dependent: Dependent, + } + } + + let foo = Foo::new(Owner, |o| Dependent(o)); + DROPS.lock().unwrap().clear(); + drop(foo); + assert_eq!( + &DROPS.lock().unwrap()[..], + &[Dropped::Dependent, Dropped::Owner] + ); +}