]> git.lizzy.rs Git - rust.git/blob - tests/ui/regions/regions-close-associated-type-into-object.rs
internally change regions to be covariant
[rust.git] / tests / ui / regions / regions-close-associated-type-into-object.rs
1 trait X {}
2
3
4
5 trait Iter {
6     type Item: X;
7
8     fn into_item(self) -> Self::Item;
9     fn as_item(&self) -> &Self::Item;
10 }
11
12 fn bad1<T: Iter>(v: T) -> Box<dyn X + 'static>
13 {
14     let item = v.into_item();
15     Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough
16 }
17
18 fn bad2<T: Iter>(v: T) -> Box<dyn X + 'static>
19     where Box<T::Item> : X
20 {
21     let item: Box<_> = Box::new(v.into_item());
22     Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough
23 }
24
25 fn bad3<'a, T: Iter>(v: T) -> Box<dyn X + 'a>
26 {
27     let item = v.into_item();
28     Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough
29 }
30
31 fn bad4<'a, T: Iter>(v: T) -> Box<dyn X + 'a>
32     where Box<T::Item> : X
33 {
34     let item: Box<_> = Box::new(v.into_item());
35     Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough
36 }
37
38 fn ok1<'a, T: Iter>(v: T) -> Box<dyn X + 'a>
39     where T::Item : 'a
40 {
41     let item = v.into_item();
42     Box::new(item) // OK, T::Item : 'a is declared
43 }
44
45 fn ok2<'a, T: Iter>(v: &T, w: &'a T::Item) -> Box<dyn X + 'a>
46     where T::Item : Clone
47 {
48     let item = Clone::clone(w);
49     Box::new(item) // OK, T::Item : 'a is implied
50 }
51
52 fn ok3<'a, T: Iter>(v: &'a T) -> Box<dyn X + 'a>
53     where T::Item : Clone + 'a
54 {
55     let item = Clone::clone(v.as_item());
56     Box::new(item) // OK, T::Item : 'a was declared
57 }
58
59 fn meh1<'a, T: Iter>(v: &'a T) -> Box<dyn X + 'a>
60     where T::Item : Clone
61 {
62     // This case is kind of interesting. It's the same as `ok3` but
63     // without the explicit declaration. This is valid because `T: 'a
64     // => T::Item: 'a`, and the former we can deduce from our argument
65     // of type `&'a T`.
66
67     let item = Clone::clone(v.as_item());
68     Box::new(item)
69 }
70
71 fn main() {}