]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/regions-dependent-addr-of.rs
fallout: run-pass tests that use box. (many could be ported to `Box::new` instead...
[rust.git] / src / test / run-pass / regions-dependent-addr-of.rs
1 // Copyright 2012 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 // Test lifetimes are linked properly when we create dependent region pointers.
12 // Issue #3148.
13
14 #![allow(unknown_features)]
15 #![feature(box_syntax)]
16
17 struct A {
18     value: B
19 }
20
21 struct B {
22     v1: int,
23     v2: [int; 3],
24     v3: Vec<int> ,
25     v4: C,
26     v5: Box<C>,
27     v6: Option<C>
28 }
29
30 struct C {
31     f: int
32 }
33
34 impl Copy for C {}
35
36 fn get_v1(a: &A) -> &int {
37     // Region inferencer must deduce that &v < L2 < L1
38     let foo = &a.value; // L1
39     &foo.v1             // L2
40 }
41
42 fn get_v2(a: &A, i: uint) -> &int {
43     let foo = &a.value;
44     &foo.v2[i]
45 }
46
47 fn get_v3(a: &A, i: uint) -> &int {
48     let foo = &a.value;
49     &foo.v3[i]
50 }
51
52 fn get_v4(a: &A, _i: uint) -> &int {
53     let foo = &a.value;
54     &foo.v4.f
55 }
56
57 fn get_v5(a: &A, _i: uint) -> &int {
58     let foo = &a.value;
59     &foo.v5.f
60 }
61
62 fn get_v6_a(a: &A, _i: uint) -> &int {
63     match a.value.v6 {
64         Some(ref v) => &v.f,
65         None => panic!()
66     }
67 }
68
69 fn get_v6_b(a: &A, _i: uint) -> &int {
70     match *a {
71         A { value: B { v6: Some(ref v), .. } } => &v.f,
72         _ => panic!()
73     }
74 }
75
76 fn get_v6_c(a: &A, _i: uint) -> &int {
77     match a {
78         &A { value: B { v6: Some(ref v), .. } } => &v.f,
79         _ => panic!()
80     }
81 }
82
83 fn get_v5_ref(a: &A, _i: uint) -> &int {
84     match &a.value {
85         &B {v5: box C {f: ref v}, ..} => v
86     }
87 }
88
89 pub fn main() {
90     let a = A {value: B {v1: 22,
91                          v2: [23, 24, 25],
92                          v3: vec!(26, 27, 28),
93                          v4: C { f: 29 },
94                          v5: box C { f: 30 },
95                          v6: Some(C { f: 31 })}};
96
97     let p = get_v1(&a);
98     assert_eq!(*p, a.value.v1);
99
100     let p = get_v2(&a, 1);
101     assert_eq!(*p, a.value.v2[1]);
102
103     let p = get_v3(&a, 1);
104     assert_eq!(*p, a.value.v3[1]);
105
106     let p = get_v4(&a, 1);
107     assert_eq!(*p, a.value.v4.f);
108
109     let p = get_v5(&a, 1);
110     assert_eq!(*p, a.value.v5.f);
111
112     let p = get_v6_a(&a, 1);
113     assert_eq!(*p, a.value.v6.unwrap().f);
114
115     let p = get_v6_b(&a, 1);
116     assert_eq!(*p, a.value.v6.unwrap().f);
117
118     let p = get_v6_c(&a, 1);
119     assert_eq!(*p, a.value.v6.unwrap().f);
120
121     let p = get_v5_ref(&a, 1);
122     assert_eq!(*p, a.value.v5.f);
123 }