summaryrefslogtreecommitdiffstats
path: root/src/test/ui/borrowck/borrowck-describe-lvalue.rs
blob: cdcff69d6e529293998ce23f69128630dd1aafec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
pub struct Foo {
  x: u32
}

pub struct Bar(u32);

pub enum Baz {
    X(u32)
}

union U {
    a: u8,
    b: u64,
}

impl Foo {
  fn x(&mut self) -> &mut u32 { &mut self.x }
}

impl Bar {
    fn x(&mut self) -> &mut u32 { &mut self.0 }
}

impl Baz {
    fn x(&mut self) -> &mut u32 {
        match *self {
            Baz::X(ref mut value) => value
        }
    }
}

fn main() {
    // Local and field from struct
    {
        let mut f = Foo { x: 22 };
        let x = f.x();
        f.x; //~ ERROR cannot use `f.x` because it was mutably borrowed
        drop(x);
    }
    // Local and field from tuple-struct
    {
        let mut g = Bar(22);
        let x = g.x();
        g.0; //~ ERROR cannot use `g.0` because it was mutably borrowed
        drop(x);
    }
    // Local and field from tuple
    {
        let mut h = (22, 23);
        let x = &mut h.0;
        h.0; //~ ERROR cannot use `h.0` because it was mutably borrowed
        drop(x);
    }
    // Local and field from enum
    {
        let mut e = Baz::X(2);
        let x = e.x();
        match e {
            Baz::X(value) => value //~ ERROR cannot use `e.0` because it was mutably borrowed
        };
        drop(x);
    }
    // Local and field from union
    unsafe {
        let mut u = U { b: 0 };
        let x = &mut u.a;
        u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed
        drop(x);
    }
    // Deref and field from struct
    {
        let mut f = Box::new(Foo { x: 22 });
        let x = f.x();
        f.x; //~ ERROR cannot use `f.x` because it was mutably borrowed
        drop(x);
    }
    // Deref and field from tuple-struct
    {
        let mut g = Box::new(Bar(22));
        let x = g.x();
        g.0; //~ ERROR cannot use `g.0` because it was mutably borrowed
        drop(x);
    }
    // Deref and field from tuple
    {
        let mut h = Box::new((22, 23));
        let x = &mut h.0;
        h.0; //~ ERROR cannot use `h.0` because it was mutably borrowed
        drop(x);
    }
    // Deref and field from enum
    {
        let mut e = Box::new(Baz::X(3));
        let x = e.x();
        match *e {
            Baz::X(value) => value
            //~^ ERROR cannot use `e.0` because it was mutably borrowed
        };
        drop(x);
    }
    // Deref and field from union
    unsafe {
        let mut u = Box::new(U { b: 0 });
        let x = &mut u.a;
        u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed
        drop(x);
    }
    // Constant index
    {
        let mut v = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        let x = &mut v;
        match v {
            &[x, _, .., _, _] => println!("{}", x),
                //~^ ERROR cannot use `v[..]` because it was mutably borrowed
                            _ => panic!("other case"),
        }
        match v {
            &[_, x, .., _, _] => println!("{}", x),
                //~^ ERROR cannot use `v[..]` because it was mutably borrowed
                            _ => panic!("other case"),
        }
        match v {
            &[_, _, .., x, _] => println!("{}", x),
                //~^ ERROR cannot use `v[..]` because it was mutably borrowed
                            _ => panic!("other case"),
        }
        match v {
            &[_, _, .., _, x] => println!("{}", x),
                //~^ ERROR cannot use `v[..]` because it was mutably borrowed
                            _ => panic!("other case"),
        }
        drop(x);
    }
    // Subslices
    {
        let mut v = &[1, 2, 3, 4, 5];
        let x = &mut v;
        match v {
            &[x @ ..] => println!("{:?}", x),
                //~^ ERROR cannot use `v[..]` because it was mutably borrowed
            _ => panic!("other case"),
        }
        match v {
            &[_, x @ ..] => println!("{:?}", x),
                //~^ ERROR cannot use `v[..]` because it was mutably borrowed
            _ => panic!("other case"),
        }
        match v {
            &[x @ .., _] => println!("{:?}", x),
                //~^ ERROR cannot use `v[..]` because it was mutably borrowed
            _ => panic!("other case"),
        }
        match v {
            &[_, x @ .., _] => println!("{:?}", x),
                //~^ ERROR cannot use `v[..]` because it was mutably borrowed
            _ => panic!("other case"),
        }
        drop(x);
    }
    // Downcasted field
    {
        enum E<X> { A(X), B { x: X } }

        let mut e = E::A(3);
        let x = &mut e;
        match e {
            //~^ ERROR cannot use `e` because it was mutably borrowed
            E::A(ref ax) =>
                //~^ ERROR cannot borrow `e.0` as immutable because it is also borrowed as mutable
                println!("e.ax: {:?}", ax),
            E::B { x: ref bx } =>
                //~^ ERROR cannot borrow `e.x` as immutable because it is also borrowed as mutable
                println!("e.bx: {:?}", bx),
        }
        drop(x);
    }
    // Field in field
    {
        struct F { x: u32, y: u32 };
        struct S { x: F, y: (u32, u32), };
        let mut s = S { x: F { x: 1, y: 2}, y: (999, 998) };
        let x = &mut s;
        match s {
            S  { y: (ref y0, _), .. } =>
                //~^ ERROR cannot borrow `s.y.0` as immutable because it is also borrowed as mutable
                println!("y0: {:?}", y0),
            _ => panic!("other case"),
        }
        match s {
            S  { x: F { y: ref x0, .. }, .. } =>
                //~^ ERROR cannot borrow `s.x.y` as immutable because it is also borrowed as mutable
                println!("x0: {:?}", x0),
            _ => panic!("other case"),
        }
        drop(x);
    }
    // Field of ref
    {
        struct Block<'a> {
            current: &'a u8,
            unrelated: &'a u8,
        };

        fn bump<'a>(mut block: &mut Block<'a>) {
            let x = &mut block;
            let p: &'a u8 = &*block.current;
            //~^ ERROR cannot borrow `*block.current` as immutable because it is also borrowed as mutable
            // See issue rust#38899
            drop(x);
        }
    }
    // Field of ptr
    {
        struct Block2 {
            current: *const u8,
            unrelated: *const u8,
        }

        unsafe fn bump2(mut block: *mut Block2) {
            let x = &mut block;
            let p : *const u8 = &*(*block).current;
            //~^ ERROR cannot borrow `*block.current` as immutable because it is also borrowed as mutable
            // See issue rust#38899
            drop(x);
        }
    }
    // Field of index
    {
        struct F {x: u32, y: u32};
        let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
        let x = &mut v;
        v[0].y;
        //~^ ERROR cannot use `v[_].y` because it was mutably borrowed
        //~| ERROR cannot use `*v` because it was mutably borrowed
        drop(x);
    }
    // Field of constant index
    {
        struct F {x: u32, y: u32};
        let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
        let x = &mut v;
        match v {
            &[_, F {x: ref xf, ..}] => println!("{}", xf),
            //~^ ERROR cannot borrow `v[..].x` as immutable because it is also borrowed as mutable
            _ => panic!("other case")
        }
        drop(x);
    }
    // Field from upvar
    {
        let mut x = 0;
        || {
            let y = &mut x;
            &mut x; //~ ERROR cannot borrow `x` as mutable more than once at a time
            *y = 1;
        };
    }
    // Field from upvar nested
    {
        let mut x = 0;
           || {
               || { //~ ERROR captured variable cannot escape `FnMut` closure body
                   let y = &mut x;
                   &mut x; //~ ERROR cannot borrow `x` as mutable more than once at a time
                   *y = 1;
                   drop(y);
                }
           };
    }
    {
        fn foo(x: Vec<i32>) {
            let c = || {
                drop(x);
                drop(x); //~ ERROR use of moved value: `x`
            };
            c();
        }
    }
}