summaryrefslogtreecommitdiffstats
path: root/tests/ui/borrowck/borrowck-issue-14498.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/borrowck/borrowck-issue-14498.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/borrowck/borrowck-issue-14498.rs')
-rw-r--r--tests/ui/borrowck/borrowck-issue-14498.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/tests/ui/borrowck/borrowck-issue-14498.rs b/tests/ui/borrowck/borrowck-issue-14498.rs
new file mode 100644
index 000000000..003533a51
--- /dev/null
+++ b/tests/ui/borrowck/borrowck-issue-14498.rs
@@ -0,0 +1,110 @@
+// This tests that we can't modify Box<&mut T> contents while they
+// are borrowed (#14498).
+//
+// Also includes tests of the errors reported when the Box in question
+// is immutable (#14270).
+
+
+
+struct A { a: isize }
+struct B<'a> { a: Box<&'a mut isize> }
+
+fn indirect_write_to_imm_box() {
+ let mut x: isize = 1;
+ let y: Box<_> = Box::new(&mut x);
+ let p = &y;
+ ***p = 2; //~ ERROR cannot assign to `***p`
+ drop(p);
+}
+
+fn borrow_in_var_from_var() {
+ let mut x: isize = 1;
+ let mut y: Box<_> = Box::new(&mut x);
+ let p = &y;
+ let q = &***p;
+ **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_var_from_var_via_imm_box() {
+ let mut x: isize = 1;
+ let y: Box<_> = Box::new(&mut x);
+ let p = &y;
+ let q = &***p;
+ **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_var_from_field() {
+ let mut x = A { a: 1 };
+ let mut y: Box<_> = Box::new(&mut x.a);
+ let p = &y;
+ let q = &***p;
+ **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_var_from_field_via_imm_box() {
+ let mut x = A { a: 1 };
+ let y: Box<_> = Box::new(&mut x.a);
+ let p = &y;
+ let q = &***p;
+ **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_field_from_var() {
+ let mut x: isize = 1;
+ let mut y = B { a: Box::new(&mut x) };
+ let p = &y.a;
+ let q = &***p;
+ **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_field_from_var_via_imm_box() {
+ let mut x: isize = 1;
+ let y = B { a: Box::new(&mut x) };
+ let p = &y.a;
+ let q = &***p;
+ **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_field_from_field() {
+ let mut x = A { a: 1 };
+ let mut y = B { a: Box::new(&mut x.a) };
+ let p = &y.a;
+ let q = &***p;
+ **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_field_from_field_via_imm_box() {
+ let mut x = A { a: 1 };
+ let y = B { a: Box::new(&mut x.a) };
+ let p = &y.a;
+ let q = &***p;
+ **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn main() {
+ indirect_write_to_imm_box();
+ borrow_in_var_from_var();
+ borrow_in_var_from_var_via_imm_box();
+ borrow_in_var_from_field();
+ borrow_in_var_from_field_via_imm_box();
+ borrow_in_field_from_var();
+ borrow_in_field_from_var_via_imm_box();
+ borrow_in_field_from_field();
+ borrow_in_field_from_field_via_imm_box();
+}