From 959d9d60e143fb969fa5bc778a2017a9312bd435 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 10 Sep 2013 10:50:41 +0200 Subject: [PATCH 1/3] check_loans.rs Visitor refactoring (#7081): unify CheckLoan{Ctxt,Visitor}. --- src/librustc/middle/borrowck/check_loans.rs | 67 +++++++++------------ 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/src/librustc/middle/borrowck/check_loans.rs b/src/librustc/middle/borrowck/check_loans.rs index 8cfb2bad57f3b..9280bbf1817a7 100644 --- a/src/librustc/middle/borrowck/check_loans.rs +++ b/src/librustc/middle/borrowck/check_loans.rs @@ -40,24 +40,23 @@ struct CheckLoanCtxt<'self> { reported: @mut HashSet, } -struct CheckLoanVisitor; +impl<'self> Visitor<()> for CheckLoanCtxt<'self> { -impl<'self> Visitor> for CheckLoanVisitor { - fn visit_expr<'a>(&mut self, ex:@ast::Expr, e:CheckLoanCtxt<'a>) { - check_loans_in_expr(self, ex, e); + fn visit_expr(&mut self, ex:@ast::Expr, _:()) { + check_loans_in_expr(self, ex); } - fn visit_local(&mut self, l:@ast::Local, e:CheckLoanCtxt) { - check_loans_in_local(self, l, e); + fn visit_local(&mut self, l:@ast::Local, _:()) { + check_loans_in_local(self, l); } - fn visit_block(&mut self, b:&ast::Block, e:CheckLoanCtxt) { - check_loans_in_block(self, b, e); + fn visit_block(&mut self, b:&ast::Block, _:()) { + check_loans_in_block(self, b); } - fn visit_pat(&mut self, p:@ast::Pat, e:CheckLoanCtxt) { - check_loans_in_pat(self, p, e); + fn visit_pat(&mut self, p:@ast::Pat, _:()) { + check_loans_in_pat(self, p); } fn visit_fn(&mut self, fk:&visit::fn_kind, fd:&ast::fn_decl, - b:&ast::Block, s:Span, n:ast::NodeId, e:CheckLoanCtxt) { - check_loans_in_fn(self, fk, fd, b, s, n, e); + b:&ast::Block, s:Span, n:ast::NodeId, _:()) { + check_loans_in_fn(self, fk, fd, b, s, n); } } @@ -68,7 +67,7 @@ pub fn check_loans(bccx: @BorrowckCtxt, body: &ast::Block) { debug!("check_loans(body id=%?)", body.id); - let clcx = CheckLoanCtxt { + let mut clcx = CheckLoanCtxt { bccx: bccx, dfcx_loans: dfcx_loans, move_data: @move_data, @@ -76,8 +75,7 @@ pub fn check_loans(bccx: @BorrowckCtxt, reported: @mut HashSet::new(), }; - let mut vt = CheckLoanVisitor; - vt.visit_block(body, clcx); + clcx.visit_block(body, ()); } enum MoveError { @@ -725,13 +723,12 @@ impl<'self> CheckLoanCtxt<'self> { } } -fn check_loans_in_fn<'a>(visitor: &mut CheckLoanVisitor, +fn check_loans_in_fn<'a>(this: &mut CheckLoanCtxt<'a>, fk: &visit::fn_kind, decl: &ast::fn_decl, body: &ast::Block, sp: Span, - id: ast::NodeId, - this: CheckLoanCtxt<'a>) { + id: ast::NodeId) { match *fk { visit::fk_item_fn(*) | visit::fk_method(*) => { @@ -745,9 +742,9 @@ fn check_loans_in_fn<'a>(visitor: &mut CheckLoanVisitor, } } - visit::walk_fn(visitor, fk, decl, body, sp, id, this); + visit::walk_fn(this, fk, decl, body, sp, id, ()); - fn check_captured_variables(this: CheckLoanCtxt, + fn check_captured_variables(this: &CheckLoanCtxt, closure_id: ast::NodeId, span: Span) { let cap_vars = this.bccx.capture_map.get(&closure_id); @@ -765,7 +762,7 @@ fn check_loans_in_fn<'a>(visitor: &mut CheckLoanVisitor, } return; - fn check_by_move_capture(this: CheckLoanCtxt, + fn check_by_move_capture(this: &CheckLoanCtxt, closure_id: ast::NodeId, cap_var: &moves::CaptureVar, move_path: @LoanPath) { @@ -788,16 +785,14 @@ fn check_loans_in_fn<'a>(visitor: &mut CheckLoanVisitor, } } -fn check_loans_in_local<'a>(vt: &mut CheckLoanVisitor, - local: @ast::Local, - this: CheckLoanCtxt<'a>) { - visit::walk_local(vt, local, this); +fn check_loans_in_local<'a>(this: &mut CheckLoanCtxt<'a>, + local: @ast::Local) { + visit::walk_local(this, local, ()); } -fn check_loans_in_expr<'a>(vt: &mut CheckLoanVisitor, - expr: @ast::Expr, - this: CheckLoanCtxt<'a>) { - visit::walk_expr(vt, expr, this); +fn check_loans_in_expr<'a>(this: &mut CheckLoanCtxt<'a>, + expr: @ast::Expr) { + visit::walk_expr(this, expr, ()); debug!("check_loans_in_expr(expr=%s)", expr.repr(this.tcx())); @@ -848,20 +843,18 @@ fn check_loans_in_expr<'a>(vt: &mut CheckLoanVisitor, } } -fn check_loans_in_pat<'a>(vt: &mut CheckLoanVisitor, - pat: @ast::Pat, - this: CheckLoanCtxt<'a>) +fn check_loans_in_pat<'a>(this: &mut CheckLoanCtxt<'a>, + pat: @ast::Pat) { this.check_for_conflicting_loans(pat.id); this.check_move_out_from_id(pat.id, pat.span); - visit::walk_pat(vt, pat, this); + visit::walk_pat(this, pat, ()); } -fn check_loans_in_block<'a>(vt: &mut CheckLoanVisitor, - blk: &ast::Block, - this: CheckLoanCtxt<'a>) +fn check_loans_in_block<'a>(this: &mut CheckLoanCtxt<'a>, + blk: &ast::Block) { - visit::walk_block(vt, blk, this); + visit::walk_block(this, blk, ()); this.check_for_conflicting_loans(blk.id); } From 6724317dad11856d6467ca2374e8c8bb6e749612 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 10 Sep 2013 11:04:11 +0200 Subject: [PATCH 2/3] gather_loans/mod.rs Visitor refactoring (#7081): unify GatherLoan{Ctxt,Visitor}. --- .../middle/borrowck/gather_loans/mod.rs | 93 +++++++++---------- 1 file changed, 42 insertions(+), 51 deletions(-) diff --git a/src/librustc/middle/borrowck/gather_loans/mod.rs b/src/librustc/middle/borrowck/gather_loans/mod.rs index 352a229633b6d..d754e0360fd45 100644 --- a/src/librustc/middle/borrowck/gather_loans/mod.rs +++ b/src/librustc/middle/borrowck/gather_loans/mod.rs @@ -73,40 +73,38 @@ struct GatherLoanCtxt { repeating_ids: ~[ast::NodeId] } -struct GatherLoanVisitor; - -impl visit::Visitor<@mut GatherLoanCtxt> for GatherLoanVisitor { - fn visit_expr(&mut self, ex:@Expr, e:@mut GatherLoanCtxt) { - gather_loans_in_expr(self, ex, e); +impl visit::Visitor<()> for GatherLoanCtxt { + fn visit_expr(&mut self, ex:@Expr, _:()) { + gather_loans_in_expr(self, ex); } - fn visit_block(&mut self, b:&Block, e:@mut GatherLoanCtxt) { - gather_loans_in_block(self, b, e); + fn visit_block(&mut self, b:&Block, _:()) { + gather_loans_in_block(self, b); } fn visit_fn(&mut self, fk:&fn_kind, fd:&fn_decl, b:&Block, - s:Span, n:NodeId, e:@mut GatherLoanCtxt) { - gather_loans_in_fn(self, fk, fd, b, s, n, e); + s:Span, n:NodeId, _:()) { + gather_loans_in_fn(self, fk, fd, b, s, n); } - fn visit_stmt(&mut self, s:@Stmt, e:@mut GatherLoanCtxt) { - add_stmt_to_map(self, s, e); + fn visit_stmt(&mut self, s:@Stmt, _:()) { + add_stmt_to_map(self, s); } - fn visit_pat(&mut self, p:@Pat, e:@mut GatherLoanCtxt) { - add_pat_to_id_range(self, p, e); + fn visit_pat(&mut self, p:@Pat, _:()) { + add_pat_to_id_range(self, p); } - fn visit_local(&mut self, l:@Local, e:@mut GatherLoanCtxt) { - gather_loans_in_local(self, l, e); + fn visit_local(&mut self, l:@Local, _:()) { + gather_loans_in_local(self, l); } // #7740: Do not visit items here, not even fn items nor methods // of impl items; the outer loop in borrowck/mod will visit them // for us in turn. Thus override visit_item's walk with a no-op. - fn visit_item(&mut self, _:@ast::item, _:@mut GatherLoanCtxt) { } + fn visit_item(&mut self, _:@ast::item, _:()) { } } pub fn gather_loans(bccx: @BorrowckCtxt, decl: &ast::fn_decl, body: &ast::Block) -> (id_range, @mut ~[Loan], @mut move_data::MoveData) { - let glcx = @mut GatherLoanCtxt { + let mut glcx = GatherLoanCtxt { bccx: bccx, id_range: id_range::max(), all_loans: @mut ~[], @@ -116,29 +114,26 @@ pub fn gather_loans(bccx: @BorrowckCtxt, }; glcx.gather_fn_arg_patterns(decl, body); - let mut v = GatherLoanVisitor; - v.visit_block(body, glcx); + glcx.visit_block(body, ()); return (glcx.id_range, glcx.all_loans, glcx.move_data); } -fn add_pat_to_id_range(v: &mut GatherLoanVisitor, - p: @ast::Pat, - this: @mut GatherLoanCtxt) { +fn add_pat_to_id_range(this: &mut GatherLoanCtxt, + p: @ast::Pat) { // NB: This visitor function just adds the pat ids into the id // range. We gather loans that occur in patterns using the // `gather_pat()` method below. Eventually these two should be // brought together. this.id_range.add(p.id); - visit::walk_pat(v, p, this); + visit::walk_pat(this, p, ()); } -fn gather_loans_in_fn(v: &mut GatherLoanVisitor, +fn gather_loans_in_fn(this: &mut GatherLoanCtxt, fk: &fn_kind, decl: &ast::fn_decl, body: &ast::Block, sp: Span, - id: ast::NodeId, - this: @mut GatherLoanCtxt) { + id: ast::NodeId) { match fk { &visit::fk_item_fn(*) | &visit::fk_method(*) => { fail!("cannot occur, due to visit_item override"); @@ -147,23 +142,21 @@ fn gather_loans_in_fn(v: &mut GatherLoanVisitor, // Visit closures as part of the containing item. &visit::fk_anon(*) | &visit::fk_fn_block(*) => { this.push_repeating_id(body.id); - visit::walk_fn(v, fk, decl, body, sp, id, this); + visit::walk_fn(this, fk, decl, body, sp, id, ()); this.pop_repeating_id(body.id); this.gather_fn_arg_patterns(decl, body); } } } -fn gather_loans_in_block(v: &mut GatherLoanVisitor, - blk: &ast::Block, - this: @mut GatherLoanCtxt) { +fn gather_loans_in_block(this: &mut GatherLoanCtxt, + blk: &ast::Block) { this.id_range.add(blk.id); - visit::walk_block(v, blk, this); + visit::walk_block(this, blk, ()); } -fn gather_loans_in_local(v: &mut GatherLoanVisitor, - local: @ast::Local, - this: @mut GatherLoanCtxt) { +fn gather_loans_in_local(this: &mut GatherLoanCtxt, + local: @ast::Local) { match local.init { None => { // Variable declarations without initializers are considered "moves": @@ -194,13 +187,12 @@ fn gather_loans_in_local(v: &mut GatherLoanVisitor, } } - visit::walk_local(v, local, this); + visit::walk_local(this, local, ()); } -fn gather_loans_in_expr(v: &mut GatherLoanVisitor, - ex: @ast::Expr, - this: @mut GatherLoanCtxt) { +fn gather_loans_in_expr(this: &mut GatherLoanCtxt, + ex: @ast::Expr) { let bccx = this.bccx; let tcx = bccx.tcx; @@ -244,7 +236,7 @@ fn gather_loans_in_expr(v: &mut GatherLoanVisitor, base_cmt, LoanMutability::from_ast_mutability(mutbl), scope_r); - visit::walk_expr(v, ex, this); + visit::walk_expr(this, ex, ()); } ast::ExprAssign(l, _) | ast::ExprAssignOp(_, _, l, _) => { @@ -261,7 +253,7 @@ fn gather_loans_in_expr(v: &mut GatherLoanVisitor, // with moves etc, just ignore. } } - visit::walk_expr(v, ex, this); + visit::walk_expr(this, ex, ()); } ast::ExprMatch(ex_v, ref arms) => { @@ -271,7 +263,7 @@ fn gather_loans_in_expr(v: &mut GatherLoanVisitor, this.gather_pat(cmt, *pat, Some((arm.body.id, ex.id))); } } - visit::walk_expr(v, ex, this); + visit::walk_expr(this, ex, ()); } ast::ExprIndex(_, _, arg) | @@ -289,36 +281,36 @@ fn gather_loans_in_expr(v: &mut GatherLoanVisitor, arg_cmt, ImmutableMutability, scope_r); - visit::walk_expr(v, ex, this); + visit::walk_expr(this, ex, ()); } // see explanation attached to the `root_ub` field: ast::ExprWhile(cond, ref body) => { // during the condition, can only root for the condition this.push_repeating_id(cond.id); - v.visit_expr(cond, this); + this.visit_expr(cond, ()); this.pop_repeating_id(cond.id); // during body, can only root for the body this.push_repeating_id(body.id); - v.visit_block(body, this); + this.visit_block(body, ()); this.pop_repeating_id(body.id); } // see explanation attached to the `root_ub` field: ast::ExprLoop(ref body, _) => { this.push_repeating_id(body.id); - visit::walk_expr(v, ex, this); + visit::walk_expr(this, ex, ()); this.pop_repeating_id(body.id); } ast::ExprFnBlock(*) => { gather_moves::gather_captures(this.bccx, this.move_data, ex); - visit::walk_expr(v, ex, this); + visit::walk_expr(this, ex, ()); } _ => { - visit::walk_expr(v, ex, this); + visit::walk_expr(this, ex, ()); } } } @@ -809,14 +801,13 @@ impl GatherLoanCtxt { // Setting up info that preserve needs. // This is just the most convenient place to do it. -fn add_stmt_to_map(v: &mut GatherLoanVisitor, - stmt: @ast::Stmt, - this: @mut GatherLoanCtxt) { +fn add_stmt_to_map(this: &mut GatherLoanCtxt, + stmt: @ast::Stmt) { match stmt.node { ast::StmtExpr(_, id) | ast::StmtSemi(_, id) => { this.bccx.stmt_map.insert(id); } _ => () } - visit::walk_stmt(v, stmt, this); + visit::walk_stmt(this, stmt, ()); } From ed37da2f799bd95c6398b92a51f7eaf65c62c5fe Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 10 Sep 2013 11:24:04 +0200 Subject: [PATCH 3/3] middle/entry.rs Visitor refactoring (#7081): unify Entry{Context,Visitor}. --- src/librustc/middle/entry.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/librustc/middle/entry.rs b/src/librustc/middle/entry.rs index 22e6bc3ac2feb..4f9c9bdc62b02 100644 --- a/src/librustc/middle/entry.rs +++ b/src/librustc/middle/entry.rs @@ -39,11 +39,9 @@ struct EntryContext { non_main_fns: ~[(NodeId, Span)], } -struct EntryVisitor; - -impl Visitor<@mut EntryContext> for EntryVisitor { - fn visit_item(&mut self, item:@item, ctxt:@mut EntryContext) { - find_item(item, ctxt, self); +impl Visitor<()> for EntryContext { + fn visit_item(&mut self, item:@item, _:()) { + find_item(item, self); } } @@ -62,7 +60,7 @@ pub fn find_entry_point(session: Session, crate: &Crate, ast_map: ast_map::map) return } - let ctxt = @mut EntryContext { + let mut ctxt = EntryContext { session: session, ast_map: ast_map, main_fn: None, @@ -71,14 +69,12 @@ pub fn find_entry_point(session: Session, crate: &Crate, ast_map: ast_map::map) non_main_fns: ~[], }; - let mut v = EntryVisitor; - - visit::walk_crate(&mut v, crate, ctxt); + visit::walk_crate(&mut ctxt, crate, ()); - configure_main(ctxt); + configure_main(&mut ctxt); } -fn find_item(item: @item, ctxt: @mut EntryContext, visitor: &mut EntryVisitor) { +fn find_item(item: @item, ctxt: &mut EntryContext) { match item.node { item_fn(*) => { if item.ident.name == special_idents::main.name { @@ -125,11 +121,10 @@ fn find_item(item: @item, ctxt: @mut EntryContext, visitor: &mut EntryVisitor) { _ => () } - visit::walk_item(visitor, item, ctxt); + visit::walk_item(ctxt, item, ()); } -fn configure_main(ctxt: @mut EntryContext) { - let this = &mut *ctxt; +fn configure_main(this: &mut EntryContext) { if this.start_fn.is_some() { *this.session.entry_fn = this.start_fn; *this.session.entry_type = Some(session::EntryStart);