rust/src/test/compile-fail/borrowck-report-with-custom-diagnostic.rs
Kiet Tran b3290d322e Make some borrow checker errors more user friendly
A mutable and immutable borrow place some restrictions on what you can
with the variable until the borrow ends. This commit attempts to convey
to the user what those restrictions are. Also, if the original borrow is
a mutable borrow, the error message has been changed (more specifically,
i. "cannot borrow `x` as immutable because it is also borrowed as
mutable" and ii. "cannot borrow `x` as mutable more than once" have
been changed to "cannot borrow `x` because it is already borrowed as
mutable").

In addition, this adds a (custom) span note to communicate where the
original borrow ends.
2014-01-23 14:44:28 -05:00

32 lines
693 B
Rust

#[allow(dead_code)];
fn main() {
// Original borrow ends at end of function
let mut x = 1u;
let y = &mut x;
let z = &x; //~ ERROR cannot borrow
}
//~^ NOTE previous borrow ends here
fn foo() {
match true {
true => {
// Original borrow ends at end of match arm
let mut x = 1u;
let y = &x;
let z = &mut x; //~ ERROR cannot borrow
}
//~^ NOTE previous borrow ends here
false => ()
}
}
fn bar() {
// Original borrow ends at end of closure
|| {
let mut x = 1u;
let y = &mut x;
let z = &mut x; //~ ERROR cannot borrow
};
//~^ NOTE previous borrow ends here
}