]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / issue-55492-borrowck-migrate-scans-parents.rs
1 // rust-lang/rust#55492: errors detected during MIR-borrowck's
2 // analysis of a closure body may only be caught when AST-borrowck
3 // looks at some parent.
4
5 // transcribed from borrowck-closures-unique.rs
6 mod borrowck_closures_unique {
7     pub fn e(x: &'static mut isize) {
8         static mut Y: isize = 3;
9         let mut c1 = |y: &'static mut isize| x = y;
10         //~^ ERROR is not declared as mutable
11         unsafe { c1(&mut Y); }
12     }
13 }
14
15 mod borrowck_closures_unique_grandparent {
16     pub fn ee(x: &'static mut isize) {
17         static mut Z: isize = 3;
18         let mut c1 = |z: &'static mut isize| {
19             let mut c2 = |y: &'static mut isize| x = y;
20         //~^ ERROR is not declared as mutable
21             c2(z);
22         };
23         unsafe { c1(&mut Z); }
24     }
25 }
26
27 // adapted from mutability_errors.rs
28 mod mutability_errors {
29     pub fn capture_assign_whole(x: (i32,)) {
30         || { x = (1,); };
31         //~^ ERROR is not declared as mutable
32     }
33     pub fn capture_assign_part(x: (i32,)) {
34         || { x.0 = 1; };
35         //~^ ERROR is not declared as mutable
36     }
37     pub fn capture_reborrow_whole(x: (i32,)) {
38         || { &mut x; };
39         //~^ ERROR is not declared as mutable
40     }
41     pub fn capture_reborrow_part(x: (i32,)) {
42         || { &mut x.0; };
43         //~^ ERROR is not declared as mutable
44     }
45 }
46
47 fn main() {
48     static mut X: isize = 2;
49     unsafe { borrowck_closures_unique::e(&mut X); }
50
51     mutability_errors::capture_assign_whole((1000,));
52     mutability_errors::capture_assign_part((2000,));
53     mutability_errors::capture_reborrow_whole((3000,));
54     mutability_errors::capture_reborrow_part((4000,));
55 }