summaryrefslogtreecommitdiffstats
path: root/src/test/ui/borrowck/borrowck-lend-flow-loop.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/borrowck/borrowck-lend-flow-loop.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/borrowck/borrowck-lend-flow-loop.rs')
-rw-r--r--src/test/ui/borrowck/borrowck-lend-flow-loop.rs132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/test/ui/borrowck/borrowck-lend-flow-loop.rs b/src/test/ui/borrowck/borrowck-lend-flow-loop.rs
new file mode 100644
index 000000000..548ffbd51
--- /dev/null
+++ b/src/test/ui/borrowck/borrowck-lend-flow-loop.rs
@@ -0,0 +1,132 @@
+fn borrow(_v: &isize) {}
+fn borrow_mut(_v: &mut isize) {}
+fn cond() -> bool { panic!() }
+fn produce<T>() -> T { panic!(); }
+
+
+fn inc(v: &mut Box<isize>) {
+ *v = Box::new(**v + 1);
+}
+
+
+fn loop_overarching_alias_mut() {
+ // In this instance, the borrow ends on the line before the loop
+
+ let mut v: Box<_> = Box::new(3);
+ let mut x = &mut v;
+ **x += 1;
+ loop {
+ borrow(&*v); // OK
+ }
+}
+
+fn block_overarching_alias_mut() {
+ // In this instance, the borrow encompasses the entire closure call.
+
+ let mut v: Box<_> = Box::new(3);
+ let mut x = &mut v;
+ for _ in 0..3 {
+ borrow(&*v); //~ ERROR cannot borrow
+ }
+ *x = Box::new(5);
+}
+fn loop_aliased_mut() {
+ // In this instance, the borrow ends right after each assignment to _x
+
+ let mut v: Box<_> = Box::new(3);
+ let mut w: Box<_> = Box::new(4);
+ let mut _x = &w;
+ loop {
+ borrow_mut(&mut *v); // OK
+ _x = &v;
+ }
+}
+
+fn while_aliased_mut() {
+ // In this instance, the borrow ends right after each assignment to _x
+
+ let mut v: Box<_> = Box::new(3);
+ let mut w: Box<_> = Box::new(4);
+ let mut _x = &w;
+ while cond() {
+ borrow_mut(&mut *v); // OK
+ _x = &v;
+ }
+}
+
+
+fn loop_aliased_mut_break() {
+ // In this instance, the borrow ends right after each assignment to _x
+
+ let mut v: Box<_> = Box::new(3);
+ let mut w: Box<_> = Box::new(4);
+ let mut _x = &w;
+ loop {
+ borrow_mut(&mut *v);
+ _x = &v;
+ break;
+ }
+ borrow_mut(&mut *v); // OK
+}
+
+fn while_aliased_mut_break() {
+ // In this instance, the borrow ends right after each assignment to _x
+
+ let mut v: Box<_> = Box::new(3);
+ let mut w: Box<_> = Box::new(4);
+ let mut _x = &w;
+ while cond() {
+ borrow_mut(&mut *v);
+ _x = &v;
+ break;
+ }
+ borrow_mut(&mut *v); // OK
+}
+
+fn while_aliased_mut_cond(cond: bool, cond2: bool) {
+ let mut v: Box<_> = Box::new(3);
+ let mut w: Box<_> = Box::new(4);
+ let mut x = &mut w;
+ while cond {
+ **x += 1;
+ borrow(&*v); //~ ERROR cannot borrow
+ if cond2 {
+ x = &mut v; // OK
+ }
+ }
+}
+fn loop_break_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F) where
+ F: FnMut(&'r mut usize) -> bool,
+{
+ // Here we check that when you break out of an inner loop, the
+ // borrows that go out of scope as you exit the inner loop are
+ // removed from the bitset.
+
+ while cond() {
+ while cond() {
+ // this borrow is limited to the scope of `r`...
+ let r: &'r mut usize = produce();
+ if !f(&mut *r) {
+ break; // ...so it is not live as exit the `while` loop here
+ }
+ }
+ }
+}
+
+fn loop_loop_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F)
+ where F: FnMut(&'r mut usize) -> bool
+{
+ // Similar to `loop_break_pops_scopes` but for the `loop` keyword
+
+ while cond() {
+ while cond() {
+ // this borrow is limited to the scope of `r`...
+ let r: &'r mut usize = produce();
+ if !f(&mut *r) {
+ continue; // ...so it is not live as exit (and re-enter) the `while` loop here
+ }
+ }
+ }
+}
+
+fn main() {}