summaryrefslogtreecommitdiffstats
path: root/src/test/ui/borrowck/mutability-errors.rs
blob: 5be0df1376135b91c11276ff0836b04878894188 (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
// All the possible mutability error cases.

#![allow(unused)]

type MakeRef = fn() -> &'static (i32,);
type MakePtr = fn() -> *const (i32,);

fn named_ref(x: &(i32,)) {
    *x = (1,); //~ ERROR
    x.0 = 1; //~ ERROR
    &mut *x; //~ ERROR
    &mut x.0; //~ ERROR
}

fn unnamed_ref(f: MakeRef) {
    *f() = (1,); //~ ERROR
    f().0 = 1; //~ ERROR
    &mut *f(); //~ ERROR
    &mut f().0; //~ ERROR
}

unsafe fn named_ptr(x: *const (i32,)) {
    *x = (1,); //~ ERROR
    (*x).0 = 1; //~ ERROR
    &mut *x; //~ ERROR
    &mut (*x).0; //~ ERROR
}

unsafe fn unnamed_ptr(f: MakePtr) {
    *f() = (1,); //~ ERROR
    (*f()).0 = 1; //~ ERROR
    &mut *f(); //~ ERROR
    &mut (*f()).0; //~ ERROR
}

fn fn_ref<F: Fn()>(f: F) -> F { f }

fn ref_closure(mut x: (i32,)) {
    fn_ref(|| {
        x = (1,); //~ ERROR
        x.0 = 1; //~ ERROR
        &mut x; //~ ERROR
        &mut x.0; //~ ERROR
    });
    fn_ref(move || {
        x = (1,); //~ ERROR
        x.0 = 1; //~ ERROR
        &mut x; //~ ERROR
        &mut x.0; //~ ERROR
    });
}

fn imm_local(x: (i32,)) {
    &mut x; //~ ERROR
    &mut x.0; //~ ERROR
}

fn imm_capture(x: (i32,)) {
    || {
        x = (1,); //~ ERROR
        x.0 = 1; //~ ERROR
        &mut x; //~ ERROR
        &mut x.0; //~ ERROR
    };
    move || {
        x = (1,); //~ ERROR
        x.0 = 1; //~ ERROR
        &mut x; //~ ERROR
        &mut x.0; //~ ERROR
    };
}

static X: (i32,) = (0,);

fn imm_static() {
    X = (1,); //~ ERROR
    X.0 = 1; //~ ERROR
    &mut X; //~ ERROR
    &mut X.0; //~ ERROR
}

fn main() {}