]> git.lizzy.rs Git - rust.git/blob - src/test/ui/borrowck/mutability-errors.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / borrowck / mutability-errors.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // All the possible mutability error cases.
12
13 #![allow(unused)]
14
15 type MakeRef = fn() -> &'static (i32,);
16 type MakePtr = fn() -> *const (i32,);
17
18 fn named_ref(x: &(i32,)) {
19     *x = (1,); //~ ERROR
20     x.0 = 1; //~ ERROR
21     &mut *x; //~ ERROR
22     &mut x.0; //~ ERROR
23 }
24
25 fn unnamed_ref(f: MakeRef) {
26     *f() = (1,); //~ ERROR
27     f().0 = 1; //~ ERROR
28     &mut *f(); //~ ERROR
29     &mut f().0; //~ ERROR
30 }
31
32 unsafe fn named_ptr(x: *const (i32,)) {
33     *x = (1,); //~ ERROR
34     (*x).0 = 1; //~ ERROR
35     &mut *x; //~ ERROR
36     &mut (*x).0; //~ ERROR
37 }
38
39 unsafe fn unnamed_ptr(f: MakePtr) {
40     *f() = (1,); //~ ERROR
41     (*f()).0 = 1; //~ ERROR
42     &mut *f(); //~ ERROR
43     &mut (*f()).0; //~ ERROR
44 }
45
46 fn fn_ref<F: Fn()>(f: F) -> F { f }
47
48 fn ref_closure(mut x: (i32,)) {
49     fn_ref(|| {
50         x = (1,); //~ ERROR
51         x.0 = 1; //~ ERROR
52         &mut x; //~ ERROR
53         &mut x.0; //~ ERROR
54     });
55     fn_ref(move || {
56         x = (1,); //~ ERROR
57         x.0 = 1; //~ ERROR
58         &mut x; //~ ERROR
59         &mut x.0; //~ ERROR
60     });
61 }
62
63 fn imm_local(x: (i32,)) {
64     &mut x; //~ ERROR
65     &mut x.0; //~ ERROR
66 }
67
68 fn imm_capture(x: (i32,)) {
69     || { //~ ERROR
70         x = (1,);
71         x.0 = 1;
72         &mut x;
73         &mut x.0;
74     };
75     move || {
76         x = (1,); //~ ERROR
77         x.0 = 1; //~ ERROR
78         &mut x; //~ ERROR
79         &mut x.0; //~ ERROR
80     };
81 }
82
83 static X: (i32,) = (0,);
84
85 fn imm_static() {
86     X = (1,); //~ ERROR
87     X.0 = 1; //~ ERROR
88     &mut X; //~ ERROR
89     &mut X.0; //~ ERROR
90 }
91
92 fn main() {}