Skip to content

librustc: Check restrictions on all subcomponents of a path when moving #11465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions src/librustc/middle/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ use middle::moves;
use middle::ty;
use syntax::ast::{MutImmutable, MutMutable};
use syntax::ast;
use syntax::ast_map;
use syntax::ast_util;
use syntax::codemap::Span;
use syntax::parse::token;
use syntax::visit::Visitor;
use syntax::visit;
use util::ppaux::Repr;
Expand Down Expand Up @@ -77,6 +79,7 @@ pub fn check_loans(bccx: &BorrowckCtxt,
clcx.visit_block(body, ());
}

#[deriving(Eq)]
enum MoveError {
MoveOk,
MoveWhileBorrowed(/*loan*/@LoanPath, /*loan*/Span)
Expand Down Expand Up @@ -125,6 +128,9 @@ impl<'a> CheckLoanCtxt<'a> {
//! given `loan_path`

self.each_in_scope_loan(scope_id, |loan| {
debug!("each_in_scope_restriction found loan: {:?}",
loan.repr(self.tcx()));

let mut ret = true;
for restr in loan.restrictions.iter() {
if restr.loan_path == loan_path {
Expand Down Expand Up @@ -647,22 +653,34 @@ impl<'a> CheckLoanCtxt<'a> {

pub fn analyze_move_out_from(&self,
expr_id: ast::NodeId,
move_path: @LoanPath) -> MoveError {
mut move_path: @LoanPath)
-> MoveError {
debug!("analyze_move_out_from(expr_id={:?}, move_path={})",
expr_id, move_path.repr(self.tcx()));

// FIXME(#4384) inadequare if/when we permit `move a.b`

let mut ret = MoveOk;
ast_map::node_id_to_str(self.tcx().items,
expr_id,
token::get_ident_interner()),
move_path.repr(self.tcx()));

// We must check every element of a move path. See
// `borrowck-move-subcomponent.rs` for a test case.
loop {
// check for a conflicting loan:
let mut ret = MoveOk;
self.each_in_scope_restriction(expr_id, move_path, |loan, _| {
// Any restriction prevents moves.
ret = MoveWhileBorrowed(loan.loan_path, loan.span);
false
});

// check for a conflicting loan:
self.each_in_scope_restriction(expr_id, move_path, |loan, _| {
// Any restriction prevents moves.
ret = MoveWhileBorrowed(loan.loan_path, loan.span);
false
});
if ret != MoveOk {
return ret
}

ret
match *move_path {
LpVar(_) => return MoveOk,
LpExtend(subpath, _, _) => move_path = subpath,
}
}
}

pub fn check_call(&self,
Expand Down
5 changes: 4 additions & 1 deletion src/librustc/middle/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,10 @@ impl Repr for LoanPath {
fn repr(&self, tcx: ty::ctxt) -> ~str {
match self {
&LpVar(id) => {
format!("$({:?})", id)
format!("$({})",
ast_map::node_id_to_str(tcx.items,
id,
token::get_ident_interner()))
}

&LpExtend(lp, _, LpDeref(_)) => {
Expand Down
27 changes: 27 additions & 0 deletions src/test/compile-fail/borrowck-move-subcomponent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2012-2013 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.

// Tests that the borrow checker checks all components of a path when moving
// out.

#[no_std];

struct S {
x : ~int
}

fn f<T>(_: T) {}

fn main() {
let a : S = S { x : ~1 };
let pb = &a;
let S { x: ax } = a; //~ ERROR cannot move out
f(pb);
}