Skip to content

Commit c2e6673

Browse files
committed
librustc: Check restrictions on all subcomponents of a path when moving
it. r=nikomatsakis
1 parent 33e8663 commit c2e6673

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

src/librustc/middle/borrowck/check_loans.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ use middle::moves;
2424
use middle::ty;
2525
use syntax::ast::{MutImmutable, MutMutable};
2626
use syntax::ast;
27+
use syntax::ast_map;
2728
use syntax::ast_util;
2829
use syntax::codemap::Span;
30+
use syntax::parse::token;
2931
use syntax::visit::Visitor;
3032
use syntax::visit;
3133
use util::ppaux::Repr;
@@ -77,6 +79,7 @@ pub fn check_loans(bccx: &BorrowckCtxt,
7779
clcx.visit_block(body, ());
7880
}
7981

82+
#[deriving(Eq)]
8083
enum MoveError {
8184
MoveOk,
8285
MoveWhileBorrowed(/*loan*/@LoanPath, /*loan*/Span)
@@ -125,6 +128,9 @@ impl<'a> CheckLoanCtxt<'a> {
125128
//! given `loan_path`
126129
127130
self.each_in_scope_loan(scope_id, |loan| {
131+
debug!("each_in_scope_restriction found loan: {:?}",
132+
loan.repr(self.tcx()));
133+
128134
let mut ret = true;
129135
for restr in loan.restrictions.iter() {
130136
if restr.loan_path == loan_path {
@@ -647,22 +653,34 @@ impl<'a> CheckLoanCtxt<'a> {
647653

648654
pub fn analyze_move_out_from(&self,
649655
expr_id: ast::NodeId,
650-
move_path: @LoanPath) -> MoveError {
656+
mut move_path: @LoanPath)
657+
-> MoveError {
651658
debug!("analyze_move_out_from(expr_id={:?}, move_path={})",
652-
expr_id, move_path.repr(self.tcx()));
653-
654-
// FIXME(#4384) inadequare if/when we permit `move a.b`
655-
656-
let mut ret = MoveOk;
659+
ast_map::node_id_to_str(self.tcx().items,
660+
expr_id,
661+
token::get_ident_interner()),
662+
move_path.repr(self.tcx()));
663+
664+
// We must check every element of a move path. See
665+
// `borrowck-move-subcomponent.rs` for a test case.
666+
loop {
667+
// check for a conflicting loan:
668+
let mut ret = MoveOk;
669+
self.each_in_scope_restriction(expr_id, move_path, |loan, _| {
670+
// Any restriction prevents moves.
671+
ret = MoveWhileBorrowed(loan.loan_path, loan.span);
672+
false
673+
});
657674

658-
// check for a conflicting loan:
659-
self.each_in_scope_restriction(expr_id, move_path, |loan, _| {
660-
// Any restriction prevents moves.
661-
ret = MoveWhileBorrowed(loan.loan_path, loan.span);
662-
false
663-
});
675+
if ret != MoveOk {
676+
return ret
677+
}
664678

665-
ret
679+
match *move_path {
680+
LpVar(_) => return MoveOk,
681+
LpExtend(subpath, _, _) => move_path = subpath,
682+
}
683+
}
666684
}
667685

668686
pub fn check_call(&self,

src/librustc/middle/borrowck/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,10 @@ impl Repr for LoanPath {
872872
fn repr(&self, tcx: ty::ctxt) -> ~str {
873873
match self {
874874
&LpVar(id) => {
875-
format!("$({:?})", id)
875+
format!("$({})",
876+
ast_map::node_id_to_str(tcx.items,
877+
id,
878+
token::get_ident_interner()))
876879
}
877880

878881
&LpExtend(lp, _, LpDeref(_)) => {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Tests that the borrow checker checks all components of a path when moving
12+
// out.
13+
14+
#[no_std];
15+
16+
struct S {
17+
x : ~int
18+
}
19+
20+
fn f<T>(_: T) {}
21+
22+
fn main() {
23+
let a : S = S { x : ~1 };
24+
let pb = &a;
25+
let S { x: ax } = a; //~ ERROR cannot move out
26+
f(pb);
27+
}

0 commit comments

Comments
 (0)