rust/tests/ui/borrowck/borrowck-loan-rcvr-overloaded-op.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
743 B
Rust
Raw Normal View History

use std::ops::Add;
2015-03-30 08:38:27 -05:00
#[derive(Copy, Clone)]
2013-03-15 14:24:24 -05:00
struct Point {
x: isize,
y: isize,
}
impl Add<isize> for Point {
type Output = isize;
2014-12-31 14:45:13 -06:00
fn add(self, z: isize) -> isize {
2014-12-01 16:52:10 -06:00
self.x + self.y + z
}
}
impl Point {
pub fn times(&self, z: isize) -> isize {
self.x * self.y * z
}
}
fn a() {
let mut p = Point {x: 3, y: 4};
// ok (we can loan out rcvr)
p + 3;
p.times(3);
}
fn b() {
let mut p = Point {x: 3, y: 4};
// Here I create an outstanding loan and check that we get conflicts:
2013-03-15 14:24:24 -05:00
let q = &mut p;
2014-12-01 16:52:10 -06:00
p + 3; //~ ERROR cannot use `p`
2013-03-15 14:24:24 -05:00
p.times(3); //~ ERROR cannot borrow `p`
*q + 3; // OK to use the new alias `q`
q.x += 1; // and OK to mutate it
}
fn main() {
}