Skip to content

Commit 6724317

Browse files
committed
gather_loans/mod.rs Visitor refactoring (#7081): unify GatherLoan{Ctxt,Visitor}.
1 parent 959d9d6 commit 6724317

File tree

1 file changed

+42
-51
lines changed
  • src/librustc/middle/borrowck/gather_loans

1 file changed

+42
-51
lines changed

src/librustc/middle/borrowck/gather_loans/mod.rs

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -73,40 +73,38 @@ struct GatherLoanCtxt {
7373
repeating_ids: ~[ast::NodeId]
7474
}
7575

76-
struct GatherLoanVisitor;
77-
78-
impl visit::Visitor<@mut GatherLoanCtxt> for GatherLoanVisitor {
79-
fn visit_expr(&mut self, ex:@Expr, e:@mut GatherLoanCtxt) {
80-
gather_loans_in_expr(self, ex, e);
76+
impl visit::Visitor<()> for GatherLoanCtxt {
77+
fn visit_expr(&mut self, ex:@Expr, _:()) {
78+
gather_loans_in_expr(self, ex);
8179
}
82-
fn visit_block(&mut self, b:&Block, e:@mut GatherLoanCtxt) {
83-
gather_loans_in_block(self, b, e);
80+
fn visit_block(&mut self, b:&Block, _:()) {
81+
gather_loans_in_block(self, b);
8482
}
8583
fn visit_fn(&mut self, fk:&fn_kind, fd:&fn_decl, b:&Block,
86-
s:Span, n:NodeId, e:@mut GatherLoanCtxt) {
87-
gather_loans_in_fn(self, fk, fd, b, s, n, e);
84+
s:Span, n:NodeId, _:()) {
85+
gather_loans_in_fn(self, fk, fd, b, s, n);
8886
}
89-
fn visit_stmt(&mut self, s:@Stmt, e:@mut GatherLoanCtxt) {
90-
add_stmt_to_map(self, s, e);
87+
fn visit_stmt(&mut self, s:@Stmt, _:()) {
88+
add_stmt_to_map(self, s);
9189
}
92-
fn visit_pat(&mut self, p:@Pat, e:@mut GatherLoanCtxt) {
93-
add_pat_to_id_range(self, p, e);
90+
fn visit_pat(&mut self, p:@Pat, _:()) {
91+
add_pat_to_id_range(self, p);
9492
}
95-
fn visit_local(&mut self, l:@Local, e:@mut GatherLoanCtxt) {
96-
gather_loans_in_local(self, l, e);
93+
fn visit_local(&mut self, l:@Local, _:()) {
94+
gather_loans_in_local(self, l);
9795
}
9896

9997
// #7740: Do not visit items here, not even fn items nor methods
10098
// of impl items; the outer loop in borrowck/mod will visit them
10199
// for us in turn. Thus override visit_item's walk with a no-op.
102-
fn visit_item(&mut self, _:@ast::item, _:@mut GatherLoanCtxt) { }
100+
fn visit_item(&mut self, _:@ast::item, _:()) { }
103101
}
104102

105103
pub fn gather_loans(bccx: @BorrowckCtxt,
106104
decl: &ast::fn_decl,
107105
body: &ast::Block)
108106
-> (id_range, @mut ~[Loan], @mut move_data::MoveData) {
109-
let glcx = @mut GatherLoanCtxt {
107+
let mut glcx = GatherLoanCtxt {
110108
bccx: bccx,
111109
id_range: id_range::max(),
112110
all_loans: @mut ~[],
@@ -116,29 +114,26 @@ pub fn gather_loans(bccx: @BorrowckCtxt,
116114
};
117115
glcx.gather_fn_arg_patterns(decl, body);
118116

119-
let mut v = GatherLoanVisitor;
120-
v.visit_block(body, glcx);
117+
glcx.visit_block(body, ());
121118
return (glcx.id_range, glcx.all_loans, glcx.move_data);
122119
}
123120

124-
fn add_pat_to_id_range(v: &mut GatherLoanVisitor,
125-
p: @ast::Pat,
126-
this: @mut GatherLoanCtxt) {
121+
fn add_pat_to_id_range(this: &mut GatherLoanCtxt,
122+
p: @ast::Pat) {
127123
// NB: This visitor function just adds the pat ids into the id
128124
// range. We gather loans that occur in patterns using the
129125
// `gather_pat()` method below. Eventually these two should be
130126
// brought together.
131127
this.id_range.add(p.id);
132-
visit::walk_pat(v, p, this);
128+
visit::walk_pat(this, p, ());
133129
}
134130

135-
fn gather_loans_in_fn(v: &mut GatherLoanVisitor,
131+
fn gather_loans_in_fn(this: &mut GatherLoanCtxt,
136132
fk: &fn_kind,
137133
decl: &ast::fn_decl,
138134
body: &ast::Block,
139135
sp: Span,
140-
id: ast::NodeId,
141-
this: @mut GatherLoanCtxt) {
136+
id: ast::NodeId) {
142137
match fk {
143138
&visit::fk_item_fn(*) | &visit::fk_method(*) => {
144139
fail!("cannot occur, due to visit_item override");
@@ -147,23 +142,21 @@ fn gather_loans_in_fn(v: &mut GatherLoanVisitor,
147142
// Visit closures as part of the containing item.
148143
&visit::fk_anon(*) | &visit::fk_fn_block(*) => {
149144
this.push_repeating_id(body.id);
150-
visit::walk_fn(v, fk, decl, body, sp, id, this);
145+
visit::walk_fn(this, fk, decl, body, sp, id, ());
151146
this.pop_repeating_id(body.id);
152147
this.gather_fn_arg_patterns(decl, body);
153148
}
154149
}
155150
}
156151

157-
fn gather_loans_in_block(v: &mut GatherLoanVisitor,
158-
blk: &ast::Block,
159-
this: @mut GatherLoanCtxt) {
152+
fn gather_loans_in_block(this: &mut GatherLoanCtxt,
153+
blk: &ast::Block) {
160154
this.id_range.add(blk.id);
161-
visit::walk_block(v, blk, this);
155+
visit::walk_block(this, blk, ());
162156
}
163157

164-
fn gather_loans_in_local(v: &mut GatherLoanVisitor,
165-
local: @ast::Local,
166-
this: @mut GatherLoanCtxt) {
158+
fn gather_loans_in_local(this: &mut GatherLoanCtxt,
159+
local: @ast::Local) {
167160
match local.init {
168161
None => {
169162
// Variable declarations without initializers are considered "moves":
@@ -194,13 +187,12 @@ fn gather_loans_in_local(v: &mut GatherLoanVisitor,
194187
}
195188
}
196189

197-
visit::walk_local(v, local, this);
190+
visit::walk_local(this, local, ());
198191
}
199192

200193

201-
fn gather_loans_in_expr(v: &mut GatherLoanVisitor,
202-
ex: @ast::Expr,
203-
this: @mut GatherLoanCtxt) {
194+
fn gather_loans_in_expr(this: &mut GatherLoanCtxt,
195+
ex: @ast::Expr) {
204196
let bccx = this.bccx;
205197
let tcx = bccx.tcx;
206198

@@ -244,7 +236,7 @@ fn gather_loans_in_expr(v: &mut GatherLoanVisitor,
244236
base_cmt,
245237
LoanMutability::from_ast_mutability(mutbl),
246238
scope_r);
247-
visit::walk_expr(v, ex, this);
239+
visit::walk_expr(this, ex, ());
248240
}
249241

250242
ast::ExprAssign(l, _) | ast::ExprAssignOp(_, _, l, _) => {
@@ -261,7 +253,7 @@ fn gather_loans_in_expr(v: &mut GatherLoanVisitor,
261253
// with moves etc, just ignore.
262254
}
263255
}
264-
visit::walk_expr(v, ex, this);
256+
visit::walk_expr(this, ex, ());
265257
}
266258

267259
ast::ExprMatch(ex_v, ref arms) => {
@@ -271,7 +263,7 @@ fn gather_loans_in_expr(v: &mut GatherLoanVisitor,
271263
this.gather_pat(cmt, *pat, Some((arm.body.id, ex.id)));
272264
}
273265
}
274-
visit::walk_expr(v, ex, this);
266+
visit::walk_expr(this, ex, ());
275267
}
276268

277269
ast::ExprIndex(_, _, arg) |
@@ -289,36 +281,36 @@ fn gather_loans_in_expr(v: &mut GatherLoanVisitor,
289281
arg_cmt,
290282
ImmutableMutability,
291283
scope_r);
292-
visit::walk_expr(v, ex, this);
284+
visit::walk_expr(this, ex, ());
293285
}
294286

295287
// see explanation attached to the `root_ub` field:
296288
ast::ExprWhile(cond, ref body) => {
297289
// during the condition, can only root for the condition
298290
this.push_repeating_id(cond.id);
299-
v.visit_expr(cond, this);
291+
this.visit_expr(cond, ());
300292
this.pop_repeating_id(cond.id);
301293

302294
// during body, can only root for the body
303295
this.push_repeating_id(body.id);
304-
v.visit_block(body, this);
296+
this.visit_block(body, ());
305297
this.pop_repeating_id(body.id);
306298
}
307299

308300
// see explanation attached to the `root_ub` field:
309301
ast::ExprLoop(ref body, _) => {
310302
this.push_repeating_id(body.id);
311-
visit::walk_expr(v, ex, this);
303+
visit::walk_expr(this, ex, ());
312304
this.pop_repeating_id(body.id);
313305
}
314306

315307
ast::ExprFnBlock(*) => {
316308
gather_moves::gather_captures(this.bccx, this.move_data, ex);
317-
visit::walk_expr(v, ex, this);
309+
visit::walk_expr(this, ex, ());
318310
}
319311

320312
_ => {
321-
visit::walk_expr(v, ex, this);
313+
visit::walk_expr(this, ex, ());
322314
}
323315
}
324316
}
@@ -809,14 +801,13 @@ impl GatherLoanCtxt {
809801

810802
// Setting up info that preserve needs.
811803
// This is just the most convenient place to do it.
812-
fn add_stmt_to_map(v: &mut GatherLoanVisitor,
813-
stmt: @ast::Stmt,
814-
this: @mut GatherLoanCtxt) {
804+
fn add_stmt_to_map(this: &mut GatherLoanCtxt,
805+
stmt: @ast::Stmt) {
815806
match stmt.node {
816807
ast::StmtExpr(_, id) | ast::StmtSemi(_, id) => {
817808
this.bccx.stmt_map.insert(id);
818809
}
819810
_ => ()
820811
}
821-
visit::walk_stmt(v, stmt, this);
812+
visit::walk_stmt(this, stmt, ());
822813
}

0 commit comments

Comments
 (0)