]> git.lizzy.rs Git - rust.git/blob - src/test/ui/impl-trait/must_outlive_least_region_or_bound.rs
Account for multiple impl/dyn Trait in return type when suggesting `'_`
[rust.git] / src / test / ui / impl-trait / must_outlive_least_region_or_bound.rs
1 use std::fmt::Debug;
2
3 fn elided(x: &i32) -> impl Copy { x }
4 //~^ ERROR cannot infer an appropriate lifetime
5
6 fn explicit<'a>(x: &'a i32) -> impl Copy { x }
7 //~^ ERROR cannot infer an appropriate lifetime
8
9 fn elided2(x: &i32) -> impl Copy + 'static { x }
10 //~^ ERROR cannot infer an appropriate lifetime
11
12 fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
13 //~^ ERROR cannot infer an appropriate lifetime
14
15 fn foo<'a>(x: &i32) -> impl Copy + 'a { x }
16 //~^ ERROR explicit lifetime required in the type of `x`
17
18 fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) }
19 //~^ ERROR cannot infer an appropriate lifetime
20
21 fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug> { Box::new(x) }
22 //~^ ERROR cannot infer an appropriate lifetime
23
24 fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) }
25 //~^ ERROR cannot infer an appropriate lifetime
26
27 fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'static> { Box::new(x) }
28 //~^ ERROR cannot infer an appropriate lifetime
29
30 fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug) { (Box::new(x), x) }
31 //~^ ERROR cannot infer an appropriate lifetime
32 //~| ERROR cannot infer an appropriate lifetime
33
34 trait LifetimeTrait<'a> {}
35 impl<'a> LifetimeTrait<'a> for &'a i32 {}
36
37 fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
38 //~^ ERROR cannot infer an appropriate lifetime
39
40 // Tests that a closure type containing 'b cannot be returned from a type where
41 // only 'a was expected.
42 fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32) {
43     //~^ ERROR lifetime mismatch
44     move |_| println!("{}", y)
45 }
46
47 fn ty_param_wont_outlive_static<T:Debug>(x: T) -> impl Debug + 'static {
48     //~^ ERROR the parameter type `T` may not live long enough
49     x
50 }
51
52 fn main() {}