]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/regions-close-associated-type-into-object.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / test / compile-fail / 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 use std::marker::MarkerTrait;
14
15 trait X : MarkerTrait {}
16
17 trait Iter {
18     type Item: X;
19
20     fn into_item(self) -> Self::Item;
21     fn as_item(&self) -> &Self::Item;
22 }
23
24 fn bad1<T: Iter>(v: T) -> Box<X+'static>
25 {
26     let item = v.into_item();
27     box item //~ ERROR associated type `<T as Iter>::Item` may not live long enough
28 }
29
30 fn bad2<T: Iter>(v: T) -> Box<X+'static>
31     where Box<T::Item> : X
32 {
33     let item = box v.into_item();
34     box item //~ ERROR associated type `<T as Iter>::Item` may not live long enough
35 }
36
37 fn bad3<'a, T: Iter>(v: T) -> Box<X+'a>
38 {
39     let item = v.into_item();
40     box item //~ ERROR associated type `<T as Iter>::Item` may not live long enough
41 }
42
43 fn bad4<'a, T: Iter>(v: T) -> Box<X+'a>
44     where Box<T::Item> : X
45 {
46     let item = box v.into_item();
47     box item //~ ERROR associated type `<T as Iter>::Item` may not live long enough
48 }
49
50 fn ok1<'a, T: Iter>(v: T) -> Box<X+'a>
51     where T::Item : 'a
52 {
53     let item = v.into_item();
54     box item // OK, T::Item : 'a is declared
55 }
56
57 fn ok2<'a, T: Iter>(v: &T, w: &'a T::Item) -> Box<X+'a>
58     where T::Item : Clone
59 {
60     let item = Clone::clone(w);
61     box item // OK, T::Item : 'a is implied
62 }
63
64 fn ok3<'a, T: Iter>(v: &'a T) -> Box<X+'a>
65     where T::Item : Clone + 'a
66 {
67     let item = Clone::clone(v.as_item());
68     box item // OK, T::Item : 'a was declared
69 }
70
71 fn meh1<'a, T: Iter>(v: &'a T) -> Box<X+'a>
72     where T::Item : Clone
73 {
74     // This case is kind of interesting. It's the same as `ok3` but
75     // without the explicit declaration. In principle, it seems like
76     // we ought to be able to infer that `T::Item : 'a` because we
77     // invoked `v.as_self()` which yielded a value of type `&'a
78     // T::Item`. But we're not that smart at present.
79
80     let item = Clone::clone(v.as_item());
81     box item //~ ERROR associated type `<T as Iter>::Item` may not live
82 }
83
84 fn main() {}
85