]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/impl-trait/lifetimes.rs
Auto merge of #54530 - pnkfelix:issue-54047-migrate-ui-run-pass-back-to-run-pass...
[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 // run-pass
12
13 #![allow(warnings)]
14
15 use std::fmt::Debug;
16
17 fn any_lifetime<'a>() -> &'a u32 { &5 }
18
19 fn static_lifetime() -> &'static u32 { &5 }
20
21 fn any_lifetime_as_static_impl_trait() -> impl Debug {
22     any_lifetime()
23 }
24
25 fn lifetimes_as_static_impl_trait() -> impl Debug {
26     static_lifetime()
27 }
28
29 fn no_params_or_lifetimes_is_static() -> impl Debug + 'static {
30     lifetimes_as_static_impl_trait()
31 }
32
33 fn static_input_type_is_static<T: Debug + 'static>(x: T) -> impl Debug + 'static { x }
34
35 fn type_outlives_reference_lifetime<'a, T: Debug>(x: &'a T) -> impl Debug + 'a { x }
36 fn type_outlives_reference_lifetime_elided<T: Debug>(x: &T) -> impl Debug + '_ { x }
37
38 trait SingleRegionTrait<'a> {}
39 impl<'a> SingleRegionTrait<'a> for u32 {}
40 impl<'a> SingleRegionTrait<'a> for &'a u32 {}
41 struct SingleRegionStruct<'a>(&'a u32);
42
43 fn simple_type_hrtb<'b>() -> impl for<'a> SingleRegionTrait<'a> { 5 }
44 // FIXME(cramertj) add test after #45992 lands to ensure lint is triggered
45 fn elision_single_region_trait(x: &u32) -> impl SingleRegionTrait { x }
46 fn elision_single_region_struct(x: SingleRegionStruct) -> impl Into<SingleRegionStruct> { x }
47
48 fn closure_hrtb() -> impl for<'a> Fn(&'a u32) { |_| () }
49 fn closure_hr_elided() -> impl Fn(&u32) { |_| () }
50 fn closure_hr_elided_return() -> impl Fn(&u32) -> &u32 { |x| x }
51 fn closure_pass_through_elided_return(x: impl Fn(&u32) -> &u32) -> impl Fn(&u32) -> &u32 { x }
52 fn closure_pass_through_reference_elided(x: &impl Fn(&u32) -> &u32) -> &impl Fn(&u32) -> &u32 { x }
53
54 fn nested_lifetime<'a>(input: &'a str)
55     -> impl Iterator<Item = impl Iterator<Item = i32> + 'a> + 'a
56 {
57     input.lines().map(|line| {
58         line.split_whitespace().map(|cell| cell.parse().unwrap())
59     })
60 }
61
62 fn pass_through_elision(x: &u32) -> impl Into<&u32> { x }
63 fn pass_through_elision_with_fn_ptr(x: &fn(&u32) -> &u32) -> impl Into<&fn(&u32) -> &u32> { x }
64
65 fn pass_through_elision_with_fn_path<T: Fn(&u32) -> &u32>(
66     x: &T
67 ) -> &impl Fn(&u32) -> &u32 { x }
68
69 fn foo(x: &impl Debug) -> &impl Debug { x }
70 fn foo_explicit_lifetime<'a>(x: &'a impl Debug) -> &'a impl Debug { x }
71 fn foo_explicit_arg<T: Debug>(x: &T) -> &impl Debug { x }
72
73 fn mixed_lifetimes<'a>() -> impl for<'b> Fn(&'b &'a u32) { |_| () }
74 fn mixed_as_static() -> impl Fn(&'static &'static u32) { mixed_lifetimes() }
75
76 trait MultiRegionTrait<'a, 'b>: Debug {}
77
78 #[derive(Debug)]
79 struct MultiRegionStruct<'a, 'b>(&'a u32, &'b u32);
80 impl<'a, 'b> MultiRegionTrait<'a, 'b> for MultiRegionStruct<'a, 'b> {}
81
82 #[derive(Debug)]
83 struct NoRegionStruct;
84 impl<'a, 'b> MultiRegionTrait<'a, 'b> for NoRegionStruct {}
85
86 fn finds_least_region<'a: 'b, 'b>(x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> {
87     MultiRegionStruct(x, y)
88 }
89
90 fn finds_explicit_bound<'a: 'b, 'b>
91     (x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> + 'b
92 {
93     MultiRegionStruct(x, y)
94 }
95
96 fn finds_explicit_bound_even_without_least_region<'a, 'b>
97     (x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> + 'b
98 {
99     NoRegionStruct
100 }
101
102 /* FIXME: `impl Trait<'a> + 'b` should live as long as 'b, even if 'b outlives 'a
103 fn outlives_bounds_even_with_contained_regions<'a, 'b>
104     (x: &'a u32, y: &'b u32) -> impl Debug + 'b
105 {
106     finds_explicit_bound_even_without_least_region(x, y)
107 }
108 */
109
110 fn unnamed_lifetimes_arent_contained_in_impl_trait_and_will_unify<'a, 'b>
111     (x: &'a u32, y: &'b u32) -> impl Debug
112 {
113     fn deref<'lt>(x: &'lt u32) -> impl Debug { *x }
114
115     if true { deref(x) } else { deref(y) }
116 }
117
118 fn can_add_region_bound_to_static_type<'a, 'b>(_: &'a u32) -> impl Debug + 'a { 5 }
119
120 struct MyVec(Vec<Vec<u8>>);
121
122 impl<'unnecessary_lifetime> MyVec {
123     fn iter_doesnt_capture_unnecessary_lifetime<'s>(&'s self) -> impl Iterator<Item = &'s u8> {
124         self.0.iter().flat_map(|inner_vec| inner_vec.iter())
125     }
126 }
127
128 fn main() {}