rust/src/test/ui/object-lifetime/object-lifetime-default-elision.rs

83 lines
2.1 KiB
Rust
Raw Normal View History

// ignore-compare-mode-nll
// revisions: base nll
// [nll]compile-flags: -Zborrowck=mir
// Test various cases where the old rules under lifetime elision
// yield slightly different results than the new rules.
#![allow(dead_code)]
trait SomeTrait {
fn dummy(&self) { }
}
struct SomeStruct<'a> {
2019-05-28 13:46:13 -05:00
r: Box<dyn SomeTrait+'a>
}
fn deref<T>(ss: &T) -> T {
// produces the type of a deref without worrying about whether a
// move out would actually be legal
loop { }
}
2019-05-28 13:46:13 -05:00
fn load0<'a>(ss: &'a Box<dyn SomeTrait>) -> Box<dyn SomeTrait> {
// Under old rules, the fully elaborated types of input/output were:
//
// for<'a,'b> fn(&'a Box<SomeTrait+'b>) -> Box<SomeTrait+'a>
//
// Under new rules the result is:
//
// for<'a> fn(&'a Box<SomeTrait+'static>) -> Box<SomeTrait+'static>
//
// Therefore, no type error.
deref(ss)
}
2019-05-28 13:46:13 -05:00
fn load1(ss: &dyn SomeTrait) -> &dyn SomeTrait {
// Under old rules, the fully elaborated types of input/output were:
//
// for<'a,'b> fn(&'a (SomeTrait+'b)) -> &'a (SomeTrait+'a)
//
// Under new rules the result is:
//
// for<'a> fn(&'a (SomeTrait+'a)) -> &'a (SomeTrait+'a)
//
// In both cases, returning `ss` is legal.
ss
}
2019-05-28 13:46:13 -05:00
fn load2<'a>(ss: &'a dyn SomeTrait) -> &dyn SomeTrait {
// Same as `load1` but with an explicit name thrown in for fun.
ss
}
2019-05-28 13:46:13 -05:00
fn load3<'a,'b>(ss: &'a dyn SomeTrait) -> &'b dyn SomeTrait {
// Under old rules, the fully elaborated types of input/output were:
//
// for<'a,'b,'c>fn(&'a (SomeTrait+'c)) -> &'b (SomeTrait+'a)
//
// Based on the input/output types, the compiler could infer that
// 'c : 'a
// 'b : 'a
// must hold, and therefore it permitted `&'a (Sometrait+'c)` to be
// coerced to `&'b (SomeTrait+'a)`.
//
// Under the newer defaults, though, we get:
//
// for<'a,'b> fn(&'a (SomeTrait+'a)) -> &'b (SomeTrait+'b)
//
// which fails to type check.
ss
//[base]~^ ERROR cannot infer
//[base]~| ERROR cannot infer
//[nll]~^^^ ERROR lifetime may not live long enough
}
fn main() {
}