2015-10-07 17:11:25 -05:00
|
|
|
// Check that an arena (TypedArena) cannot carry elements whose drop
|
2014-10-27 06:58:30 -05:00
|
|
|
// methods might access borrowed data of lifetime that does not
|
|
|
|
// strictly outlive the arena itself.
|
|
|
|
//
|
|
|
|
// Compare against run-pass/dropck_tarena_sound_drop.rs, which shows a
|
|
|
|
// similar setup, but loosens `f` so that the struct `C<'a>` can be
|
|
|
|
// fed a lifetime longer than that of the arena.
|
|
|
|
//
|
|
|
|
// (Also compare against dropck_tarena_cycle_checked.rs, from which
|
|
|
|
// this was reduced to better understand its error message.)
|
|
|
|
|
2016-11-10 11:08:21 -06:00
|
|
|
#![feature(rustc_private)]
|
|
|
|
|
2020-06-02 12:19:49 -05:00
|
|
|
extern crate rustc_arena;
|
2014-10-27 06:58:30 -05:00
|
|
|
|
2020-06-02 12:19:49 -05:00
|
|
|
use rustc_arena::TypedArena;
|
2014-10-27 06:58:30 -05:00
|
|
|
|
|
|
|
trait HasId { fn count(&self) -> usize; }
|
|
|
|
|
|
|
|
struct CheckId<T:HasId> { v: T }
|
|
|
|
|
|
|
|
// In the code below, the impl of HasId for `&'a usize` does not
|
|
|
|
// actually access the borrowed data, but the point is that the
|
|
|
|
// interface to CheckId does not (and cannot) know that, and therefore
|
2015-10-07 17:11:25 -05:00
|
|
|
// when encountering a value V of type CheckId<S>, we must
|
2014-10-27 06:58:30 -05:00
|
|
|
// conservatively force the type S to strictly outlive V.
|
|
|
|
impl<T:HasId> Drop for CheckId<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
assert!(self.v.count() > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct C<'a> { v: CheckId<&'a usize>, }
|
|
|
|
|
|
|
|
impl<'a> HasId for &'a usize { fn count(&self) -> usize { 1 } }
|
|
|
|
|
|
|
|
fn f<'a>(_arena: &'a TypedArena<C<'a>>) {}
|
|
|
|
|
|
|
|
fn main() {
|
2018-10-17 02:11:55 -05:00
|
|
|
let arena: TypedArena<C> = TypedArena::default();
|
2016-10-14 10:55:45 -05:00
|
|
|
f(&arena);
|
2017-12-14 11:57:34 -06:00
|
|
|
} //~^ ERROR `arena` does not live long enough
|