2012-12-10 17:32:48 -08:00
|
|
|
// 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.
|
|
|
|
|
2012-07-27 19:32:42 -07:00
|
|
|
struct Point {
|
2012-09-07 14:50:47 -07:00
|
|
|
x: int,
|
|
|
|
y: int,
|
2012-07-27 19:32:42 -07:00
|
|
|
}
|
2012-06-01 21:54:38 -07:00
|
|
|
|
2012-09-19 18:00:26 -07:00
|
|
|
impl Point : ops::Add<int,int> {
|
2012-12-06 11:08:23 -08:00
|
|
|
pure fn add(&self, z: &int) -> int {
|
2012-09-19 18:00:26 -07:00
|
|
|
self.x + self.y + (*z)
|
|
|
|
}
|
|
|
|
}
|
2012-07-11 15:00:40 -07:00
|
|
|
|
2012-07-27 19:32:42 -07:00
|
|
|
impl Point {
|
|
|
|
fn times(z: int) -> int {
|
|
|
|
self.x * self.y * z
|
|
|
|
}
|
2012-06-01 21:54:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn a() {
|
2012-07-27 19:32:42 -07:00
|
|
|
let mut p = Point {x: 3, y: 4};
|
2012-06-01 21:54:38 -07:00
|
|
|
|
|
|
|
// ok (we can loan out rcvr)
|
|
|
|
p + 3;
|
2012-07-27 19:32:42 -07:00
|
|
|
p.times(3);
|
2012-06-01 21:54:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {
|
2012-07-27 19:32:42 -07:00
|
|
|
let mut p = Point {x: 3, y: 4};
|
2012-06-01 21:54:38 -07:00
|
|
|
|
|
|
|
// Here I create an outstanding loan and check that we get conflicts:
|
|
|
|
|
2012-08-17 14:09:20 -07:00
|
|
|
let q = &mut p; //~ NOTE prior loan as mutable granted here
|
2012-06-01 21:54:38 -07:00
|
|
|
|
2012-12-06 14:53:21 -08:00
|
|
|
p + 3; // ok for pure fns
|
2012-07-27 19:32:42 -07:00
|
|
|
p.times(3); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
|
2012-08-17 14:09:20 -07:00
|
|
|
|
|
|
|
q.x += 1;
|
2012-06-01 21:54:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn c() {
|
|
|
|
// Here the receiver is in aliased memory and hence we cannot
|
|
|
|
// consider it immutable:
|
2012-07-27 19:32:42 -07:00
|
|
|
let q = @mut Point {x: 3, y: 4};
|
2012-06-01 21:54:38 -07:00
|
|
|
|
|
|
|
// ...this is ok for pure fns
|
|
|
|
*q + 3;
|
|
|
|
|
|
|
|
|
|
|
|
// ...but not impure fns
|
2012-09-11 21:25:01 -07:00
|
|
|
(*q).times(3); //~ ERROR illegal borrow unless pure
|
2012-06-30 12:23:59 +01:00
|
|
|
//~^ NOTE impure due to access to impure function
|
2012-06-01 21:54:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|
|
|
|
|