Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 38 additions & 20 deletions src/test/compile-fail/borrowck-field-sensitivity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@

struct A { a: int, b: Box<int> }

fn borrow<T>(_: &T) { }

fn use_after_move() {
fn deref_after_move() {
let x = A { a: 1, b: box 2 };
drop(x.b);
drop(*x.b); //~ ERROR use of partially moved value: `*x.b`
}

fn use_after_fu_move() {
fn deref_after_fu_move() {
let x = A { a: 1, b: box 2 };
let y = A { a: 3, .. x };
drop(*x.b); //~ ERROR use of partially moved value: `*x.b`
Expand All @@ -27,35 +25,37 @@ fn use_after_fu_move() {
fn borrow_after_move() {
let x = A { a: 1, b: box 2 };
drop(x.b);
borrow(&x.b); //~ ERROR use of moved value: `x.b`
let p = &x.b; //~ ERROR use of moved value: `x.b`
drop(**p);
}

fn borrow_after_fu_move() {
let x = A { a: 1, b: box 2 };
let _y = A { a: 3, .. x };
borrow(&x.b); //~ ERROR use of moved value: `x.b`
let p = &x.b; //~ ERROR use of moved value: `x.b`
drop(**p);
}

fn move_after_borrow() {
let x = A { a: 1, b: box 2 };
let y = &x.b;
let p = &x.b;
drop(x.b); //~ ERROR cannot move out of `x.b` because it is borrowed
borrow(&*y);
drop(**p);
}

fn fu_move_after_borrow() {
let x = A { a: 1, b: box 2 };
let y = &x.b;
let _z = A { a: 3, .. x }; //~ ERROR cannot move out of `x.b` because it is borrowed
borrow(&*y);
let p = &x.b;
let _y = A { a: 3, .. x }; //~ ERROR cannot move out of `x.b` because it is borrowed
drop(**p);
}

fn mut_borrow_after_mut_borrow() {
let mut x = A { a: 1, b: box 2 };
let y = &mut x.a;
let z = &mut x.a; //~ ERROR cannot borrow `x.a` as mutable more than once at a time
drop(*y);
drop(*z);
let p = &mut x.a;
let q = &mut x.a; //~ ERROR cannot borrow `x.a` as mutable more than once at a time
drop(*p);
drop(*q);
}

fn move_after_move() {
Expand Down Expand Up @@ -84,7 +84,21 @@ fn fu_move_after_fu_move() {

// The following functions aren't yet accepted, but they should be.

fn use_after_field_assign_after_uninit() {
fn move_after_borrow_correct() {
let x = A { a: 1, b: box 2 };
let p = &x.a;
drop(x.b); //~ ERROR cannot move out of `x.b` because it is borrowed
drop(*p);
}

fn fu_move_after_borrow_correct() {
let x = A { a: 1, b: box 2 };
let p = &x.a;
let _y = A { a: 3, .. x }; //~ ERROR cannot move out of `x.b` because it is borrowed
drop(*p);
}

fn copy_after_field_assign_after_uninit() {
let mut x: A;
x.a = 1;
drop(x.a); //~ ERROR use of possibly uninitialized variable: `x.a`
Expand All @@ -93,7 +107,8 @@ fn use_after_field_assign_after_uninit() {
fn borrow_after_field_assign_after_uninit() {
let mut x: A;
x.a = 1;
borrow(&x.a); //~ ERROR use of possibly uninitialized variable: `x.a`
let p = &x.a; //~ ERROR use of possibly uninitialized variable: `x.a`
drop(*p);
}

fn move_after_field_assign_after_uninit() {
Expand All @@ -103,8 +118,8 @@ fn move_after_field_assign_after_uninit() {
}

fn main() {
use_after_move();
use_after_fu_move();
deref_after_move();
deref_after_fu_move();

borrow_after_move();
borrow_after_fu_move();
Expand All @@ -117,7 +132,10 @@ fn main() {
fu_move_after_move();
fu_move_after_fu_move();

use_after_field_assign_after_uninit();
move_after_borrow_correct();
fu_move_after_borrow_correct();

copy_after_field_assign_after_uninit();
borrow_after_field_assign_after_uninit();
move_after_field_assign_after_uninit();
}
Expand Down
Loading