]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail-fulldeps/dropck_tarena_unsound_drop.rs
Move parse-fail tests to UI
[rust.git] / src / test / compile-fail-fulldeps / dropck_tarena_unsound_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 // Check that an arena (TypedArena) cannot carry elements whose drop
12 // methods might access borrowed data of lifetime that does not
13 // strictly outlive the arena itself.
14 //
15 // Compare against run-pass/dropck_tarena_sound_drop.rs, which shows a
16 // similar setup, but loosens `f` so that the struct `C<'a>` can be
17 // fed a lifetime longer than that of the arena.
18 //
19 // (Also compare against dropck_tarena_cycle_checked.rs, from which
20 // this was reduced to better understand its error message.)
21
22 #![feature(rustc_private)]
23
24 extern crate arena;
25
26 use arena::TypedArena;
27
28 trait HasId { fn count(&self) -> usize; }
29
30 struct CheckId<T:HasId> { v: T }
31
32 // In the code below, the impl of HasId for `&'a usize` does not
33 // actually access the borrowed data, but the point is that the
34 // interface to CheckId does not (and cannot) know that, and therefore
35 // when encountering a value V of type CheckId<S>, we must
36 // conservatively force the type S to strictly outlive V.
37 impl<T:HasId> Drop for CheckId<T> {
38     fn drop(&mut self) {
39         assert!(self.v.count() > 0);
40     }
41 }
42
43 struct C<'a> { v: CheckId<&'a usize>, }
44
45 impl<'a> HasId for &'a usize { fn count(&self) -> usize { 1 } }
46
47 fn f<'a>(_arena: &'a TypedArena<C<'a>>) {}
48
49 fn main() {
50     let arena: TypedArena<C> = TypedArena::default();
51     f(&arena);
52 } //~^ ERROR `arena` does not live long enough
53