b3290d322e
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.
25 lines
778 B
Rust
25 lines
778 B
Rust
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
// file at the top-level directory of this distribution and at
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
|
|
// Check that `&mut` objects cannot be borrowed twice, just like
|
|
// other `&mut` pointers.
|
|
|
|
trait Foo {
|
|
fn f1<'a>(&'a mut self) -> &'a ();
|
|
fn f2(&mut self);
|
|
}
|
|
|
|
fn test(x: &mut Foo) {
|
|
let _y = x.f1();
|
|
x.f2(); //~ ERROR cannot borrow `*x` because it is already borrowed as mutable
|
|
}
|
|
|
|
fn main() {}
|