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.
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.
12 // Tests that you can use an early-bound lifetime parameter as
13 // on of the generic parameters in a trait.
15 #![feature(box_syntax)]
18 fn long(&'a self) -> isize;
19 fn short<'b>(&'b self) -> isize;
22 fn poly_invoke<'c, T: Trait<'c>>(x: &'c T) -> (isize, isize) {
28 fn object_invoke1<'d>(x: &'d Trait<'d>) -> (isize, isize) {
38 fn field_invoke1<'f, 'g>(x: &'g Struct1<'f>) -> (isize,isize) {
44 struct Struct2<'h, 'i:'h> {
48 fn object_invoke2<'j, 'k>(x: &'k Trait<'j>) -> isize {
52 fn field_invoke2<'l, 'm, 'n>(x: &'n Struct2<'l,'m>) -> isize {
60 fn make_val<T:MakerTrait>() -> T {
64 trait RefMakerTrait<'q> {
65 fn mk(_: Self) -> &'q Self;
68 fn make_ref<'r, T:RefMakerTrait<'r>>(t:T) -> &'r T {
72 impl<'s> Trait<'s> for (isize,isize) {
73 fn long(&'s self) -> isize {
77 fn short<'b>(&'b self) -> isize {
83 impl<'t> MakerTrait for Box<Trait<'t>+'static> {
84 fn mk() -> Box<Trait<'t>+'static> {
85 let tup: Box<(isize, isize)> = box (4,5);
91 Cons(isize, &'l List<'l>),
96 fn car<'m>(&'m self) -> isize {
98 &List::Cons(car, _) => car,
99 &List::Null => panic!(),
102 fn cdr<'n>(&'n self) -> &'l List<'l> {
104 &List::Cons(_, cdr) => cdr,
105 &List::Null => panic!(),
110 impl<'t> RefMakerTrait<'t> for List<'t> {
111 fn mk(l:List<'t>) -> &'t List<'t> {
118 let o = &t as &Trait;
119 let s1 = Struct1 { f: o };
120 let s2 = Struct2 { f: o };
121 assert_eq!(poly_invoke(&t), (2,3));
122 assert_eq!(object_invoke1(&t), (2,3));
123 assert_eq!(field_invoke1(&s1), (2,3));
124 assert_eq!(object_invoke2(&t), 3);
125 assert_eq!(field_invoke2(&s2), 3);
127 let m : Box<Trait> = make_val();
128 // assert_eq!(object_invoke1(&*m), (4,5));
129 // ~~~~~~~~~~~~~~~~~~~
130 // this call yields a compilation error; see compile-fail/dropck-object-cycle.rs
132 assert_eq!(object_invoke2(&*m), 5);
134 // The RefMakerTrait above is pretty strange (i.e. it is strange
135 // to consume a value of type T and return a &T). Easiest thing
136 // that came to my mind: consume a cell of a linked list and
137 // return a reference to the list it points to.
139 let l1 = List::Cons(1, &l0);
140 let l2 = List::Cons(2, &l1);
142 let r = make_ref(l2);
143 assert_eq!(rl1.car(), r.car());