]> git.lizzy.rs Git - rust.git/blob - src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.rs
Test cases for handling mutable references
[rust.git] / src / test / ui / closures / 2229_closure_analysis / diagnostics / cant-mutate-imm-borrow.rs
1 // Test that if we deref an immutable borrow to access a Place,
2 // then we can't mutate the final place.
3
4 #![feature(capture_disjoint_fields)]
5 //~^ WARNING: the feature `capture_disjoint_fields` is incomplete
6
7 fn main() {
8     let mut x = (format!(""), format!("X2"));
9     let mut y = (&x, "Y");
10     let z = (&mut y, "Z");
11
12     // `x.0` is mutable but we access `x` via `z.0.0`, which is an immutable reference and
13     // therefore can't be mutated.
14     let mut c = || {
15     //~^ ERROR: cannot borrow `z.0.0.0` as mutable, as it is behind a `&` reference
16         z.0.0.0 = format!("X1");
17         //~^ ERROR: cannot assign to `z`, as it is not declared as mutable
18     };
19
20     c();
21 }