Skip to content

Commit 917d3c2

Browse files
committed
auto merge of #9094 : pnkfelix/rust/fsk-visitor-ports, r=huonw
r? anyone Remove some trivial Visitor structs, using their non-trivial Contexts as the Visitor implementation instead. Removed a little bit of `@boxing` as well. Part of ongoing work on #7081.
2 parents 753d8c2 + ed37da2 commit 917d3c2

File tree

3 files changed

+81
-102
lines changed

3 files changed

+81
-102
lines changed

src/librustc/middle/borrowck/check_loans.rs

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,23 @@ struct CheckLoanCtxt<'self> {
4040
reported: @mut HashSet<ast::NodeId>,
4141
}
4242

43-
struct CheckLoanVisitor;
43+
impl<'self> Visitor<()> for CheckLoanCtxt<'self> {
4444

45-
impl<'self> Visitor<CheckLoanCtxt<'self>> for CheckLoanVisitor {
46-
fn visit_expr<'a>(&mut self, ex:@ast::Expr, e:CheckLoanCtxt<'a>) {
47-
check_loans_in_expr(self, ex, e);
45+
fn visit_expr(&mut self, ex:@ast::Expr, _:()) {
46+
check_loans_in_expr(self, ex);
4847
}
49-
fn visit_local(&mut self, l:@ast::Local, e:CheckLoanCtxt) {
50-
check_loans_in_local(self, l, e);
48+
fn visit_local(&mut self, l:@ast::Local, _:()) {
49+
check_loans_in_local(self, l);
5150
}
52-
fn visit_block(&mut self, b:&ast::Block, e:CheckLoanCtxt) {
53-
check_loans_in_block(self, b, e);
51+
fn visit_block(&mut self, b:&ast::Block, _:()) {
52+
check_loans_in_block(self, b);
5453
}
55-
fn visit_pat(&mut self, p:@ast::Pat, e:CheckLoanCtxt) {
56-
check_loans_in_pat(self, p, e);
54+
fn visit_pat(&mut self, p:@ast::Pat, _:()) {
55+
check_loans_in_pat(self, p);
5756
}
5857
fn visit_fn(&mut self, fk:&visit::fn_kind, fd:&ast::fn_decl,
59-
b:&ast::Block, s:Span, n:ast::NodeId, e:CheckLoanCtxt) {
60-
check_loans_in_fn(self, fk, fd, b, s, n, e);
58+
b:&ast::Block, s:Span, n:ast::NodeId, _:()) {
59+
check_loans_in_fn(self, fk, fd, b, s, n);
6160
}
6261
}
6362

@@ -68,16 +67,15 @@ pub fn check_loans(bccx: @BorrowckCtxt,
6867
body: &ast::Block) {
6968
debug!("check_loans(body id=%?)", body.id);
7069

71-
let clcx = CheckLoanCtxt {
70+
let mut clcx = CheckLoanCtxt {
7271
bccx: bccx,
7372
dfcx_loans: dfcx_loans,
7473
move_data: @move_data,
7574
all_loans: all_loans,
7675
reported: @mut HashSet::new(),
7776
};
7877

79-
let mut vt = CheckLoanVisitor;
80-
vt.visit_block(body, clcx);
78+
clcx.visit_block(body, ());
8179
}
8280

8381
enum MoveError {
@@ -725,13 +723,12 @@ impl<'self> CheckLoanCtxt<'self> {
725723
}
726724
}
727725

728-
fn check_loans_in_fn<'a>(visitor: &mut CheckLoanVisitor,
726+
fn check_loans_in_fn<'a>(this: &mut CheckLoanCtxt<'a>,
729727
fk: &visit::fn_kind,
730728
decl: &ast::fn_decl,
731729
body: &ast::Block,
732730
sp: Span,
733-
id: ast::NodeId,
734-
this: CheckLoanCtxt<'a>) {
731+
id: ast::NodeId) {
735732
match *fk {
736733
visit::fk_item_fn(*) |
737734
visit::fk_method(*) => {
@@ -745,9 +742,9 @@ fn check_loans_in_fn<'a>(visitor: &mut CheckLoanVisitor,
745742
}
746743
}
747744

748-
visit::walk_fn(visitor, fk, decl, body, sp, id, this);
745+
visit::walk_fn(this, fk, decl, body, sp, id, ());
749746

750-
fn check_captured_variables(this: CheckLoanCtxt,
747+
fn check_captured_variables(this: &CheckLoanCtxt,
751748
closure_id: ast::NodeId,
752749
span: Span) {
753750
let cap_vars = this.bccx.capture_map.get(&closure_id);
@@ -765,7 +762,7 @@ fn check_loans_in_fn<'a>(visitor: &mut CheckLoanVisitor,
765762
}
766763
return;
767764

768-
fn check_by_move_capture(this: CheckLoanCtxt,
765+
fn check_by_move_capture(this: &CheckLoanCtxt,
769766
closure_id: ast::NodeId,
770767
cap_var: &moves::CaptureVar,
771768
move_path: @LoanPath) {
@@ -788,16 +785,14 @@ fn check_loans_in_fn<'a>(visitor: &mut CheckLoanVisitor,
788785
}
789786
}
790787

791-
fn check_loans_in_local<'a>(vt: &mut CheckLoanVisitor,
792-
local: @ast::Local,
793-
this: CheckLoanCtxt<'a>) {
794-
visit::walk_local(vt, local, this);
788+
fn check_loans_in_local<'a>(this: &mut CheckLoanCtxt<'a>,
789+
local: @ast::Local) {
790+
visit::walk_local(this, local, ());
795791
}
796792

797-
fn check_loans_in_expr<'a>(vt: &mut CheckLoanVisitor,
798-
expr: @ast::Expr,
799-
this: CheckLoanCtxt<'a>) {
800-
visit::walk_expr(vt, expr, this);
793+
fn check_loans_in_expr<'a>(this: &mut CheckLoanCtxt<'a>,
794+
expr: @ast::Expr) {
795+
visit::walk_expr(this, expr, ());
801796

802797
debug!("check_loans_in_expr(expr=%s)",
803798
expr.repr(this.tcx()));
@@ -848,20 +843,18 @@ fn check_loans_in_expr<'a>(vt: &mut CheckLoanVisitor,
848843
}
849844
}
850845

851-
fn check_loans_in_pat<'a>(vt: &mut CheckLoanVisitor,
852-
pat: @ast::Pat,
853-
this: CheckLoanCtxt<'a>)
846+
fn check_loans_in_pat<'a>(this: &mut CheckLoanCtxt<'a>,
847+
pat: @ast::Pat)
854848
{
855849
this.check_for_conflicting_loans(pat.id);
856850
this.check_move_out_from_id(pat.id, pat.span);
857-
visit::walk_pat(vt, pat, this);
851+
visit::walk_pat(this, pat, ());
858852
}
859853

860-
fn check_loans_in_block<'a>(vt: &mut CheckLoanVisitor,
861-
blk: &ast::Block,
862-
this: CheckLoanCtxt<'a>)
854+
fn check_loans_in_block<'a>(this: &mut CheckLoanCtxt<'a>,
855+
blk: &ast::Block)
863856
{
864-
visit::walk_block(vt, blk, this);
857+
visit::walk_block(this, blk, ());
865858
this.check_for_conflicting_loans(blk.id);
866859
}
867860

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)