]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/two-phase-activation-sharing-interference.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / two-phase-activation-sharing-interference.rs
1 // revisions: nll_target
2
3 // The following revisions are disabled due to missing support from two-phase beyond autorefs
4 //[nll_beyond] compile-flags: -Z two-phase-beyond-autoref
5
6 // This is an important corner case pointed out by Niko: one is
7 // allowed to initiate a shared borrow during a reservation, but it
8 // *must end* before the activation occurs.
9 //
10 // FIXME: for clarity, diagnostics for these cases might be better off
11 // if they specifically said "cannot activate mutable borrow of `x`"
12 //
13 // The convention for the listed revisions: "lxl" means lexical
14 // lifetimes (which can be easier to reason about). "nll" means
15 // non-lexical lifetimes. "nll_target" means the initial conservative
16 // two-phase borrows that only applies to autoref-introduced borrows.
17 // "nll_beyond" means the generalization of two-phase borrows to all
18 // `&mut`-borrows (doing so makes it easier to write code for specific
19 // corner cases).
20
21 #![allow(dead_code)]
22
23 fn read(_: &i32) { }
24
25 fn ok() {
26     let mut x = 3;
27     let y = &mut x;
28     { let z = &x; read(z); }
29     //[nll_target]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
30     *y += 1;
31 }
32
33 fn not_ok() {
34     let mut x = 3;
35     let y = &mut x;
36     let z = &x;
37     //[nll_target]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
38     *y += 1;
39     //[lxl_beyond]~^   ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
40     //[nll_beyond]~^^  ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
41     read(z);
42 }
43
44 fn should_be_ok_with_nll() {
45     let mut x = 3;
46     let y = &mut x;
47     let z = &x;
48     //[nll_target]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
49     read(z);
50     *y += 1;
51     //[lxl_beyond]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
52     // (okay with (generalized) nll today)
53 }
54
55 fn should_also_eventually_be_ok_with_nll() {
56     let mut x = 3;
57     let y = &mut x;
58     let _z = &x;
59     //[nll_target]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
60     *y += 1;
61     //[lxl_beyond]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
62     // (okay with (generalized) nll today)
63 }
64
65 fn main() { }