From fc0b466fd47ebe7f40a08276cbb26ef1f5657fce Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Sun, 22 Dec 2013 14:17:48 -0800 Subject: [PATCH] librustc: De-`@mut` `all_loans` in the borrow checker --- .../middle/borrowck/gather_loans/mod.rs | 16 ++++++++++------ src/librustc/middle/borrowck/mod.rs | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/librustc/middle/borrowck/gather_loans/mod.rs b/src/librustc/middle/borrowck/gather_loans/mod.rs index 7cf07b38d30b..36e35760400d 100644 --- a/src/librustc/middle/borrowck/gather_loans/mod.rs +++ b/src/librustc/middle/borrowck/gather_loans/mod.rs @@ -26,6 +26,7 @@ use middle::ty; use util::common::indenter; use util::ppaux::{Repr}; +use std::cell::RefCell; use syntax::ast; use syntax::ast_util::id_range; use syntax::codemap::Span; @@ -68,7 +69,7 @@ struct GatherLoanCtxt<'a> { bccx: &'a BorrowckCtxt, id_range: id_range, move_data: @move_data::MoveData, - all_loans: @mut ~[Loan], + all_loans: @RefCell<~[Loan]>, item_ub: ast::NodeId, repeating_ids: ~[ast::NodeId] } @@ -103,11 +104,11 @@ impl<'a> visit::Visitor<()> for GatherLoanCtxt<'a> { pub fn gather_loans(bccx: &BorrowckCtxt, decl: &ast::fn_decl, body: ast::P) - -> (id_range, @mut ~[Loan], @move_data::MoveData) { + -> (id_range, @RefCell<~[Loan]>, @move_data::MoveData) { let mut glcx = GatherLoanCtxt { bccx: bccx, id_range: id_range::max(), - all_loans: @mut ~[], + all_loans: @RefCell::new(~[]), item_ub: body.id, repeating_ids: ~[body.id], move_data: @MoveData::new() @@ -511,9 +512,9 @@ impl<'a> GatherLoanCtxt<'a> { self.mark_loan_path_as_mutated(loan_path); } - let all_loans = &mut *self.all_loans; // FIXME(#5074) + let all_loans = self.all_loans.borrow(); Loan { - index: all_loans.len(), + index: all_loans.get().len(), loan_path: loan_path, cmt: cmt, mutbl: req_mutbl, @@ -531,7 +532,10 @@ impl<'a> GatherLoanCtxt<'a> { // let loan_path = loan.loan_path; // let loan_gen_scope = loan.gen_scope; // let loan_kill_scope = loan.kill_scope; - self.all_loans.push(loan); + { + let mut all_loans = self.all_loans.borrow_mut(); + all_loans.get().push(loan); + } // if loan_gen_scope != borrow_id { // FIXME(#6268) Nested method calls diff --git a/src/librustc/middle/borrowck/mod.rs b/src/librustc/middle/borrowck/mod.rs index dd886b3054e8..61e5d1f25fe7 100644 --- a/src/librustc/middle/borrowck/mod.rs +++ b/src/librustc/middle/borrowck/mod.rs @@ -133,16 +133,18 @@ fn borrowck_fn(this: &mut BorrowckCtxt, // Check the body of fn items. let (id_range, all_loans, move_data) = gather_loans::gather_loans(this, decl, body); - let mut loan_dfcx = - DataFlowContext::new(this.tcx, - this.method_map, - LoanDataFlowOperator, - id_range, - all_loans.len()); - for (loan_idx, loan) in all_loans.iter().enumerate() { + + let all_loans = all_loans.borrow(); + let mut loan_dfcx = DataFlowContext::new(this.tcx, + this.method_map, + LoanDataFlowOperator, + id_range, + all_loans.get().len()); + for (loan_idx, loan) in all_loans.get().iter().enumerate() { loan_dfcx.add_gen(loan.gen_scope, loan_idx); loan_dfcx.add_kill(loan.kill_scope, loan_idx); } + loan_dfcx.propagate(body); let flowed_moves = move_data::FlowedMoveData::new(move_data, @@ -152,7 +154,7 @@ fn borrowck_fn(this: &mut BorrowckCtxt, body); check_loans::check_loans(this, &loan_dfcx, flowed_moves, - *all_loans, body); + *all_loans.get(), body); } }