summaryrefslogtreecommitdiffstats
path: root/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.rs
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.rs')
-rw-r--r--tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.rs136
1 files changed, 136 insertions, 0 deletions
diff --git a/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.rs b/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.rs
new file mode 100644
index 000000000..6bc0d346c
--- /dev/null
+++ b/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.rs
@@ -0,0 +1,136 @@
+enum Option<T> {
+ None,
+ Some(T),
+}
+
+fn main() {
+ match &mut Some(1) {
+ ref mut z @ &mut Some(ref a) => {
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ **z = None;
+ println!("{}", *a);
+ }
+ _ => ()
+ }
+
+ struct U;
+
+ // Prevent promotion:
+ fn u() -> U { U }
+
+ fn f1(ref a @ ref mut b: U) {}
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ fn f2(ref mut a @ ref b: U) {}
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ fn f3(ref a @ [ref b, ref mut mid @ .., ref c]: [U; 4]) {}
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ fn f4_also_moved(ref a @ ref mut b @ c: U) {}
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot move out of value because it is borrowed
+ //~| ERROR borrow of moved value
+
+ let ref mut a @ (ref b @ ref mut c) = u(); // sub-in-sub
+ //~^ ERROR cannot borrow value as mutable more than once at a time
+ //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
+
+ let ref a @ ref mut b = U;
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ let ref mut a @ ref b = U;
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ let ref a @ (ref mut b, ref mut c) = (U, U);
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ let ref mut a @ (ref b, ref c) = (U, U);
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+
+ let ref mut a @ ref b = u();
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ *a = u();
+ drop(b);
+ let ref a @ ref mut b = u();
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ *b = u();
+ drop(a);
+
+ let ref mut a @ ref b = U;
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ *a = U;
+ drop(b);
+ let ref a @ ref mut b = U;
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ *b = U;
+ drop(a);
+
+ match Ok(U) {
+ ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) => {
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ *a = Err(U);
+ drop(b);
+ }
+ }
+
+ match Ok(U) {
+ ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) => {
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ *b = U;
+ drop(a);
+ }
+ }
+
+ match Ok(U) {
+ ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) if { *b = U; false } => {}
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot assign to `*b`, as it is immutable for the pattern guard
+ _ => {}
+ }
+ match Ok(U) {
+ ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) if { *a = Err(U); false } => {}
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ //~| ERROR cannot assign to `*a`, as it is immutable for the pattern guard
+ _ => {}
+ }
+ match Ok(U) {
+ ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) if { drop(b); false } => {}
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot move out of `b` in pattern guard
+ //~| ERROR cannot move out of `b` in pattern guard
+ _ => {}
+ }
+ match Ok(U) {
+ ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) if { drop(a); false } => {}
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ //~| ERROR cannot move out of `a` in pattern guard
+ //~| ERROR cannot move out of `a` in pattern guard
+ _ => {}
+ }
+
+ let ref a @ (ref mut b, ref mut c) = (U, U);
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ *b = U;
+ *c = U;
+
+ let ref a @ (ref mut b, ref mut c) = (U, U);
+ //~^ ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ //~| ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ *b = U;
+ drop(a);
+
+ let ref a @ (ref mut b, ref mut c) = (U, U);
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+ *b = U; //~| ERROR cannot borrow value as mutable because it is also borrowed as immutable
+ *c = U;
+ drop(a);
+ let ref mut a @ (ref b, ref c) = (U, U);
+ //~^ ERROR cannot borrow value as immutable because it is also borrowed as mutable
+}