]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/regions-early-bound-trait-param.rs
Ignore tests broken by failing on ICE
[rust.git] / src / test / run-pass / regions-early-bound-trait-param.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 // Tests that you can use an early-bound lifetime parameter as
12 // on of the generic parameters in a trait.
13
14 trait Trait<'a> {
15     fn long(&'a self) -> int;
16     fn short<'b>(&'b self) -> int;
17 }
18
19 fn poly_invoke<'c, T: Trait<'c>>(x: &'c T) -> (int, int) {
20     let l = x.long();
21     let s = x.short();
22     (l,s)
23 }
24
25 fn object_invoke1<'d>(x: &'d Trait<'d>) -> (int, int) {
26     let l = x.long();
27     let s = x.short();
28     (l,s)
29 }
30
31 struct Struct1<'e> {
32     f: &'e Trait<'e>
33 }
34
35 fn field_invoke1<'f, 'g>(x: &'g Struct1<'f>) -> (int,int) {
36     let l = x.f.long();
37     let s = x.f.short();
38     (l,s)
39 }
40
41 struct Struct2<'h, 'i> {
42     f: &'h Trait<'i>
43 }
44
45 fn object_invoke2<'j, 'k>(x: &'k Trait<'j>) -> int {
46     x.short()
47 }
48
49 fn field_invoke2<'l, 'm, 'n>(x: &'n Struct2<'l,'m>) -> int {
50     x.f.short()
51 }
52
53 trait MakerTrait<'o> {
54     fn mk() -> Self;
55 }
56
57 fn make_val<'p, T:MakerTrait<'p>>() -> T {
58     MakerTrait::mk()
59 }
60
61 trait RefMakerTrait<'q> {
62     fn mk(Self) -> &'q Self;
63 }
64
65 fn make_ref<'r, T:RefMakerTrait<'r>>(t:T) -> &'r T {
66     RefMakerTrait::mk(t)
67 }
68
69 impl<'s> Trait<'s> for (int,int) {
70     fn long(&'s self) -> int {
71         let &(x,_) = self;
72         x
73     }
74     fn short<'b>(&'b self) -> int {
75         let &(_,y) = self;
76         y
77     }
78 }
79
80 impl<'t> MakerTrait<'t> for ~Trait<'t> {
81     fn mk() -> ~Trait<'t> { ~(4,5) as ~Trait }
82 }
83
84 enum List<'l> {
85     Cons(int, &'l List<'l>),
86     Null
87 }
88
89 impl<'l> List<'l> {
90     fn car<'m>(&'m self) -> int {
91         match self {
92             &Cons(car, _) => car,
93             &Null => fail!(),
94         }
95     }
96     fn cdr<'n>(&'n self) -> &'l List<'l> {
97         match self {
98             &Cons(_, cdr) => cdr,
99             &Null => fail!(),
100         }
101     }
102 }
103
104 impl<'t> RefMakerTrait<'t> for List<'t> {
105     fn mk(l:List<'t>) -> &'t List<'t> {
106         l.cdr()
107     }
108 }
109
110 pub fn main() {
111     let t = (2,3);
112     let o = &t as &Trait;
113     let s1 = Struct1 { f: o };
114     let s2 = Struct2 { f: o };
115     assert_eq!(poly_invoke(&t), (2,3));
116     assert_eq!(object_invoke1(&t), (2,3));
117     assert_eq!(field_invoke1(&s1), (2,3));
118     assert_eq!(object_invoke2(&t), 3);
119     assert_eq!(field_invoke2(&s2), 3);
120
121     let m : ~Trait = make_val();
122     assert_eq!(object_invoke1(m), (4,5));
123     assert_eq!(object_invoke2(m), 5);
124
125     // The RefMakerTrait above is pretty strange (i.e. it is strange
126     // to consume a value of type T and return a &T).  Easiest thing
127     // that came to my mind: consume a cell of a linked list and
128     // return a reference to the list it points to.
129     let l0 = Null;
130     let l1 = Cons(1, &l0);
131     let l2 = Cons(2, &l1);
132     let rl1 = &l1;
133     let r  = make_ref(l2);
134     assert_eq!(rl1.car(), r.car());
135 }