Skip to content
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

Some Visitor+Context unifications #9094

Closed
wants to merge 3 commits 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
67 changes: 30 additions & 37 deletions src/librustc/middle/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,23 @@ struct CheckLoanCtxt<'self> {
reported: @mut HashSet<ast::NodeId>,
}

struct CheckLoanVisitor;
impl<'self> Visitor<()> for CheckLoanCtxt<'self> {

impl<'self> Visitor<CheckLoanCtxt<'self>> 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);
}
}

Expand All @@ -68,16 +67,15 @@ 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,
all_loans: all_loans,
reported: @mut HashSet::new(),
};

let mut vt = CheckLoanVisitor;
vt.visit_block(body, clcx);
clcx.visit_block(body, ());
}

enum MoveError {
Expand Down Expand Up @@ -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(*) => {
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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()));
Expand Down Expand Up @@ -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);
}

93 changes: 42 additions & 51 deletions src/librustc/middle/borrowck/gather_loans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ~[],
Expand All @@ -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");
Expand All @@ -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":
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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, _) => {
Expand All @@ -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) => {
Expand All @@ -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) |
Expand All @@ -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, ());
}
}
}
Expand Down Expand Up @@ -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, ());
}
Loading