summaryrefslogtreecommitdiffstats
path: root/src/test/ui/borrowck/borrowck-field-sensitivity.rs
blob: 03edf445ee97b41cde5111d7c54b7e1ca36617be (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
struct A { a: isize, b: Box<isize> }



fn deref_after_move() {
    let x = A { a: 1, b: Box::new(2) };
    drop(x.b);
    drop(*x.b); //~ ERROR use of moved value: `x.b`
}

fn deref_after_fu_move() {
    let x = A { a: 1, b: Box::new(2) };
    let y = A { a: 3, .. x };
    drop(*x.b); //~ ERROR use of moved value: `x.b`
}

fn borrow_after_move() {
    let x = A { a: 1, b: Box::new(2) };
    drop(x.b);
    let p = &x.b; //~ ERROR borrow of moved value: `x.b`
    drop(**p);
}

fn borrow_after_fu_move() {
    let x = A { a: 1, b: Box::new(2) };
    let _y = A { a: 3, .. x };
    let p = &x.b; //~ ERROR borrow of moved value: `x.b`
    drop(**p);
}

fn move_after_borrow() {
    let x = A { a: 1, b: Box::new(2) };
    let p = &x.b;
    drop(x.b); //~ ERROR cannot move out of `x.b` because it is borrowed
    drop(**p);
}

fn fu_move_after_borrow() {
    let x = A { a: 1, b: Box::new(2) };
    let p = &x.b;
    let _y = A { a: 3, .. x }; //~ ERROR cannot move out of `x.b` because it is borrowed
    drop(**p);
}

fn mut_borrow_after_mut_borrow() {
    let mut x = A { a: 1, b: Box::new(2) };
    let p = &mut x.a;
    let q = &mut x.a; //~ ERROR cannot borrow `x.a` as mutable more than once at a time
    drop(*p);
    drop(*q);
}

fn move_after_move() {
    let x = A { a: 1, b: Box::new(2) };
    drop(x.b);
    drop(x.b);  //~ ERROR use of moved value: `x.b`
}

fn move_after_fu_move() {
    let x = A { a: 1, b: Box::new(2) };
    let _y = A { a: 3, .. x };
    drop(x.b);  //~ ERROR use of moved value: `x.b`
}

fn fu_move_after_move() {
    let x = A { a: 1, b: Box::new(2) };
    drop(x.b);
    let _z = A { a: 3, .. x };  //~ ERROR use of moved value: `x.b`
}

fn fu_move_after_fu_move() {
    let x = A { a: 1, b: Box::new(2) };
    let _y = A { a: 3, .. x };
    let _z = A { a: 4, .. x };  //~ ERROR use of moved value: `x.b`
}

// The following functions aren't yet accepted, but they should be.

fn copy_after_field_assign_after_uninit() {
    let mut x: A;
    x.a = 1; //~ ERROR E0381
    drop(x.a);
}

fn borrow_after_field_assign_after_uninit() {
    let mut x: A;
    x.a = 1; //~ ERROR E0381
    let p = &x.a;
    drop(*p);
}

fn move_after_field_assign_after_uninit() {
    let mut x: A;
    x.b = Box::new(1); //~ ERROR E0381
    drop(x.b);
}

fn main() {
    deref_after_move();
    deref_after_fu_move();

    borrow_after_move();
    borrow_after_fu_move();
    move_after_borrow();
    fu_move_after_borrow();
    mut_borrow_after_mut_borrow();

    move_after_move();
    move_after_fu_move();
    fu_move_after_move();
    fu_move_after_fu_move();

    copy_after_field_assign_after_uninit();
    borrow_after_field_assign_after_uninit();
    move_after_field_assign_after_uninit();
}