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