File: borrowck-access-permissions.rs

package info (click to toggle)
rustc 1.89.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 906,624 kB
  • sloc: xml: 158,148; python: 34,888; javascript: 19,595; sh: 19,221; ansic: 13,046; cpp: 7,144; asm: 4,376; makefile: 692; lisp: 174; sql: 15
file content (61 lines) | stat: -rw-r--r-- 1,381 bytes parent folder | download | duplicates (14)
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
static static_x: i32 = 1;
static mut static_x_mut: i32 = 1;

fn main() {
    let x = 1;
    let mut x_mut = 1;

    {
        // borrow of local
        let _y1 = &mut x; //~ ERROR [E0596]
        let _y2 = &mut x_mut; // No error
    }

    {
        // borrow of static
        let _y1 = &mut static_x; //~ ERROR [E0596]
        unsafe {
            let _y2 = &mut static_x_mut;
        }
    }

    {
        // borrow of deref to box
        let box_x = Box::new(1);
        let mut box_x_mut = Box::new(1);

        let _y1 = &mut *box_x; //~ ERROR [E0596]
        let _y2 = &mut *box_x_mut; // No error
    }

    {
        // borrow of deref to reference
        let ref_x = &x;
        let ref_x_mut = &mut x_mut;

        let _y1 = &mut *ref_x; //~ ERROR [E0596]
        let _y2 = &mut *ref_x_mut; // No error
    }

    {
        // borrow of deref to pointer
        let ptr_x: *const _ = &x;
        let ptr_mut_x: *mut _ = &mut x_mut;

        unsafe {
            let _y1 = &mut *ptr_x; //~ ERROR [E0596]
            let _y2 = &mut *ptr_mut_x; // No error
        }
    }

    {
        // borrowing mutably through an immutable reference
        struct Foo<'a> {
            f: &'a mut i32,
            g: &'a i32,
        };
        let mut foo = Foo { f: &mut x_mut, g: &x };
        let foo_ref = &foo;
        let _y = &mut *foo_ref.f; //~ ERROR [E0596]
    }
}