handle moves in let initializers and allow moves from unsafe ptrs

Related to issue #2657, but this is not a complete fix.
This commit is contained in:
Niko Matsakis 2012-06-20 20:08:25 -07:00
parent f9afce319a
commit 60603703ea
5 changed files with 57 additions and 0 deletions

View File

@ -47,6 +47,7 @@ fn check_loans(bccx: borrowck_ctxt,
mut declared_purity: ast::impure_fn,
mut fn_args: @[]});
let vt = visit::mk_vt(@{visit_expr: check_loans_in_expr,
visit_local: check_loans_in_local,
visit_block: check_loans_in_block,
visit_fn: check_loans_in_fn
with *visit::default_visitor()});
@ -419,6 +420,9 @@ impl methods for check_loan_ctxt {
// rvalues, I guess.
cat_special(sk_static_item) { }
cat_deref(_, _, unsafe_ptr) {
}
// Nothing else.
_ {
self.bccx.span_err(
@ -542,6 +546,18 @@ fn check_loans_in_fn(fk: visit::fn_kind, decl: ast::fn_decl, body: ast::blk,
#debug["purity on exit=%?", copy self.declared_purity];
}
fn check_loans_in_local(local: @ast::local,
&&self: check_loan_ctxt,
vt: visit::vt<check_loan_ctxt>) {
alt local.node.init {
some({op: ast::init_move, expr: expr}) {
self.check_move_out(expr);
}
some({op: ast::init_assign, _}) | none {}
}
visit::visit_local(local, self, vt);
}
fn check_loans_in_expr(expr: @ast::expr,
&&self: check_loan_ctxt,
vt: visit::vt<check_loan_ctxt>) {

View File

@ -0,0 +1,9 @@
fn main() {
let x = some(~1);
alt x { //! NOTE loan of immutable local variable granted here
some(y) {
let _a <- x; //! ERROR moving out of immutable local variable prohibited due to outstanding loan
}
_ {}
}
}

View File

@ -0,0 +1,14 @@
//xfail-test
// this should be illegal but borrowck is not handling
// pattern bindings correctly right now
fn main() {
let x = some(~1);
alt x {
some(y) {
let b <- y;
}
_ {}
}
}

View File

@ -0,0 +1,7 @@
fn foo(x: *~int) -> ~int {
let y <- *x; //! ERROR dereference of unsafe pointer requires unsafe function or block
ret y;
}
fn main() {
}

View File

@ -0,0 +1,11 @@
// just make sure this compiles:
fn bar(x: *~int) -> ~int {
unsafe {
let y <- *x;
ret y;
}
}
fn main() {
}