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