Skip to content

Commit 339027e

Browse files
committed
Move unchanging portions of Context over to the Visitor.
1 parent bc13365 commit 339027e

File tree

1 file changed

+70
-67
lines changed

1 file changed

+70
-67
lines changed

src/librustc/middle/region.rs

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,22 @@ pub struct RegionMaps {
6565

6666
#[deriving(Clone)]
6767
pub struct Context {
68-
sess: Session,
69-
def_map: resolve::DefMap,
70-
71-
// Generated maps:
72-
region_maps: @mut RegionMaps,
73-
7468
// Scope where variables should be parented to
7569
var_parent: Option<ast::NodeId>,
7670

7771
// Innermost enclosing expression
7872
parent: Option<ast::NodeId>,
7973
}
8074

75+
struct RegionResolutionVisitor {
76+
sess: Session,
77+
def_map: resolve::DefMap,
78+
79+
// Generated maps:
80+
region_maps: @mut RegionMaps,
81+
}
82+
83+
8184
impl RegionMaps {
8285
pub fn relate_free_regions(&mut self, sub: FreeRegion, sup: FreeRegion) {
8386
match self.free_region_map.find_mut(&sub) {
@@ -318,24 +321,24 @@ impl RegionMaps {
318321
}
319322

320323
/// Records the current parent (if any) as the parent of `child_id`.
321-
fn parent_to_expr(cx: Context, child_id: ast::NodeId, sp: Span) {
324+
fn parent_to_expr(visitor: &mut RegionResolutionVisitor,
325+
cx: Context, child_id: ast::NodeId, sp: Span) {
322326
debug!("region::parent_to_expr(span=%?)",
323-
cx.sess.codemap.span_to_str(sp));
327+
visitor.sess.codemap.span_to_str(sp));
324328
for parent_id in cx.parent.iter() {
325-
cx.region_maps.record_parent(child_id, *parent_id);
329+
visitor.region_maps.record_parent(child_id, *parent_id);
326330
}
327331
}
328332

329333
fn resolve_block(visitor: &mut RegionResolutionVisitor,
330334
blk: &ast::Block,
331335
cx: Context) {
332336
// Record the parent of this block.
333-
parent_to_expr(cx, blk.id, blk.span);
337+
parent_to_expr(visitor, cx, blk.id, blk.span);
334338

335339
// Descend.
336340
let new_cx = Context {var_parent: Some(blk.id),
337-
parent: Some(blk.id),
338-
..cx};
341+
parent: Some(blk.id)};
339342
visit::walk_block(visitor, blk, new_cx);
340343
}
341344

@@ -349,7 +352,7 @@ fn resolve_pat(visitor: &mut RegionResolutionVisitor,
349352
pat: @ast::Pat,
350353
cx: Context) {
351354
assert_eq!(cx.var_parent, cx.parent);
352-
parent_to_expr(cx, pat.id, pat.span);
355+
parent_to_expr(visitor, cx, pat.id, pat.span);
353356
visit::walk_pat(visitor, pat, cx);
354357
}
355358

@@ -362,18 +365,18 @@ fn resolve_stmt(visitor: &mut RegionResolutionVisitor,
362365
}
363366
ast::StmtExpr(_, stmt_id) |
364367
ast::StmtSemi(_, stmt_id) => {
365-
parent_to_expr(cx, stmt_id, stmt.span);
368+
parent_to_expr(visitor, cx, stmt_id, stmt.span);
366369
let expr_cx = Context {parent: Some(stmt_id), ..cx};
367370
visit::walk_stmt(visitor, stmt, expr_cx);
368371
}
369-
ast::StmtMac(*) => cx.sess.bug("unexpanded macro")
372+
ast::StmtMac(*) => visitor.sess.bug("unexpanded macro")
370373
}
371374
}
372375

373376
fn resolve_expr(visitor: &mut RegionResolutionVisitor,
374377
expr: @ast::Expr,
375378
cx: Context) {
376-
parent_to_expr(cx, expr.id, expr.span);
379+
parent_to_expr(visitor, cx, expr.id, expr.span);
377380

378381
let mut new_cx = cx;
379382
new_cx.parent = Some(expr.id);
@@ -415,7 +418,7 @@ fn resolve_local(visitor: &mut RegionResolutionVisitor,
415418
local: @ast::Local,
416419
cx: Context) {
417420
assert_eq!(cx.var_parent, cx.parent);
418-
parent_to_expr(cx, local.id, local.span);
421+
parent_to_expr(visitor, cx, local.id, local.span);
419422
visit::walk_local(visitor, local, cx);
420423
}
421424

@@ -439,7 +442,7 @@ fn resolve_fn(visitor: &mut RegionResolutionVisitor,
439442
body.id=%?, \
440443
cx.parent=%?)",
441444
id,
442-
cx.sess.codemap.span_to_str(sp),
445+
visitor.sess.codemap.span_to_str(sp),
443446
body.id,
444447
cx.parent);
445448

@@ -449,7 +452,7 @@ fn resolve_fn(visitor: &mut RegionResolutionVisitor,
449452
..cx};
450453
match *fk {
451454
visit::fk_method(_, _, method) => {
452-
cx.region_maps.record_parent(method.self_id, body.id);
455+
visitor.region_maps.record_parent(method.self_id, body.id);
453456
}
454457
_ => {}
455458
}
@@ -470,8 +473,6 @@ fn resolve_fn(visitor: &mut RegionResolutionVisitor,
470473
visitor.visit_block(body, body_cx);
471474
}
472475

473-
struct RegionResolutionVisitor;
474-
475476
impl Visitor<Context> for RegionResolutionVisitor {
476477

477478
fn visit_block(&mut self, b:&Block, cx:Context) {
@@ -511,12 +512,13 @@ pub fn resolve_crate(sess: Session,
511512
free_region_map: HashMap::new(),
512513
cleanup_scopes: HashSet::new(),
513514
};
514-
let cx = Context {sess: sess,
515-
def_map: def_map,
516-
region_maps: region_maps,
517-
parent: None,
515+
let cx = Context {parent: None,
518516
var_parent: None};
519-
let mut visitor = RegionResolutionVisitor;
517+
let mut visitor = RegionResolutionVisitor {
518+
sess: sess,
519+
def_map: def_map,
520+
region_maps: region_maps,
521+
};
520522
visit::walk_crate(&mut visitor, crate, cx);
521523
return region_maps;
522524
}
@@ -733,10 +735,9 @@ impl DetermineRpCtxt {
733735
}
734736

735737
fn determine_rp_in_item(visitor: &mut DetermineRpVisitor,
736-
item: @ast::item,
737-
cx: @mut DetermineRpCtxt) {
738-
do cx.with(item.id, true) {
739-
visit::walk_item(visitor, item, cx);
738+
item: @ast::item) {
739+
do visitor.cx.with(item.id, true) {
740+
visit::walk_item(visitor, item, ());
740741
}
741742
}
742743

@@ -745,32 +746,33 @@ fn determine_rp_in_fn(visitor: &mut DetermineRpVisitor,
745746
decl: &ast::fn_decl,
746747
body: &ast::Block,
747748
_: Span,
748-
_: ast::NodeId,
749-
cx: @mut DetermineRpCtxt) {
749+
_: ast::NodeId) {
750+
let cx = visitor.cx;
750751
do cx.with(cx.item_id, false) {
751752
do cx.with_ambient_variance(rv_contravariant) {
752753
for a in decl.inputs.iter() {
753-
visitor.visit_ty(&a.ty, cx);
754+
visitor.visit_ty(&a.ty, ());
754755
}
755756
}
756-
visitor.visit_ty(&decl.output, cx);
757+
visitor.visit_ty(&decl.output, ());
757758
let generics = visit::generics_of_fn(fk);
758-
visitor.visit_generics(&generics, cx);
759-
visitor.visit_block(body, cx);
759+
visitor.visit_generics(&generics, ());
760+
visitor.visit_block(body, ());
760761
}
761762
}
762763

763764
fn determine_rp_in_ty_method(visitor: &mut DetermineRpVisitor,
764-
ty_m: &ast::TypeMethod,
765-
cx: @mut DetermineRpCtxt) {
765+
ty_m: &ast::TypeMethod) {
766+
let cx = visitor.cx;
766767
do cx.with(cx.item_id, false) {
767-
visit::walk_ty_method(visitor, ty_m, cx);
768+
visit::walk_ty_method(visitor, ty_m, ());
768769
}
769770
}
770771

771772
fn determine_rp_in_ty(visitor: &mut DetermineRpVisitor,
772-
ty: &ast::Ty,
773-
cx: @mut DetermineRpCtxt) {
773+
ty: &ast::Ty) {
774+
let cx = visitor.cx;
775+
774776
// we are only interested in types that will require an item to
775777
// be region-parameterized. if cx.item_id is zero, then this type
776778
// is not a member of a type defn nor is it a constitutent of an
@@ -854,14 +856,14 @@ fn determine_rp_in_ty(visitor: &mut DetermineRpVisitor,
854856
match ty.node {
855857
ast::ty_box(ref mt) | ast::ty_uniq(ref mt) | ast::ty_vec(ref mt) |
856858
ast::ty_rptr(_, ref mt) | ast::ty_ptr(ref mt) => {
857-
visit_mt(visitor, mt, cx);
859+
visit_mt(visitor, mt);
858860
}
859861

860862
ast::ty_path(ref path, _, _) => {
861863
// type parameters are---for now, anyway---always invariant
862864
do cx.with_ambient_variance(rv_invariant) {
863865
for tp in path.segments.iter().flat_map(|s| s.types.iter()) {
864-
visitor.visit_ty(tp, cx);
866+
visitor.visit_ty(tp, ());
865867
}
866868
}
867869
}
@@ -874,57 +876,58 @@ fn determine_rp_in_ty(visitor: &mut DetermineRpVisitor,
874876
// parameters are contravariant
875877
do cx.with_ambient_variance(rv_contravariant) {
876878
for a in decl.inputs.iter() {
877-
visitor.visit_ty(&a.ty, cx);
879+
visitor.visit_ty(&a.ty, ());
878880
}
879881
}
880-
visitor.visit_ty(&decl.output, cx);
882+
visitor.visit_ty(&decl.output, ());
881883
}
882884
}
883885

884886
_ => {
885-
visit::walk_ty(visitor, ty, cx);
887+
visit::walk_ty(visitor, ty, ());
886888
}
887889
}
888890

889891
fn visit_mt(visitor: &mut DetermineRpVisitor,
890-
mt: &ast::mt,
891-
cx: @mut DetermineRpCtxt) {
892+
mt: &ast::mt) {
893+
let cx = visitor.cx;
892894
// mutability is invariant
893895
if mt.mutbl == ast::MutMutable {
894896
do cx.with_ambient_variance(rv_invariant) {
895-
visitor.visit_ty(mt.ty, cx);
897+
visitor.visit_ty(mt.ty, ());
896898
}
897899
} else {
898-
visitor.visit_ty(mt.ty, cx);
900+
visitor.visit_ty(mt.ty, ());
899901
}
900902
}
901903
}
902904

903905
fn determine_rp_in_struct_field(visitor: &mut DetermineRpVisitor,
904-
cm: @ast::struct_field,
905-
cx: @mut DetermineRpCtxt) {
906-
visit::walk_struct_field(visitor, cm, cx);
906+
cm: @ast::struct_field) {
907+
visit::walk_struct_field(visitor, cm, ());
907908
}
908909

909-
struct DetermineRpVisitor;
910+
struct DetermineRpVisitor {
911+
cx: @mut DetermineRpCtxt
912+
}
910913

911-
impl Visitor<@mut DetermineRpCtxt> for DetermineRpVisitor {
914+
impl Visitor<()> for DetermineRpVisitor {
912915

913916
fn visit_fn(&mut self, fk:&fn_kind, fd:&fn_decl,
914-
b:&Block, s:Span, n:NodeId, e:@mut DetermineRpCtxt) {
915-
determine_rp_in_fn(self, fk, fd, b, s, n, e);
917+
b:&Block, s:Span, n:NodeId, _:()) {
918+
determine_rp_in_fn(self, fk, fd, b, s, n);
916919
}
917-
fn visit_item(&mut self, i:@item, e:@mut DetermineRpCtxt) {
918-
determine_rp_in_item(self, i, e);
920+
fn visit_item(&mut self, i:@item, _:()) {
921+
determine_rp_in_item(self, i);
919922
}
920-
fn visit_ty(&mut self, t:&Ty, e:@mut DetermineRpCtxt) {
921-
determine_rp_in_ty(self, t, e);
923+
fn visit_ty(&mut self, t:&Ty, _:()) {
924+
determine_rp_in_ty(self, t);
922925
}
923-
fn visit_ty_method(&mut self, t:&TypeMethod, e:@mut DetermineRpCtxt) {
924-
determine_rp_in_ty_method(self, t, e);
926+
fn visit_ty_method(&mut self, t:&TypeMethod, _:()) {
927+
determine_rp_in_ty_method(self, t);
925928
}
926-
fn visit_struct_field(&mut self, s:@struct_field, e:@mut DetermineRpCtxt) {
927-
determine_rp_in_struct_field(self, s, e);
929+
fn visit_struct_field(&mut self, s:@struct_field, _:()) {
930+
determine_rp_in_struct_field(self, s);
928931
}
929932

930933
}
@@ -947,8 +950,8 @@ pub fn determine_rp_in_crate(sess: Session,
947950
};
948951

949952
// Gather up the base set, worklist and dep_map
950-
let mut visitor = DetermineRpVisitor;
951-
visit::walk_crate(&mut visitor, crate, cx);
953+
let mut visitor = DetermineRpVisitor { cx: cx };
954+
visit::walk_crate(&mut visitor, crate, ());
952955

953956
// Propagate indirect dependencies
954957
//

0 commit comments

Comments
 (0)