2012-12-10 19:32:48 -06: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.
|
|
|
|
|
2013-02-21 17:19:40 -06:00
|
|
|
struct point { x: int, y: int }
|
2012-06-01 17:46:32 -05:00
|
|
|
|
2012-07-11 17:00:40 -05:00
|
|
|
trait methods {
|
|
|
|
fn impurem();
|
|
|
|
fn blockm(f: fn());
|
|
|
|
pure fn purem();
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl methods for point {
|
2012-06-01 17:46:32 -05:00
|
|
|
fn impurem() {
|
|
|
|
}
|
|
|
|
|
2012-06-01 23:54:38 -05:00
|
|
|
fn blockm(f: fn()) { f() }
|
|
|
|
|
2012-06-01 17:46:32 -05:00
|
|
|
pure fn purem() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn a() {
|
2013-02-21 17:19:40 -06:00
|
|
|
let mut p = point {x: 3, y: 4};
|
2012-06-01 17:46:32 -05:00
|
|
|
|
2012-06-01 23:54:38 -05:00
|
|
|
// Here: it's ok to call even though receiver is mutable, because we
|
|
|
|
// can loan it out.
|
2012-06-01 17:46:32 -05:00
|
|
|
p.purem();
|
|
|
|
p.impurem();
|
2012-06-01 23:54:38 -05:00
|
|
|
|
|
|
|
// But in this case we do not honor the loan:
|
2012-07-04 14:04:28 -05:00
|
|
|
do p.blockm { //~ NOTE loan of mutable local variable granted here
|
2012-06-30 06:23:59 -05:00
|
|
|
p.x = 10; //~ ERROR assigning to mutable field prohibited due to outstanding loan
|
2012-06-01 23:54:38 -05:00
|
|
|
}
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {
|
2013-02-21 17:19:40 -06:00
|
|
|
let mut p = point {x: 3, y: 4};
|
2012-06-01 17:46:32 -05:00
|
|
|
|
2012-06-01 23:54:38 -05:00
|
|
|
// Here I create an outstanding loan and check that we get conflicts:
|
|
|
|
|
2012-08-17 16:09:20 -05:00
|
|
|
let l = &mut p; //~ NOTE prior loan as mutable granted here
|
2012-06-30 06:23:59 -05:00
|
|
|
//~^ NOTE prior loan as mutable granted here
|
2012-06-01 17:46:32 -05:00
|
|
|
|
2012-06-30 06:23:59 -05:00
|
|
|
p.purem(); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
|
|
|
|
p.impurem(); //~ ERROR loan of mutable local variable as immutable conflicts with prior loan
|
2012-08-17 16:09:20 -05:00
|
|
|
|
|
|
|
l.x += 1;
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn c() {
|
2013-01-17 20:43:35 -06:00
|
|
|
// Loaning @mut as & is considered legal due to dynamic checks:
|
2013-02-21 17:19:40 -06:00
|
|
|
let q = @mut point {x: 3, y: 4};
|
2013-01-17 20:43:35 -06:00
|
|
|
q.purem();
|
|
|
|
q.impurem();
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|
|
|
|
|