|
| 1 | +// Copyright 2014 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 | +// This test used to be part of a run-pass test, but revised outlives |
| 12 | +// rule means that it no longer compiles. |
| 13 | + |
| 14 | +#![allow(unused_variables)] |
| 15 | + |
| 16 | +trait Trait<'a> { |
| 17 | + fn long(&'a self) -> isize; |
| 18 | + fn short<'b>(&'b self) -> isize; |
| 19 | +} |
| 20 | + |
| 21 | +fn object_invoke1<'d>(x: &'d Trait<'d>) -> (isize, isize) { loop { } } |
| 22 | + |
| 23 | +trait MakerTrait { |
| 24 | + fn mk() -> Self; |
| 25 | +} |
| 26 | + |
| 27 | +fn make_val<T:MakerTrait>() -> T { |
| 28 | + MakerTrait::mk() |
| 29 | +} |
| 30 | + |
| 31 | +impl<'t> MakerTrait for Box<Trait<'t>+'static> { |
| 32 | + fn mk() -> Box<Trait<'t>+'static> { loop { } } |
| 33 | +} |
| 34 | + |
| 35 | +pub fn main() { |
| 36 | + let m : Box<Trait+'static> = make_val(); |
| 37 | + assert_eq!(object_invoke1(&*m), (4,5)); |
| 38 | + //~^ ERROR `*m` does not live long enough |
| 39 | + |
| 40 | + // the problem here is that the full type of `m` is |
| 41 | + // |
| 42 | + // Box<Trait<'m>+'static> |
| 43 | + // |
| 44 | + // Here `'m` must be exactly the lifetime of the variable `m`. |
| 45 | + // This is because of two requirements: |
| 46 | + // 1. First, the basic type rules require that the |
| 47 | + // type of `m`'s value outlives the lifetime of `m`. This puts a lower |
| 48 | + // bound `'m`. |
| 49 | + // |
| 50 | + // 2. Meanwhile, the signature of `object_invoke1` requires that |
| 51 | + // we create a reference of type `&'d Trait<'d>` for some `'d`. |
| 52 | + // `'d` cannot outlive `'m`, so that forces the lifetime to be `'m`. |
| 53 | + // |
| 54 | + // This then conflicts with the dropck rules, which require that |
| 55 | + // the type of `m` *strictly outlives* `'m`. Hence we get an |
| 56 | + // error. |
| 57 | +} |
0 commit comments