rust/tests/ui/regions/region-object-lifetime-in-coercion.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
786 B
Rust
Raw Normal View History

// Test that attempts to implicitly coerce a value into an
// object respect the lifetime bound on the object type.
2015-04-18 00:12:20 -05:00
trait Foo {}
2014-10-21 22:53:03 -05:00
impl<'a> Foo for &'a [u8] {}
2019-05-28 13:46:13 -05:00
fn a(v: &[u8]) -> Box<dyn Foo + 'static> {
let x: Box<dyn Foo + 'static> = Box::new(v);
2022-04-01 12:13:25 -05:00
//~^ ERROR lifetime may not live long enough
x
}
2019-05-28 13:46:13 -05:00
fn b(v: &[u8]) -> Box<dyn Foo + 'static> {
Box::new(v)
2022-04-01 12:13:25 -05:00
//~^ ERROR lifetime may not live long enough
}
2019-05-28 13:46:13 -05:00
fn c(v: &[u8]) -> Box<dyn Foo> {
// same as previous case due to RFC 599
Box::new(v)
2022-04-01 12:13:25 -05:00
//~^ ERROR lifetime may not live long enough
}
2019-05-28 13:46:13 -05:00
fn d<'a,'b>(v: &'a [u8]) -> Box<dyn Foo+'b> {
Box::new(v)
2022-04-01 12:13:25 -05:00
//~^ ERROR lifetime may not live long enough
}
2019-05-28 13:46:13 -05:00
fn e<'a:'b,'b>(v: &'a [u8]) -> Box<dyn Foo+'b> {
Box::new(v) // OK, thanks to 'a:'b
}
fn main() { }