Skip to content

Commit 6060370

Browse files
committed
handle moves in let initializers and allow moves from unsafe ptrs
Related to issue #2657, but this is not a complete fix.
1 parent f9afce3 commit 6060370

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

src/rustc/middle/borrowck/check_loans.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fn check_loans(bccx: borrowck_ctxt,
4747
mut declared_purity: ast::impure_fn,
4848
mut fn_args: @[]});
4949
let vt = visit::mk_vt(@{visit_expr: check_loans_in_expr,
50+
visit_local: check_loans_in_local,
5051
visit_block: check_loans_in_block,
5152
visit_fn: check_loans_in_fn
5253
with *visit::default_visitor()});
@@ -419,6 +420,9 @@ impl methods for check_loan_ctxt {
419420
// rvalues, I guess.
420421
cat_special(sk_static_item) { }
421422

423+
cat_deref(_, _, unsafe_ptr) {
424+
}
425+
422426
// Nothing else.
423427
_ {
424428
self.bccx.span_err(
@@ -542,6 +546,18 @@ fn check_loans_in_fn(fk: visit::fn_kind, decl: ast::fn_decl, body: ast::blk,
542546
#debug["purity on exit=%?", copy self.declared_purity];
543547
}
544548

549+
fn check_loans_in_local(local: @ast::local,
550+
&&self: check_loan_ctxt,
551+
vt: visit::vt<check_loan_ctxt>) {
552+
alt local.node.init {
553+
some({op: ast::init_move, expr: expr}) {
554+
self.check_move_out(expr);
555+
}
556+
some({op: ast::init_assign, _}) | none {}
557+
}
558+
visit::visit_local(local, self, vt);
559+
}
560+
545561
fn check_loans_in_expr(expr: @ast::expr,
546562
&&self: check_loan_ctxt,
547563
vt: visit::vt<check_loan_ctxt>) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
let x = some(~1);
3+
alt x { //! NOTE loan of immutable local variable granted here
4+
some(y) {
5+
let _a <- x; //! ERROR moving out of immutable local variable prohibited due to outstanding loan
6+
}
7+
_ {}
8+
}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//xfail-test
2+
3+
// this should be illegal but borrowck is not handling
4+
// pattern bindings correctly right now
5+
6+
fn main() {
7+
let x = some(~1);
8+
alt x {
9+
some(y) {
10+
let b <- y;
11+
}
12+
_ {}
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn foo(x: *~int) -> ~int {
2+
let y <- *x; //! ERROR dereference of unsafe pointer requires unsafe function or block
3+
ret y;
4+
}
5+
6+
fn main() {
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// just make sure this compiles:
2+
3+
fn bar(x: *~int) -> ~int {
4+
unsafe {
5+
let y <- *x;
6+
ret y;
7+
}
8+
}
9+
10+
fn main() {
11+
}

0 commit comments

Comments
 (0)