]> git.lizzy.rs Git - rust.git/blob - src/test/ui-fulldeps/dropck_tarena_sound_drop.rs
:arrow_up: rust-analyzer
[rust.git] / src / test / ui-fulldeps / dropck_tarena_sound_drop.rs
1 // run-pass
2
3 #![allow(unknown_lints)]
4 // Check that an arena (TypedArena) can carry elements whose drop
5 // methods might access borrowed data, as long as the borrowed data
6 // has lifetime that strictly outlives the arena itself.
7 //
8 // Compare against ui-fulldeps/dropck-tarena-unsound-drop.rs, which
9 // shows a similar setup, but restricts `f` so that the struct `C<'a>`
10 // is force-fed a lifetime equal to that of the borrowed arena.
11
12 #![allow(unstable)]
13 #![feature(rustc_private)]
14
15 extern crate rustc_arena;
16
17 use rustc_arena::TypedArena;
18
19 trait HasId { fn count(&self) -> usize; }
20
21 struct CheckId<T:HasId> { v: T }
22
23 // In the code below, the impl of HasId for `&'a usize` does not
24 // actually access the borrowed data, but the point is that the
25 // interface to CheckId does not (and cannot) know that, and therefore
26 // when encountering a value V of type CheckId<S>, we must
27 // conservatively force the type S to strictly outlive V.
28 impl<T:HasId> Drop for CheckId<T> {
29     fn drop(&mut self) {
30         assert!(self.v.count() > 0);
31     }
32 }
33
34 struct C<'a> { _v: CheckId<&'a usize>, }
35
36 impl<'a> HasId for &'a usize { fn count(&self) -> usize { 1 } }
37
38 fn f<'a, 'b>(_arena: &'a TypedArena<C<'b>>) {}
39
40 fn main() {
41     let arena: TypedArena<C> = TypedArena::default();
42     f(&arena);
43 }