]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass-fulldeps/dropck_tarena_sound_drop.rs
Auto merge of #54720 - davidtwco:issue-51191, r=nikomatsakis
[rust.git] / src / test / run-pass-fulldeps / dropck_tarena_sound_drop.rs
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 #![allow(unknown_lints)]
12 // Check that an arena (TypedArena) can carry elements whose drop
13 // methods might access borrowed data, as long as the borrowed data
14 // has lifetime that strictly outlives the arena itself.
15 //
16 // Compare against compile-fail/dropck_tarena_unsound_drop.rs, which
17 // shows a similar setup, but restricts `f` so that the struct `C<'a>`
18 // is force-fed a lifetime equal to that of the borrowed arena.
19
20 #![allow(unstable)]
21 #![feature(rustc_private)]
22
23 extern crate arena;
24
25 use arena::TypedArena;
26
27 trait HasId { fn count(&self) -> usize; }
28
29 struct CheckId<T:HasId> { v: T }
30
31 // In the code below, the impl of HasId for `&'a usize` does not
32 // actually access the borrowed data, but the point is that the
33 // interface to CheckId does not (and cannot) know that, and therefore
34 // when encountering the a value V of type CheckId<S>, we must
35 // conservatively force the type S to strictly outlive V.
36 impl<T:HasId> Drop for CheckId<T> {
37     fn drop(&mut self) {
38         assert!(self.v.count() > 0);
39     }
40 }
41
42 struct C<'a> { _v: CheckId<&'a usize>, }
43
44 impl<'a> HasId for &'a usize { fn count(&self) -> usize { 1 } }
45
46 fn f<'a, 'b>(_arena: &'a TypedArena<C<'b>>) {}
47
48 fn main() {
49     let arena: TypedArena<C> = TypedArena::new();
50     f(&arena);
51 }