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.
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.
11 #![feature(conservative_impl_trait, underscore_lifetimes, universal_impl_trait)]
16 fn any_lifetime<'a>() -> &'a u32 { &5 }
18 fn static_lifetime() -> &'static u32 { &5 }
20 fn any_lifetime_as_static_impl_trait() -> impl Debug {
24 fn lifetimes_as_static_impl_trait() -> impl Debug {
28 fn no_params_or_lifetimes_is_static() -> impl Debug + 'static {
29 lifetimes_as_static_impl_trait()
32 fn static_input_type_is_static<T: Debug + 'static>(x: T) -> impl Debug + 'static { x }
34 fn type_outlives_reference_lifetime<'a, T: Debug>(x: &'a T) -> impl Debug + 'a { x }
35 fn type_outlives_reference_lifetime_elided<T: Debug>(x: &T) -> impl Debug + '_ { x }
37 trait SingleRegionTrait<'a> {}
38 impl<'a> SingleRegionTrait<'a> for u32 {}
39 impl<'a> SingleRegionTrait<'a> for &'a u32 {}
40 struct SingleRegionStruct<'a>(&'a u32);
42 fn simple_type_hrtb<'b>() -> impl for<'a> SingleRegionTrait<'a> { 5 }
43 // FIXME(cramertj) add test after #45992 lands to ensure lint is triggered
44 fn elision_single_region_trait(x: &u32) -> impl SingleRegionTrait { x }
45 fn elision_single_region_struct(x: SingleRegionStruct) -> impl Into<SingleRegionStruct> { x }
47 fn closure_hrtb() -> impl for<'a> Fn(&'a u32) { |_| () }
48 fn closure_hr_elided() -> impl Fn(&u32) { |_| () }
49 fn closure_hr_elided_return() -> impl Fn(&u32) -> &u32 { |x| x }
50 fn closure_pass_through_elided_return(x: impl Fn(&u32) -> &u32) -> impl Fn(&u32) -> &u32 { x }
51 fn closure_pass_through_reference_elided(x: &impl Fn(&u32) -> &u32) -> &impl Fn(&u32) -> &u32 { x }
53 fn pass_through_elision(x: &u32) -> impl Into<&u32> { x }
54 fn pass_through_elision_with_fn_ptr(x: &fn(&u32) -> &u32) -> impl Into<&fn(&u32) -> &u32> { x }
56 fn pass_through_elision_with_fn_path<T: Fn(&u32) -> &u32>(
58 ) -> impl Into<&impl Fn(&u32) -> &u32> { x }
60 fn foo(x: &impl Debug) -> impl Into<&impl Debug> { x }
61 fn foo_explicit_lifetime<'a>(x: &'a impl Debug) -> impl Into<&'a impl Debug> { x }
62 fn foo_no_outer_impl(x: &impl Debug) -> &impl Debug { x }
63 fn foo_explicit_arg<T: Debug>(x: &T) -> impl Into<&impl Debug> { x }
65 fn mixed_lifetimes<'a>() -> impl for<'b: 'a> Fn(&'b u32) { |_| () }
66 fn mixed_as_static() -> impl Fn(&'static u32) { mixed_lifetimes() }
68 trait MultiRegionTrait<'a, 'b>: Debug {}
71 struct MultiRegionStruct<'a, 'b>(&'a u32, &'b u32);
72 impl<'a, 'b> MultiRegionTrait<'a, 'b> for MultiRegionStruct<'a, 'b> {}
75 struct NoRegionStruct;
76 impl<'a, 'b> MultiRegionTrait<'a, 'b> for NoRegionStruct {}
78 fn finds_least_region<'a: 'b, 'b>(x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> {
79 MultiRegionStruct(x, y)
82 fn finds_explicit_bound<'a: 'b, 'b>
83 (x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> + 'b
85 MultiRegionStruct(x, y)
88 fn finds_explicit_bound_even_without_least_region<'a, 'b>
89 (x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> + 'b
94 /* FIXME: `impl Trait<'a> + 'b` should live as long as 'b, even if 'b outlives 'a
95 fn outlives_bounds_even_with_contained_regions<'a, 'b>
96 (x: &'a u32, y: &'b u32) -> impl Debug + 'b
98 finds_explicit_bound_even_without_least_region(x, y)
102 fn unnamed_lifetimes_arent_contained_in_impl_trait_and_will_unify<'a, 'b>
103 (x: &'a u32, y: &'b u32) -> impl Debug
105 fn deref<'lt>(x: &'lt u32) -> impl Debug { *x }
107 if true { deref(x) } else { deref(y) }
110 fn can_add_region_bound_to_static_type<'a, 'b>(_: &'a u32) -> impl Debug + 'a { 5 }
112 struct MyVec(Vec<Vec<u8>>);
114 impl<'unnecessary_lifetime> MyVec {
115 fn iter_doesnt_capture_unnecessary_lifetime<'s>(&'s self) -> impl Iterator<Item = &'s u8> {
116 self.0.iter().flat_map(|inner_vec| inner_vec.iter())