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.
|
|
|
|
|
2012-06-01 17:46:32 -05:00
|
|
|
type point = { x: int, y: int };
|
|
|
|
|
2012-07-11 17:00:40 -05:00
|
|
|
trait methods {
|
|
|
|
fn impurem();
|
|
|
|
fn blockm(f: fn());
|
|
|
|
pure fn purem();
|
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl point: methods {
|
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() {
|
|
|
|
let mut p = {x: 3, y: 4};
|
|
|
|
|
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() {
|
|
|
|
let mut p = {x: 3, y: 4};
|
|
|
|
|
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:
|
2012-06-01 17:46:32 -05:00
|
|
|
let q = @mut {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() {
|
|
|
|
}
|
|
|
|
|