2019-04-22 02:40:08 -05:00
|
|
|
// Test that `Box<T>` is treated specially by borrow checking. This is the case
|
|
|
|
// because NLL reverted the deicision in rust-lang/rfcs#130.
|
|
|
|
|
|
|
|
// run-pass
|
|
|
|
|
2014-07-30 15:36:21 -05:00
|
|
|
struct A {
|
2015-01-08 04:54:35 -06:00
|
|
|
x: Box<isize>,
|
|
|
|
y: isize,
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct B {
|
2015-01-08 04:54:35 -06:00
|
|
|
x: Box<isize>,
|
|
|
|
y: Box<isize>,
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct C {
|
|
|
|
x: Box<A>,
|
2015-01-08 04:54:35 -06:00
|
|
|
y: isize,
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct D {
|
|
|
|
x: Box<A>,
|
2015-01-08 04:54:35 -06:00
|
|
|
y: Box<isize>,
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn copy_after_move() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let a: Box<_> = Box::new(A { x: Box::new(0), y: 1 });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = a.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = a.y;
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn move_after_move() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let a: Box<_> = Box::new(B { x: Box::new(0), y: Box::new(1) });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = a.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = a.y;
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow_after_move() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let a: Box<_> = Box::new(A { x: Box::new(0), y: 1 });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = a.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = &a.y;
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn move_after_borrow() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let a: Box<_> = Box::new(B { x: Box::new(0), y: Box::new(1) });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = &a.x;
|
2016-05-12 18:39:09 -05:00
|
|
|
let _y = a.y;
|
2018-11-08 17:10:19 -06:00
|
|
|
use_imm(_x);
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
fn copy_after_mut_borrow() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut a: Box<_> = Box::new(A { x: Box::new(0), y: 1 });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = &mut a.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = a.y;
|
2018-11-08 17:10:19 -06:00
|
|
|
use_mut(_x);
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
fn move_after_mut_borrow() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut a: Box<_> = Box::new(B { x: Box::new(0), y: Box::new(1) });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = &mut a.x;
|
2016-05-12 18:39:09 -05:00
|
|
|
let _y = a.y;
|
2018-11-08 17:10:19 -06:00
|
|
|
use_mut(_x);
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
fn borrow_after_mut_borrow() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut a: Box<_> = Box::new(A { x: Box::new(0), y: 1 });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = &mut a.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = &a.y;
|
2018-11-08 17:10:19 -06:00
|
|
|
use_mut(_x);
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
fn mut_borrow_after_borrow() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut a: Box<_> = Box::new(A { x: Box::new(0), y: 1 });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = &a.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = &mut a.y;
|
2018-11-08 17:10:19 -06:00
|
|
|
use_imm(_x);
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
fn copy_after_move_nested() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let a: Box<_> = Box::new(C { x: Box::new(A { x: Box::new(0), y: 1 }), y: 2 });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = a.x.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = a.y;
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn move_after_move_nested() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let a: Box<_> = Box::new(D { x: Box::new(A { x: Box::new(0), y: 1 }), y: Box::new(2) });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = a.x.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = a.y;
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow_after_move_nested() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let a: Box<_> = Box::new(C { x: Box::new(A { x: Box::new(0), y: 1 }), y: 2 });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = a.x.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = &a.y;
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn move_after_borrow_nested() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let a: Box<_> = Box::new(D { x: Box::new(A { x: Box::new(0), y: 1 }), y: Box::new(2) });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = &a.x.x;
|
2016-05-12 18:39:09 -05:00
|
|
|
let _y = a.y;
|
2018-11-08 17:10:19 -06:00
|
|
|
use_imm(_x);
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
fn copy_after_mut_borrow_nested() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut a: Box<_> = Box::new(C { x: Box::new(A { x: Box::new(0), y: 1 }), y: 2 });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = &mut a.x.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = a.y;
|
2018-11-08 17:10:19 -06:00
|
|
|
use_mut(_x);
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
fn move_after_mut_borrow_nested() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut a: Box<_> = Box::new(D { x: Box::new(A { x: Box::new(0), y: 1 }), y: Box::new(2) });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = &mut a.x.x;
|
2016-05-12 18:39:09 -05:00
|
|
|
let _y = a.y;
|
2018-11-08 17:10:19 -06:00
|
|
|
use_mut(_x);
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
fn borrow_after_mut_borrow_nested() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut a: Box<_> = Box::new(C { x: Box::new(A { x: Box::new(0), y: 1 }), y: 2 });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = &mut a.x.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = &a.y;
|
2018-11-08 17:10:19 -06:00
|
|
|
use_mut(_x);
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
|
|
|
fn mut_borrow_after_borrow_nested() {
|
2021-08-24 19:39:40 -05:00
|
|
|
let mut a: Box<_> = Box::new(C { x: Box::new(A { x: Box::new(0), y: 1 }), y: 2 });
|
2014-07-30 15:36:21 -05:00
|
|
|
let _x = &a.x.x;
|
2019-04-22 02:40:08 -05:00
|
|
|
let _y = &mut a.y;
|
2018-11-08 17:10:19 -06:00
|
|
|
use_imm(_x);
|
2014-07-30 15:36:21 -05:00
|
|
|
}
|
2019-04-22 02:40:08 -05:00
|
|
|
|
|
|
|
fn main() {
|
2014-07-30 15:36:21 -05:00
|
|
|
copy_after_move();
|
|
|
|
move_after_move();
|
|
|
|
borrow_after_move();
|
|
|
|
|
|
|
|
move_after_borrow();
|
|
|
|
|
|
|
|
copy_after_mut_borrow();
|
|
|
|
move_after_mut_borrow();
|
|
|
|
borrow_after_mut_borrow();
|
|
|
|
mut_borrow_after_borrow();
|
|
|
|
|
|
|
|
copy_after_move_nested();
|
|
|
|
move_after_move_nested();
|
|
|
|
borrow_after_move_nested();
|
|
|
|
|
|
|
|
move_after_borrow_nested();
|
|
|
|
|
|
|
|
copy_after_mut_borrow_nested();
|
|
|
|
move_after_mut_borrow_nested();
|
|
|
|
borrow_after_mut_borrow_nested();
|
|
|
|
mut_borrow_after_borrow_nested();
|
|
|
|
}
|
2018-11-08 17:10:19 -06:00
|
|
|
|
|
|
|
fn use_mut<T>(_: &mut T) { }
|
|
|
|
fn use_imm<T>(_: &T) { }
|