summary refs log tree commit diff
path: root/src/test/compile-fail/borrowck-lend-flow.rs
blob: 0a489630c3811a68c32b0ace97f84bb80216af0f (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
// Note: the borrowck analysis is currently flow-insensitive.
// Therefore, some of these errors are marked as spurious and could be
// corrected by a simple change to the analysis.  The others are
// either genuine or would require more advanced changes.  The latter
// cases are noted.

fn borrow(_v: &int) {}

fn inc(v: &mut ~int) {
    *v = ~(**v + 1);
}

fn post_aliased_const() {
    let mut v = ~3;
    borrow(v);
    let _w = &const v;
}

fn post_aliased_mut() {
    // SPURIOUS--flow
    let mut v = ~3;
    borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
    let _w = &mut v; //~ NOTE prior loan as mutable granted here
}

fn post_aliased_scope(cond: bool) {
    let mut v = ~3;
    borrow(v);
    if cond { inc(&mut v); }
}

fn loop_overarching_alias_mut() {
    let mut v = ~3;
    let mut _x = &mut v; //~ NOTE prior loan as mutable granted here
    loop {
        borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
    }
}

fn block_overarching_alias_mut() {
    let mut v = ~3;
    let mut _x = &mut v; //~ NOTE prior loan as mutable granted here
    for 3.times {
        borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
    }
}

fn loop_aliased_mut() {
    let mut v = ~3, w = ~4;
    let mut _x = &mut w;
    loop {
        borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
        _x = &mut v; //~ NOTE prior loan as mutable granted here
    }
}

fn while_aliased_mut(cond: bool) {
    let mut v = ~3, w = ~4;
    let mut _x = &mut w;
    while cond {
        borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
        _x = &mut v; //~ NOTE prior loan as mutable granted here
    }
}

fn while_aliased_mut_cond(cond: bool, cond2: bool) {
    let mut v = ~3, w = ~4;
    let mut _x = &mut w;
    while cond {
        borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
        if cond2 {
            _x = &mut v; //~ NOTE prior loan as mutable granted here
        }
    }
}

fn loop_in_block() {
    let mut v = ~3, w = ~4;
    let mut _x = &mut w;
    for uint::range(0u, 10u) |_i| {
        borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
        _x = &mut v; //~ NOTE prior loan as mutable granted here
    }
}

fn at_most_once_block() {
    fn at_most_once(f: fn()) { f() }

    // Here, the borrow check has no way of knowing that the block is
    // executed at most once.

    let mut v = ~3, w = ~4;
    let mut _x = &mut w;
    do at_most_once {
        borrow(v); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
        _x = &mut v; //~ NOTE prior loan as mutable granted here
    }
}

fn main() {}