]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/diagnostics/mut_ref.rs
Rollup merge of #107264 - ferrocene:pa-private-items, r=Mark-Simulacrum
[rust.git] / tests / ui / closures / 2229_closure_analysis / diagnostics / mut_ref.rs
1 // edition:2021
2
3 // Test that we can't mutate a place if we need to deref an imm-borrow
4 // to reach it.
5
6 fn imm_mut_ref() {
7     let mut x = String::new();
8     let y = String::new();
9     let mref_x = &mut x;
10     let ref_mref_x = &mref_x;
11
12     let c = || {
13     //~^ ERROR: cannot borrow `**ref_mref_x` as mutable, as it is behind a `&` reference
14         **ref_mref_x = y;
15     };
16
17     c();
18 }
19
20 fn mut_imm_ref() {
21     let x = String::new();
22     let y = String::new();
23     let mut ref_x = &x;
24     let mref_ref_x = &mut ref_x;
25
26     let c = || {
27     //~^ ERROR: cannot borrow `**mref_ref_x` as mutable, as it is behind a `&` reference
28         **mref_ref_x = y;
29     };
30
31     c();
32 }
33
34 fn main() {
35     imm_mut_ref();
36     mut_imm_ref();
37 }