2014-05-30 04:17:49 +00:00
|
|
|
// This tests that we can't modify Box<&mut T> contents while they
|
2015-03-30 09:47:27 +00:00
|
|
|
// are borrowed (#14498).
|
|
|
|
//
|
|
|
|
// Also includes tests of the errors reported when the Box in question
|
|
|
|
// is immutable (#14270).
|
2014-05-30 04:17:49 +00:00
|
|
|
|
2021-08-25 00:39:40 +00:00
|
|
|
|
2015-01-08 02:53:58 +00:00
|
|
|
|
2015-01-08 10:54:35 +00:00
|
|
|
struct A { a: isize }
|
|
|
|
struct B<'a> { a: Box<&'a mut isize> }
|
2014-05-30 04:17:49 +00:00
|
|
|
|
2015-03-30 09:47:27 +00:00
|
|
|
fn indirect_write_to_imm_box() {
|
|
|
|
let mut x: isize = 1;
|
2021-08-25 00:39:40 +00:00
|
|
|
let y: Box<_> = Box::new(&mut x);
|
2015-03-30 09:47:27 +00:00
|
|
|
let p = &y;
|
2019-04-22 07:40:08 +00:00
|
|
|
***p = 2; //~ ERROR cannot assign to `***p`
|
2015-03-30 09:47:27 +00:00
|
|
|
drop(p);
|
|
|
|
}
|
|
|
|
|
2014-05-30 04:17:49 +00:00
|
|
|
fn borrow_in_var_from_var() {
|
2015-03-30 09:47:27 +00:00
|
|
|
let mut x: isize = 1;
|
2021-08-25 00:39:40 +00:00
|
|
|
let mut y: Box<_> = Box::new(&mut x);
|
2015-03-30 09:47:27 +00:00
|
|
|
let p = &y;
|
|
|
|
let q = &***p;
|
2019-04-22 07:40:08 +00:00
|
|
|
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
|
2015-03-30 09:47:27 +00:00
|
|
|
drop(p);
|
|
|
|
drop(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow_in_var_from_var_via_imm_box() {
|
2015-01-08 10:54:35 +00:00
|
|
|
let mut x: isize = 1;
|
2021-08-25 00:39:40 +00:00
|
|
|
let y: Box<_> = Box::new(&mut x);
|
2014-05-30 04:17:49 +00:00
|
|
|
let p = &y;
|
|
|
|
let q = &***p;
|
2019-04-22 07:40:08 +00:00
|
|
|
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
|
2014-05-30 04:17:49 +00:00
|
|
|
drop(p);
|
|
|
|
drop(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow_in_var_from_field() {
|
2015-03-30 09:47:27 +00:00
|
|
|
let mut x = A { a: 1 };
|
2021-08-25 00:39:40 +00:00
|
|
|
let mut y: Box<_> = Box::new(&mut x.a);
|
2015-03-30 09:47:27 +00:00
|
|
|
let p = &y;
|
|
|
|
let q = &***p;
|
2019-04-22 07:40:08 +00:00
|
|
|
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
|
2015-03-30 09:47:27 +00:00
|
|
|
drop(p);
|
|
|
|
drop(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow_in_var_from_field_via_imm_box() {
|
2014-05-30 04:17:49 +00:00
|
|
|
let mut x = A { a: 1 };
|
2021-08-25 00:39:40 +00:00
|
|
|
let y: Box<_> = Box::new(&mut x.a);
|
2014-05-30 04:17:49 +00:00
|
|
|
let p = &y;
|
|
|
|
let q = &***p;
|
2019-04-22 07:40:08 +00:00
|
|
|
**y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
|
2014-05-30 04:17:49 +00:00
|
|
|
drop(p);
|
|
|
|
drop(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow_in_field_from_var() {
|
2015-03-30 09:47:27 +00:00
|
|
|
let mut x: isize = 1;
|
2021-08-25 00:39:40 +00:00
|
|
|
let mut y = B { a: Box::new(&mut x) };
|
2015-03-30 09:47:27 +00:00
|
|
|
let p = &y.a;
|
|
|
|
let q = &***p;
|
2019-04-22 07:40:08 +00:00
|
|
|
**y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
|
2015-03-30 09:47:27 +00:00
|
|
|
drop(p);
|
|
|
|
drop(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow_in_field_from_var_via_imm_box() {
|
2015-01-08 10:54:35 +00:00
|
|
|
let mut x: isize = 1;
|
2021-08-25 00:39:40 +00:00
|
|
|
let y = B { a: Box::new(&mut x) };
|
2014-05-30 04:17:49 +00:00
|
|
|
let p = &y.a;
|
|
|
|
let q = &***p;
|
2019-04-22 07:40:08 +00:00
|
|
|
**y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
|
2014-05-30 04:17:49 +00:00
|
|
|
drop(p);
|
|
|
|
drop(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow_in_field_from_field() {
|
2015-03-30 09:47:27 +00:00
|
|
|
let mut x = A { a: 1 };
|
2021-08-25 00:39:40 +00:00
|
|
|
let mut y = B { a: Box::new(&mut x.a) };
|
2015-03-30 09:47:27 +00:00
|
|
|
let p = &y.a;
|
|
|
|
let q = &***p;
|
2019-04-22 07:40:08 +00:00
|
|
|
**y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
|
2015-03-30 09:47:27 +00:00
|
|
|
drop(p);
|
|
|
|
drop(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow_in_field_from_field_via_imm_box() {
|
2014-05-30 04:17:49 +00:00
|
|
|
let mut x = A { a: 1 };
|
2021-08-25 00:39:40 +00:00
|
|
|
let y = B { a: Box::new(&mut x.a) };
|
2014-05-30 04:17:49 +00:00
|
|
|
let p = &y.a;
|
|
|
|
let q = &***p;
|
2019-04-22 07:40:08 +00:00
|
|
|
**y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
|
2014-05-30 04:17:49 +00:00
|
|
|
drop(p);
|
|
|
|
drop(q);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2015-03-30 09:47:27 +00:00
|
|
|
indirect_write_to_imm_box();
|
2014-05-30 04:17:49 +00:00
|
|
|
borrow_in_var_from_var();
|
2015-03-30 09:47:27 +00:00
|
|
|
borrow_in_var_from_var_via_imm_box();
|
2014-05-30 04:17:49 +00:00
|
|
|
borrow_in_var_from_field();
|
2015-03-30 09:47:27 +00:00
|
|
|
borrow_in_var_from_field_via_imm_box();
|
2014-05-30 04:17:49 +00:00
|
|
|
borrow_in_field_from_var();
|
2015-03-30 09:47:27 +00:00
|
|
|
borrow_in_field_from_var_via_imm_box();
|
2014-05-30 04:17:49 +00:00
|
|
|
borrow_in_field_from_field();
|
2015-03-30 09:47:27 +00:00
|
|
|
borrow_in_field_from_field_via_imm_box();
|
2014-05-30 04:17:49 +00:00
|
|
|
}
|