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