]> git.proxmox.com Git - rustc.git/blob - tests/ui-fulldeps/dropck_tarena_sound_drop.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / 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 // Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta
18 // files.
19 #[allow(unused_extern_crates)]
20 extern crate rustc_driver;
21
22 use rustc_arena::TypedArena;
23
24 trait HasId { fn count(&self) -> usize; }
25
26 struct CheckId<T:HasId> { v: T }
27
28 // In the code below, the impl of HasId for `&'a usize` does not
29 // actually access the borrowed data, but the point is that the
30 // interface to CheckId does not (and cannot) know that, and therefore
31 // when encountering a value V of type CheckId<S>, we must
32 // conservatively force the type S to strictly outlive V.
33 impl<T:HasId> Drop for CheckId<T> {
34 fn drop(&mut self) {
35 assert!(self.v.count() > 0);
36 }
37 }
38
39 struct C<'a> { _v: CheckId<&'a usize>, }
40
41 impl<'a> HasId for &'a usize { fn count(&self) -> usize { 1 } }
42
43 fn f<'a, 'b>(_arena: &'a TypedArena<C<'b>>) {}
44
45 fn main() {
46 let arena: TypedArena<C> = TypedArena::default();
47 f(&arena);
48 }