]> git.lizzy.rs Git - rust.git/blob - src/test/ui/regions/regions-close-associated-type-into-object.rs
Rollup merge of #53317 - estebank:abolish-ice, r=oli-obk
[rust.git] / src / test / ui / regions / regions-close-associated-type-into-object.rs
1 // Copyright 2014 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(box_syntax)]
12
13 trait X {}
14
15 trait Iter {
16     type Item: X;
17
18     fn into_item(self) -> Self::Item;
19     fn as_item(&self) -> &Self::Item;
20 }
21
22 fn bad1<T: Iter>(v: T) -> Box<X+'static>
23 {
24     let item = v.into_item();
25     Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough
26 }
27
28 fn bad2<T: Iter>(v: T) -> Box<X+'static>
29     where Box<T::Item> : X
30 {
31     let item: Box<_> = box v.into_item();
32     Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough
33 }
34
35 fn bad3<'a, T: Iter>(v: T) -> Box<X+'a>
36 {
37     let item = v.into_item();
38     Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough
39 }
40
41 fn bad4<'a, T: Iter>(v: T) -> Box<X+'a>
42     where Box<T::Item> : X
43 {
44     let item: Box<_> = box v.into_item();
45     Box::new(item) //~ ERROR associated type `<T as Iter>::Item` may not live long enough
46 }
47
48 fn ok1<'a, T: Iter>(v: T) -> Box<X+'a>
49     where T::Item : 'a
50 {
51     let item = v.into_item();
52     Box::new(item) // OK, T::Item : 'a is declared
53 }
54
55 fn ok2<'a, T: Iter>(v: &T, w: &'a T::Item) -> Box<X+'a>
56     where T::Item : Clone
57 {
58     let item = Clone::clone(w);
59     Box::new(item) // OK, T::Item : 'a is implied
60 }
61
62 fn ok3<'a, T: Iter>(v: &'a T) -> Box<X+'a>
63     where T::Item : Clone + 'a
64 {
65     let item = Clone::clone(v.as_item());
66     Box::new(item) // OK, T::Item : 'a was declared
67 }
68
69 fn meh1<'a, T: Iter>(v: &'a T) -> Box<X+'a>
70     where T::Item : Clone
71 {
72     // This case is kind of interesting. It's the same as `ok3` but
73     // without the explicit declaration. This is valid because `T: 'a
74     // => T::Item: 'a`, and the former we can deduce from our argument
75     // of type `&'a T`.
76
77     let item = Clone::clone(v.as_item());
78     Box::new(item)
79 }
80
81 fn main() {}