]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-issue-14498.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-issue-14498.rs
1 // This tests that we can't modify Box<&mut T> contents while they
2 // are borrowed (#14498).
3 //
4 // Also includes tests of the errors reported when the Box in question
5 // is immutable (#14270).
6
7
8
9 struct A { a: isize }
10 struct B<'a> { a: Box<&'a mut isize> }
11
12 fn indirect_write_to_imm_box() {
13     let mut x: isize = 1;
14     let y: Box<_> = Box::new(&mut x);
15     let p = &y;
16     ***p = 2; //~ ERROR cannot assign to `***p`
17     drop(p);
18 }
19
20 fn borrow_in_var_from_var() {
21     let mut x: isize = 1;
22     let mut y: Box<_> = Box::new(&mut x);
23     let p = &y;
24     let q = &***p;
25     **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
26     drop(p);
27     drop(q);
28 }
29
30 fn borrow_in_var_from_var_via_imm_box() {
31     let mut x: isize = 1;
32     let y: Box<_> = Box::new(&mut x);
33     let p = &y;
34     let q = &***p;
35     **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
36     drop(p);
37     drop(q);
38 }
39
40 fn borrow_in_var_from_field() {
41     let mut x = A { a: 1 };
42     let mut y: Box<_> = Box::new(&mut x.a);
43     let p = &y;
44     let q = &***p;
45     **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
46     drop(p);
47     drop(q);
48 }
49
50 fn borrow_in_var_from_field_via_imm_box() {
51     let mut x = A { a: 1 };
52     let y: Box<_> = Box::new(&mut x.a);
53     let p = &y;
54     let q = &***p;
55     **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
56     drop(p);
57     drop(q);
58 }
59
60 fn borrow_in_field_from_var() {
61     let mut x: isize = 1;
62     let mut y = B { a: Box::new(&mut x) };
63     let p = &y.a;
64     let q = &***p;
65     **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
66     drop(p);
67     drop(q);
68 }
69
70 fn borrow_in_field_from_var_via_imm_box() {
71     let mut x: isize = 1;
72     let y = B { a: Box::new(&mut x) };
73     let p = &y.a;
74     let q = &***p;
75     **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
76     drop(p);
77     drop(q);
78 }
79
80 fn borrow_in_field_from_field() {
81     let mut x = A { a: 1 };
82     let mut y = B { a: Box::new(&mut x.a) };
83     let p = &y.a;
84     let q = &***p;
85     **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
86     drop(p);
87     drop(q);
88 }
89
90 fn borrow_in_field_from_field_via_imm_box() {
91     let mut x = A { a: 1 };
92     let y = B { a: Box::new(&mut x.a) };
93     let p = &y.a;
94     let q = &***p;
95     **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
96     drop(p);
97     drop(q);
98 }
99
100 fn main() {
101     indirect_write_to_imm_box();
102     borrow_in_var_from_var();
103     borrow_in_var_from_var_via_imm_box();
104     borrow_in_var_from_field();
105     borrow_in_var_from_field_via_imm_box();
106     borrow_in_field_from_var();
107     borrow_in_field_from_var_via_imm_box();
108     borrow_in_field_from_field();
109     borrow_in_field_from_field_via_imm_box();
110 }