Skip to content

Commit 1483020

Browse files
committed
Port regionck from oldvisit to <V:Visitor> trait API.
1 parent 41a60ab commit 1483020

File tree

1 file changed

+45
-39
lines changed

1 file changed

+45
-39
lines changed

src/librustc/middle/typeck/check/regionck.rs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ use syntax::ast::{ManagedSigil, OwnedSigil, BorrowedSigil};
4343
use syntax::ast::{def_arg, def_binding, def_local, def_self, def_upvar};
4444
use syntax::ast;
4545
use syntax::codemap::span;
46-
use syntax::oldvisit;
46+
use syntax::visit;
47+
use syntax::visit::Visitor;
4748

4849
pub struct Rcx {
4950
fcx: @mut FnCtxt,
@@ -53,8 +54,6 @@ pub struct Rcx {
5354
repeating_scope: ast::NodeId,
5455
}
5556

56-
pub type rvt = oldvisit::vt<@mut Rcx>;
57-
5857
fn encl_region_of_def(fcx: @mut FnCtxt, def: ast::def) -> ty::Region {
5958
let tcx = fcx.tcx();
6059
match def {
@@ -146,8 +145,8 @@ pub fn regionck_expr(fcx: @mut FnCtxt, e: @ast::expr) {
146145
repeating_scope: e.id };
147146
if fcx.err_count_since_creation() == 0 {
148147
// regionck assumes typeck succeeded
149-
let v = regionck_visitor();
150-
(v.visit_expr)(e, (rcx, v));
148+
let mut v = regionck_visitor();
149+
v.visit_expr(e, rcx);
151150
}
152151
fcx.infcx().resolve_regions();
153152
}
@@ -157,55 +156,62 @@ pub fn regionck_fn(fcx: @mut FnCtxt, blk: &ast::Block) {
157156
repeating_scope: blk.id };
158157
if fcx.err_count_since_creation() == 0 {
159158
// regionck assumes typeck succeeded
160-
let v = regionck_visitor();
161-
(v.visit_block)(blk, (rcx, v));
159+
let mut v = regionck_visitor();
160+
v.visit_block(blk, rcx);
162161
}
163162
fcx.infcx().resolve_regions();
164163
}
165164

166-
fn regionck_visitor() -> rvt {
165+
struct RegionckVisitor;
166+
167+
impl Visitor<@mut Rcx> for RegionckVisitor {
167168
// (*) FIXME(#3238) should use visit_pat, not visit_arm/visit_local,
168169
// However, right now we run into an issue whereby some free
169170
// regions are not properly related if they appear within the
170171
// types of arguments that must be inferred. This could be
171172
// addressed by deferring the construction of the region
172173
// hierarchy, and in particular the relationships between free
173174
// regions, until regionck, as described in #3238.
174-
oldvisit::mk_vt(@oldvisit::Visitor {
175-
visit_item: visit_item,
176-
visit_expr: visit_expr,
175+
176+
fn visit_item(&mut self, i:@ast::item, e:@mut Rcx) { visit_item(self, i, e); }
177+
178+
fn visit_expr(&mut self, ex:@ast::expr, e:@mut Rcx) { visit_expr(self, ex, e); }
177179

178180
//visit_pat: visit_pat, // (*) see above
179-
visit_arm: visit_arm,
180-
visit_local: visit_local,
181181

182-
visit_block: visit_block,
183-
.. *oldvisit::default_visitor()
184-
})
182+
fn visit_arm(&mut self, a:&ast::arm, e:@mut Rcx) { visit_arm(self, a, e); }
183+
184+
fn visit_local(&mut self, l:@ast::Local, e:@mut Rcx) { visit_local(self, l, e); }
185+
186+
fn visit_block(&mut self, b:&ast::Block, e:@mut Rcx) { visit_block(self, b, e); }
187+
}
188+
189+
fn regionck_visitor() -> RegionckVisitor {
190+
RegionckVisitor
185191
}
186192

187-
fn visit_item(_item: @ast::item, (_rcx, _v): (@mut Rcx, rvt)) {
193+
fn visit_item(_v: &mut RegionckVisitor, _item: @ast::item, _rcx: @mut Rcx) {
188194
// Ignore items
189195
}
190196

191-
fn visit_block(b: &ast::Block, (rcx, v): (@mut Rcx, rvt)) {
197+
fn visit_block(v: &mut RegionckVisitor, b: &ast::Block, rcx: @mut Rcx) {
192198
rcx.fcx.tcx().region_maps.record_cleanup_scope(b.id);
193-
oldvisit::visit_block(b, (rcx, v));
199+
visit::walk_block(v, b, rcx);
194200
}
195201

196-
fn visit_arm(arm: &ast::arm, (rcx, v): (@mut Rcx, rvt)) {
202+
fn visit_arm(v: &mut RegionckVisitor, arm: &ast::arm, rcx: @mut Rcx) {
197203
// see above
198204
for &p in arm.pats.iter() {
199205
constrain_bindings_in_pat(p, rcx);
200206
}
201207

202-
oldvisit::visit_arm(arm, (rcx, v));
208+
visit::walk_arm(v, arm, rcx);
203209
}
204210

205-
fn visit_local(l: @ast::Local, (rcx, v): (@mut Rcx, rvt)) {
211+
fn visit_local(v: &mut RegionckVisitor, l: @ast::Local, rcx: @mut Rcx) {
206212
// see above
207213
constrain_bindings_in_pat(l.pat, rcx);
208-
oldvisit::visit_local(l, (rcx, v));
214+
visit::walk_local(v, l, rcx);
209215
}
210216

211217
fn constrain_bindings_in_pat(pat: @ast::pat, rcx: @mut Rcx) {
@@ -242,7 +248,7 @@ fn constrain_bindings_in_pat(pat: @ast::pat, rcx: @mut Rcx) {
242248
}
243249
}
244250

245-
fn visit_expr(expr: @ast::expr, (rcx, v): (@mut Rcx, rvt)) {
251+
fn visit_expr(v: &mut RegionckVisitor, expr: @ast::expr, rcx: @mut Rcx) {
246252
debug!("regionck::visit_expr(e=%s, repeating_scope=%?)",
247253
expr.repr(rcx.fcx.tcx()), rcx.repeating_scope);
248254

@@ -330,13 +336,13 @@ fn visit_expr(expr: @ast::expr, (rcx, v): (@mut Rcx, rvt)) {
330336
constrain_callee(rcx, callee.id, expr, callee);
331337
constrain_call(rcx, callee.id, expr, None, *args, false);
332338

333-
oldvisit::visit_expr(expr, (rcx, v));
339+
visit::walk_expr(v, expr, rcx);
334340
}
335341

336342
ast::expr_method_call(callee_id, arg0, _, _, ref args, _) => {
337343
constrain_call(rcx, callee_id, expr, Some(arg0), *args, false);
338344

339-
oldvisit::visit_expr(expr, (rcx, v));
345+
visit::walk_expr(v,expr, rcx);
340346
}
341347

342348
ast::expr_index(callee_id, lhs, rhs) |
@@ -348,30 +354,30 @@ fn visit_expr(expr: @ast::expr, (rcx, v): (@mut Rcx, rvt)) {
348354
// should be converted to an adjustment!
349355
constrain_call(rcx, callee_id, expr, Some(lhs), [rhs], true);
350356

351-
oldvisit::visit_expr(expr, (rcx, v));
357+
visit::walk_expr(v, expr, rcx);
352358
}
353359

354360
ast::expr_unary(callee_id, _, lhs) if has_method_map => {
355361
// As above.
356362
constrain_call(rcx, callee_id, expr, Some(lhs), [], true);
357363

358-
oldvisit::visit_expr(expr, (rcx, v));
364+
visit::walk_expr(v, expr, rcx);
359365
}
360366

361367
ast::expr_unary(_, ast::deref, base) => {
362368
// For *a, the lifetime of a must enclose the deref
363369
let base_ty = rcx.resolve_node_type(base.id);
364370
constrain_derefs(rcx, expr, 1, base_ty);
365371

366-
oldvisit::visit_expr(expr, (rcx, v));
372+
visit::walk_expr(v, expr, rcx);
367373
}
368374

369375
ast::expr_index(_, vec_expr, _) => {
370376
// For a[b], the lifetime of a must enclose the deref
371377
let vec_type = rcx.resolve_expr_type_adjusted(vec_expr);
372378
constrain_index(rcx, expr, vec_type);
373379

374-
oldvisit::visit_expr(expr, (rcx, v));
380+
visit::walk_expr(v, expr, rcx);
375381
}
376382

377383
ast::expr_cast(source, _) => {
@@ -401,7 +407,7 @@ fn visit_expr(expr: @ast::expr, (rcx, v): (@mut Rcx, rvt)) {
401407
_ => ()
402408
}
403409

404-
oldvisit::visit_expr(expr, (rcx, v));
410+
visit::walk_expr(v, expr, rcx);
405411
}
406412

407413
ast::expr_addr_of(_, base) => {
@@ -417,13 +423,13 @@ fn visit_expr(expr: @ast::expr, (rcx, v): (@mut Rcx, rvt)) {
417423
let ty0 = rcx.resolve_node_type(expr.id);
418424
constrain_regions_in_type(rcx, ty::re_scope(expr.id),
419425
infer::AddrOf(expr.span), ty0);
420-
oldvisit::visit_expr(expr, (rcx, v));
426+
visit::walk_expr(v, expr, rcx);
421427
}
422428

423429
ast::expr_match(discr, ref arms) => {
424430
guarantor::for_match(rcx, discr, *arms);
425431

426-
oldvisit::visit_expr(expr, (rcx, v));
432+
visit::walk_expr(v, expr, rcx);
427433
}
428434

429435
ast::expr_fn_block(*) => {
@@ -432,29 +438,29 @@ fn visit_expr(expr: @ast::expr, (rcx, v): (@mut Rcx, rvt)) {
432438

433439
ast::expr_loop(ref body, _) => {
434440
let repeating_scope = rcx.set_repeating_scope(body.id);
435-
oldvisit::visit_expr(expr, (rcx, v));
441+
visit::walk_expr(v, expr, rcx);
436442
rcx.set_repeating_scope(repeating_scope);
437443
}
438444

439445
ast::expr_while(cond, ref body) => {
440446
let repeating_scope = rcx.set_repeating_scope(cond.id);
441-
(v.visit_expr)(cond, (rcx, v));
447+
v.visit_expr(cond, rcx);
442448

443449
rcx.set_repeating_scope(body.id);
444-
(v.visit_block)(body, (rcx, v));
450+
v.visit_block(body, rcx);
445451

446452
rcx.set_repeating_scope(repeating_scope);
447453
}
448454

449455
_ => {
450-
oldvisit::visit_expr(expr, (rcx, v));
456+
visit::walk_expr(v, expr, rcx);
451457
}
452458
}
453459
}
454460

455461
fn check_expr_fn_block(rcx: @mut Rcx,
456462
expr: @ast::expr,
457-
v: rvt) {
463+
v: &mut RegionckVisitor) {
458464
let tcx = rcx.fcx.tcx();
459465
match expr.node {
460466
ast::expr_fn_block(_, ref body) => {
@@ -483,7 +489,7 @@ fn check_expr_fn_block(rcx: @mut Rcx,
483489
}
484490

485491
let repeating_scope = rcx.set_repeating_scope(body.id);
486-
oldvisit::visit_expr(expr, (rcx, v));
492+
visit::walk_expr(v, expr, rcx);
487493
rcx.set_repeating_scope(repeating_scope);
488494
}
489495

0 commit comments

Comments
 (0)