summaryrefslogtreecommitdiffstats
path: root/tests/ui/impl-trait/must_outlive_least_region_or_bound.rs
blob: 18404f9860317b7c3a9926d3775fa1faf99082b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::fmt::Debug;

fn elided(x: &i32) -> impl Copy { x }
//~^ ERROR: captures lifetime that does not appear in bounds

fn explicit<'a>(x: &'a i32) -> impl Copy { x }
//~^ ERROR: captures lifetime that does not appear in bounds

fn elided2(x: &i32) -> impl Copy + 'static { x }
//~^ ERROR lifetime may not live long enough

fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
//~^ ERROR lifetime may not live long enough

fn foo<'a>(x: &i32) -> impl Copy + 'a { x }
//~^ ERROR explicit lifetime required in the type of `x`

fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) }

fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug> { Box::new(x) }

fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) }

fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'static> { Box::new(x) }

fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug) { (Box::new(x), x) }
//~^ ERROR lifetime may not live long enough

trait LifetimeTrait<'a> {}
impl<'a> LifetimeTrait<'a> for &'a i32 {}

fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
//~^ ERROR lifetime may not live long enough

// Tests that a closure type containing 'b cannot be returned from a type where
// only 'a was expected.
fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32) {
    move |_| println!("{}", y)
    //~^ ERROR: captures lifetime that does not appear in bounds
}

fn ty_param_wont_outlive_static<T:Debug>(x: T) -> impl Debug + 'static {
    x
    //~^ ERROR the parameter type `T` may not live long enough
}

fn main() {}