summaryrefslogtreecommitdiffstats
path: root/tests/ui/borrowck/two-phase-activation-sharing-interference.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/borrowck/two-phase-activation-sharing-interference.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/borrowck/two-phase-activation-sharing-interference.rs')
-rw-r--r--tests/ui/borrowck/two-phase-activation-sharing-interference.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/ui/borrowck/two-phase-activation-sharing-interference.rs b/tests/ui/borrowck/two-phase-activation-sharing-interference.rs
new file mode 100644
index 000000000..8b880ff64
--- /dev/null
+++ b/tests/ui/borrowck/two-phase-activation-sharing-interference.rs
@@ -0,0 +1,65 @@
+// revisions: nll_target
+
+// The following revisions are disabled due to missing support from two-phase beyond autorefs
+//[nll_beyond] compile-flags: -Z two-phase-beyond-autoref
+
+// This is an important corner case pointed out by Niko: one is
+// allowed to initiate a shared borrow during a reservation, but it
+// *must end* before the activation occurs.
+//
+// FIXME: for clarity, diagnostics for these cases might be better off
+// if they specifically said "cannot activate mutable borrow of `x`"
+//
+// The convention for the listed revisions: "lxl" means lexical
+// lifetimes (which can be easier to reason about). "nll" means
+// non-lexical lifetimes. "nll_target" means the initial conservative
+// two-phase borrows that only applies to autoref-introduced borrows.
+// "nll_beyond" means the generalization of two-phase borrows to all
+// `&mut`-borrows (doing so makes it easier to write code for specific
+// corner cases).
+
+#![allow(dead_code)]
+
+fn read(_: &i32) { }
+
+fn ok() {
+ let mut x = 3;
+ let y = &mut x;
+ { let z = &x; read(z); }
+ //[nll_target]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
+ *y += 1;
+}
+
+fn not_ok() {
+ let mut x = 3;
+ let y = &mut x;
+ let z = &x;
+ //[nll_target]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
+ *y += 1;
+ //[lxl_beyond]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
+ //[nll_beyond]~^^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
+ read(z);
+}
+
+fn should_be_ok_with_nll() {
+ let mut x = 3;
+ let y = &mut x;
+ let z = &x;
+ //[nll_target]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
+ read(z);
+ *y += 1;
+ //[lxl_beyond]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
+ // (okay with (generalized) nll today)
+}
+
+fn should_also_eventually_be_ok_with_nll() {
+ let mut x = 3;
+ let y = &mut x;
+ let _z = &x;
+ //[nll_target]~^ ERROR cannot borrow `x` as immutable because it is also borrowed as mutable
+ *y += 1;
+ //[lxl_beyond]~^ ERROR cannot borrow `x` as mutable because it is also borrowed as immutable
+ // (okay with (generalized) nll today)
+}
+
+fn main() { }