]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/mutability-errors.rs
Move /src/test to /tests
[rust.git] / tests / ui / borrowck / mutability-errors.rs
1 // All the possible mutability error cases.
2
3 #![allow(unused)]
4
5 type MakeRef = fn() -> &'static (i32,);
6 type MakePtr = fn() -> *const (i32,);
7
8 fn named_ref(x: &(i32,)) {
9     *x = (1,); //~ ERROR
10     x.0 = 1; //~ ERROR
11     &mut *x; //~ ERROR
12     &mut x.0; //~ ERROR
13 }
14
15 fn unnamed_ref(f: MakeRef) {
16     *f() = (1,); //~ ERROR
17     f().0 = 1; //~ ERROR
18     &mut *f(); //~ ERROR
19     &mut f().0; //~ ERROR
20 }
21
22 unsafe fn named_ptr(x: *const (i32,)) {
23     *x = (1,); //~ ERROR
24     (*x).0 = 1; //~ ERROR
25     &mut *x; //~ ERROR
26     &mut (*x).0; //~ ERROR
27 }
28
29 unsafe fn unnamed_ptr(f: MakePtr) {
30     *f() = (1,); //~ ERROR
31     (*f()).0 = 1; //~ ERROR
32     &mut *f(); //~ ERROR
33     &mut (*f()).0; //~ ERROR
34 }
35
36 fn fn_ref<F: Fn()>(f: F) -> F { f }
37
38 fn ref_closure(mut x: (i32,)) {
39     fn_ref(|| {
40         x = (1,); //~ ERROR
41         x.0 = 1; //~ ERROR
42         &mut x; //~ ERROR
43         &mut x.0; //~ ERROR
44     });
45     fn_ref(move || {
46         x = (1,); //~ ERROR
47         x.0 = 1; //~ ERROR
48         &mut x; //~ ERROR
49         &mut x.0; //~ ERROR
50     });
51 }
52
53 fn imm_local(x: (i32,)) { //~ ERROR
54     &mut x;
55     &mut x.0;
56 }
57
58 fn imm_capture(x: (i32,)) {
59     || {
60         x = (1,); //~ ERROR
61         x.0 = 1; //~ ERROR
62         &mut x; //~ ERROR
63         &mut x.0; //~ ERROR
64     };
65     move || {
66         x = (1,); //~ ERROR
67         x.0 = 1; //~ ERROR
68         &mut x; //~ ERROR
69         &mut x.0; //~ ERROR
70     };
71 }
72
73 static X: (i32,) = (0,);
74
75 fn imm_static() {
76     X = (1,); //~ ERROR
77     X.0 = 1; //~ ERROR
78     &mut X; //~ ERROR
79     &mut X.0; //~ ERROR
80 }
81
82 fn main() {}