]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/impl-trait/lifetimes.rs
Fix impl Trait Lifetime Handling
[rust.git] / src / test / run-pass / impl-trait / lifetimes.rs
1 // Copyright 2017 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 #![feature(conservative_impl_trait)]
12 #![allow(warnings)]
13
14 use std::fmt::Debug;
15
16 fn any_lifetime<'a>() -> &'a u32 { &5 }
17
18 fn static_lifetime() -> &'static u32 { &5 }
19
20 fn any_lifetime_as_static_impl_trait() -> impl Debug {
21     any_lifetime()
22 }
23
24 fn lifetimes_as_static_impl_trait() -> impl Debug {
25     static_lifetime()
26 }
27
28 fn no_params_or_lifetimes_is_static() -> impl Debug + 'static {
29     lifetimes_as_static_impl_trait()
30 }
31
32 fn static_input_type_is_static<T: Debug + 'static>(x: T) -> impl Debug + 'static { x }
33
34 fn type_outlives_reference_lifetime<'a, T: Debug>(x: &'a T) -> impl Debug + 'a { x }
35
36 trait SingleRegionTrait<'a> {}
37 impl<'a> SingleRegionTrait<'a> for u32 {}
38
39 fn simple_type_hrtb<'b>() -> impl for<'a> SingleRegionTrait<'a> { 5 }
40 fn closure_hrtb() -> impl for<'a> Fn(&'a u32) { |_| () }
41
42 fn mixed_lifetimes<'a>() -> impl for<'b: 'a> Fn(&'b u32) { |_| () }
43 fn mixed_as_static() -> impl Fn(&'static u32) { mixed_lifetimes() }
44
45 trait MultiRegionTrait<'a, 'b>: Debug {}
46
47 #[derive(Debug)]
48 struct MultiRegionStruct<'a, 'b>(&'a u32, &'b u32);
49 impl<'a, 'b> MultiRegionTrait<'a, 'b> for MultiRegionStruct<'a, 'b> {}
50
51 #[derive(Debug)]
52 struct NoRegionStruct;
53 impl<'a, 'b> MultiRegionTrait<'a, 'b> for NoRegionStruct {}
54
55 fn finds_least_region<'a: 'b, 'b>(x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> {
56     MultiRegionStruct(x, y)
57 }
58
59 fn finds_explicit_bound<'a: 'b, 'b>
60     (x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> + 'b
61 {
62     MultiRegionStruct(x, y)
63 }
64
65 fn finds_explicit_bound_even_without_least_region<'a, 'b>
66     (x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> + 'b
67 {
68     NoRegionStruct
69 }
70
71 /* FIXME: `impl Trait<'a> + 'b` should live as long as 'b, even if 'b outlives 'a
72 fn outlives_bounds_even_with_contained_regions<'a, 'b>
73     (x: &'a u32, y: &'b u32) -> impl Debug + 'b
74 {
75     finds_explicit_bound_even_without_least_region(x, y)
76 }
77 */
78
79 fn unnamed_lifetimes_arent_contained_in_impl_trait_and_will_unify<'a, 'b>
80     (x: &'a u32, y: &'b u32) -> impl Debug
81 {
82     fn deref<'lt>(x: &'lt u32) -> impl Debug { *x }
83
84     if true { deref(x) } else { deref(y) }
85 }
86
87 fn can_add_region_bound_to_static_type<'a, 'b>(_: &'a u32) -> impl Debug + 'a { 5 }
88
89 struct MyVec(Vec<Vec<u8>>);
90
91 impl<'unnecessary_lifetime> MyVec {
92     fn iter_doesnt_capture_unnecessary_lifetime<'s>(&'s self) -> impl Iterator<Item = &'s u8> {
93         self.0.iter().flat_map(|inner_vec| inner_vec.iter())
94     }
95 }
96
97 fn main() {}