Skip to content

Commit db200c2

Browse files
committed
Add more tests for impl Trait lifetime handling
1 parent 163b65e commit db200c2

File tree

6 files changed

+96
-75
lines changed

6 files changed

+96
-75
lines changed

src/test/compile-fail/impl-trait/lifetimes.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
13+
use std::fmt::Debug;
14+
15+
fn elided(x: &i32) -> impl Copy { x }
16+
//~^ ERROR cannot infer an appropriate lifetime
17+
18+
fn explicit<'a>(x: &'a i32) -> impl Copy { x }
19+
//~^ ERROR cannot infer an appropriate lifetime
20+
21+
trait LifetimeTrait<'a> {}
22+
impl<'a> LifetimeTrait<'a> for &'a i32 {}
23+
24+
fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
25+
//~^ ERROR cannot infer an appropriate lifetime
26+
27+
// Tests that a closure type contianing 'b cannot be returned from a type where
28+
// only 'a was expected.
29+
fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32) {
30+
//~^ ERROR lifetime mismatch
31+
move |_| println!("{}", y)
32+
}
33+
34+
fn main() {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
13+
use std::fmt::Debug;
14+
15+
trait MultiRegionTrait<'a, 'b> {}
16+
impl<'a, 'b> MultiRegionTrait<'a, 'b> for (&'a u32, &'b u32) {}
17+
18+
fn no_least_region<'a, 'b>(x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> {
19+
//~^ ERROR ambiguous lifetime bound
20+
(x, y)
21+
}
22+
23+
fn main() {}

src/test/compile-fail/impl-trait/more_lifetimes.rs renamed to src/test/compile-fail/impl-trait/type_parameters_captured.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ trait Any {}
1616
impl<T> Any for T {}
1717

1818
// Check that type parameters are captured and not considered 'static
19-
fn whatever<T>(x: T) -> impl Any + 'static {
19+
fn foo<T>(x: T) -> impl Any + 'static {
2020
//~^ ERROR the parameter type `T` may not live long enough
2121
x
2222
}
2323

24-
fn move_lifetime_into_fn<'a, 'b>(x: &'a u32, y: &'b u32) -> impl Fn(&'a u32) {
25-
//~^ ERROR lifetime mismatch
26-
move |_| println!("{}", y)
27-
}
28-
2924
fn main() {}

src/test/run-pass/impl-trait/lifetimes.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,57 @@ fn lifetimes_as_static_impl_trait() -> impl Debug {
2525
static_lifetime()
2626
}
2727

28-
trait Foo<'a> {}
29-
impl<'a> Foo<'a> for u32 {}
30-
31-
fn foo<'b>() -> impl for<'a> Foo<'a> { 5 }
28+
trait SingleRegionTrait<'a> {}
29+
impl<'a> SingleRegionTrait<'a> for u32 {}
3230

31+
fn simple_type_hrtb<'b>() -> impl for<'a> SingleRegionTrait<'a> { 5 }
3332
fn closure_hrtb() -> impl for<'a> Fn(&'a u32) { |_| () }
3433

3534
fn mixed_lifetimes<'a>() -> impl for<'b: 'a> Fn(&'b u32) { |_| () }
36-
3735
fn mixed_as_static() -> impl Fn(&'static u32) { mixed_lifetimes() }
3836

39-
trait MultiRegion<'a, 'b> {}
37+
trait MultiRegionTrait<'a, 'b>: Debug {}
38+
39+
#[derive(Debug)]
4040
struct MultiRegionStruct<'a, 'b>(&'a u32, &'b u32);
41-
impl<'a, 'b> MultiRegion<'a, 'b> for MultiRegionStruct<'a, 'b> {}
41+
impl<'a, 'b> MultiRegionTrait<'a, 'b> for MultiRegionStruct<'a, 'b> {}
4242

43-
fn finds_least_region<'a: 'b, 'b>(x: &'a u32, y: &'b u32) -> impl MultiRegion<'a, 'b>
44-
{
43+
#[derive(Debug)]
44+
struct NoRegionStruct;
45+
impl<'a, 'b> MultiRegionTrait<'a, 'b> for NoRegionStruct {}
46+
47+
fn finds_least_region<'a: 'b, 'b>(x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> {
4548
MultiRegionStruct(x, y)
4649
}
4750

48-
fn explicit_bound<'a: 'b, 'b>(x: &'a u32, y: &'b u32) -> impl MultiRegion<'a, 'b> + 'b
51+
fn finds_explicit_bound<'a: 'b, 'b>
52+
(x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> + 'b
4953
{
5054
MultiRegionStruct(x, y)
5155
}
5256

57+
fn finds_explicit_bound_even_without_least_region<'a, 'b>
58+
(x: &'a u32, y: &'b u32) -> impl MultiRegionTrait<'a, 'b> + 'b
59+
{
60+
NoRegionStruct
61+
}
62+
63+
/* FIXME: `impl Trait<'a> + 'b` should live as long as 'b, even if 'b outlives 'a
64+
fn outlives_bounds_even_with_contained_regions<'a, 'b>
65+
(x: &'a u32, y: &'b u32) -> impl Debug + 'b
66+
{
67+
finds_explicit_bound_even_without_least_region(x, y)
68+
}
69+
*/
70+
71+
fn unnamed_lifetimes_arent_contained_in_impl_trait_and_will_unify<'a, 'b>
72+
(x: &'a u32, y: &'b u32) -> impl Debug
73+
{
74+
fn deref<'lt>(x: &'lt u32) -> impl Debug { *x }
75+
76+
if true { deref(x) } else { deref(y) }
77+
}
78+
79+
fn can_add_region_bound_to_static_type<'a, 'b>(_: &'a u32) -> impl Debug + 'a { 5 }
80+
5381
fn main() {}

src/test/ui/span/loan-extend.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)