Skip to content

Commit cc05847

Browse files
committed
librustc: De-@mut the def map.
This is the last `@mut` in `librustc` that does not depend on libsyntax.
1 parent 56b9a20 commit cc05847

30 files changed

+246
-117
lines changed

src/librustc/metadata/encoder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,8 @@ impl<'a,'b> Visitor<()> for ImplVisitor<'a,'b> {
16941694
match item.node {
16951695
item_impl(_, Some(ref trait_ref), _, _) => {
16961696
let def_map = self.ecx.tcx.def_map;
1697-
let trait_def = def_map.get_copy(&trait_ref.ref_id);
1697+
let def_map = def_map.borrow();
1698+
let trait_def = def_map.get().get_copy(&trait_ref.ref_id);
16981699
let def_id = ast_util::def_id_of_def(trait_def);
16991700

17001701
// Load eagerly if this is an implementation of the Drop trait

src/librustc/middle/astencode.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,8 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
910910
debug!("Encoding side tables for id {}", id);
911911

912912
{
913-
let r = tcx.def_map.find(&id);
913+
let def_map = tcx.def_map.borrow();
914+
let r = def_map.get().find(&id);
914915
for def in r.iter() {
915916
ebml_w.tag(c::tag_table_def, |ebml_w| {
916917
ebml_w.id(id);
@@ -1238,7 +1239,8 @@ fn decode_side_tables(xcx: @ExtendedDecodeContext,
12381239
match value {
12391240
c::tag_table_def => {
12401241
let def = decode_def(xcx, val_doc);
1241-
dcx.tcx.def_map.insert(id, def);
1242+
let mut def_map = dcx.tcx.def_map.borrow_mut();
1243+
def_map.get().insert(id, def);
12421244
}
12431245
c::tag_table_node_type => {
12441246
let ty = val_dsr.read_ty(xcx);

src/librustc/middle/cfg/construct.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ impl CFGBuilder {
496496
}
497497

498498
Some(_) => {
499-
match self.tcx.def_map.find(&expr.id) {
499+
let def_map = self.tcx.def_map.borrow();
500+
match def_map.get().find(&expr.id) {
500501
Some(&ast::DefLabel(loop_id)) => {
501502
for l in self.loop_scopes.iter() {
502503
if l.loop_id == loop_id {

src/librustc/middle/check_const.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
148148
e.span, "paths in constants may only refer to \
149149
items without type parameters");
150150
}
151-
match def_map.find(&e.id) {
151+
let def_map = def_map.borrow();
152+
match def_map.get().find(&e.id) {
152153
Some(&DefStatic(..)) |
153154
Some(&DefFn(_, _)) |
154155
Some(&DefVariant(_, _, _)) |
@@ -167,7 +168,8 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
167168
}
168169
}
169170
ExprCall(callee, _, NoSugar) => {
170-
match def_map.find(&callee.id) {
171+
let def_map = def_map.borrow();
172+
match def_map.get().find(&callee.id) {
171173
Some(&DefStruct(..)) => {} // OK.
172174
Some(&DefVariant(..)) => {} // OK.
173175
_ => {
@@ -260,15 +262,19 @@ impl Visitor<()> for CheckItemRecursionVisitor {
260262

261263
fn visit_expr(&mut self, e: @Expr, _: ()) {
262264
match e.node {
263-
ExprPath(..) => match self.env.def_map.find(&e.id) {
264-
Some(&DefStatic(def_id, _)) if ast_util::is_local(def_id) =>
265-
match self.env.ast_map.get_copy(&def_id.node) {
266-
ast_map::node_item(it, _) => {
267-
self.visit_item(it, ());
268-
}
269-
_ => fail!("const not bound to an item")
270-
},
271-
_ => ()
265+
ExprPath(..) => {
266+
let def_map = self.env.def_map.borrow();
267+
match def_map.get().find(&e.id) {
268+
Some(&DefStatic(def_id, _)) if
269+
ast_util::is_local(def_id) =>
270+
match self.env.ast_map.get_copy(&def_id.node) {
271+
ast_map::node_item(it, _) => {
272+
self.visit_item(it, ());
273+
}
274+
_ => fail!("const not bound to an item")
275+
},
276+
_ => ()
277+
}
272278
},
273279
_ => ()
274280
}

src/librustc/middle/check_match.rs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,12 @@ fn check_arms(cx: &MatchCheckCtxt, arms: &[Arm]) {
121121

122122
// Check that we do not match against a static NaN (#6804)
123123
let pat_matches_nan: |&Pat| -> bool = |p| {
124-
match cx.tcx.def_map.find(&p.id) {
125-
Some(&DefStatic(did, false)) => {
124+
let opt_def = {
125+
let def_map = cx.tcx.def_map.borrow();
126+
def_map.get().find_copy(&p.id)
127+
};
128+
match opt_def {
129+
Some(DefStatic(did, false)) => {
126130
let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
127131
match eval_const_expr(cx.tcx, const_expr) {
128132
const_float(f) if f.is_nan() => true,
@@ -334,9 +338,13 @@ fn pat_ctor_id(cx: &MatchCheckCtxt, p: @Pat) -> Option<ctor> {
334338
match pat.node {
335339
PatWild | PatWildMulti => { None }
336340
PatIdent(_, _, _) | PatEnum(_, _) => {
337-
match cx.tcx.def_map.find(&pat.id) {
338-
Some(&DefVariant(_, id, _)) => Some(variant(id)),
339-
Some(&DefStatic(did, false)) => {
341+
let opt_def = {
342+
let def_map = cx.tcx.def_map.borrow();
343+
def_map.get().find_copy(&pat.id)
344+
};
345+
match opt_def {
346+
Some(DefVariant(_, id, _)) => Some(variant(id)),
347+
Some(DefStatic(did, false)) => {
340348
let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
341349
Some(val(eval_const_expr(cx.tcx, const_expr)))
342350
}
@@ -348,7 +356,8 @@ fn pat_ctor_id(cx: &MatchCheckCtxt, p: @Pat) -> Option<ctor> {
348356
Some(range(eval_const_expr(cx.tcx, lo), eval_const_expr(cx.tcx, hi)))
349357
}
350358
PatStruct(..) => {
351-
match cx.tcx.def_map.find(&pat.id) {
359+
let def_map = cx.tcx.def_map.borrow();
360+
match def_map.get().find(&pat.id) {
352361
Some(&DefVariant(_, id, _)) => Some(variant(id)),
353362
_ => Some(single)
354363
}
@@ -370,7 +379,8 @@ fn is_wild(cx: &MatchCheckCtxt, p: @Pat) -> bool {
370379
match pat.node {
371380
PatWild | PatWildMulti => { true }
372381
PatIdent(_, _, _) => {
373-
match cx.tcx.def_map.find(&pat.id) {
382+
let def_map = cx.tcx.def_map.borrow();
383+
match def_map.get().find(&pat.id) {
374384
Some(&DefVariant(_, _, _)) | Some(&DefStatic(..)) => { false }
375385
_ => { true }
376386
}
@@ -551,15 +561,19 @@ fn specialize(cx: &MatchCheckCtxt,
551561
Some(vec::append(vec::from_elem(arity, wild_multi()), r.tail()))
552562
}
553563
PatIdent(_, _, _) => {
554-
match cx.tcx.def_map.find(&pat_id) {
555-
Some(&DefVariant(_, id, _)) => {
564+
let opt_def = {
565+
let def_map = cx.tcx.def_map.borrow();
566+
def_map.get().find_copy(&pat_id)
567+
};
568+
match opt_def {
569+
Some(DefVariant(_, id, _)) => {
556570
if variant(id) == *ctor_id {
557571
Some(r.tail().to_owned())
558572
} else {
559573
None
560574
}
561575
}
562-
Some(&DefStatic(did, _)) => {
576+
Some(DefStatic(did, _)) => {
563577
let const_expr =
564578
lookup_const_by_id(cx.tcx, did).unwrap();
565579
let e_v = eval_const_expr(cx.tcx, const_expr);
@@ -608,7 +622,11 @@ fn specialize(cx: &MatchCheckCtxt,
608622
}
609623
}
610624
PatEnum(_, args) => {
611-
match cx.tcx.def_map.get_copy(&pat_id) {
625+
let opt_def = {
626+
let def_map = cx.tcx.def_map.borrow();
627+
def_map.get().get_copy(&pat_id)
628+
};
629+
match opt_def {
612630
DefStatic(did, _) => {
613631
let const_expr =
614632
lookup_const_by_id(cx.tcx, did).unwrap();
@@ -668,7 +686,11 @@ fn specialize(cx: &MatchCheckCtxt,
668686
}
669687
PatStruct(_, ref pattern_fields, _) => {
670688
// Is this a struct or an enum variant?
671-
match cx.tcx.def_map.get_copy(&pat_id) {
689+
let opt_def = {
690+
let def_map = cx.tcx.def_map.borrow();
691+
def_map.get().get_copy(&pat_id)
692+
};
693+
match opt_def {
672694
DefVariant(_, variant_id, _) => {
673695
if variant(variant_id) == *ctor_id {
674696
let struct_fields = ty::lookup_struct_fields(cx.tcx, variant_id);
@@ -838,13 +860,17 @@ fn check_fn(v: &mut CheckMatchVisitor,
838860
}
839861

840862
fn is_refutable(cx: &MatchCheckCtxt, pat: &Pat) -> bool {
841-
match cx.tcx.def_map.find(&pat.id) {
842-
Some(&DefVariant(enum_id, _, _)) => {
863+
let opt_def = {
864+
let def_map = cx.tcx.def_map.borrow();
865+
def_map.get().find_copy(&pat.id)
866+
};
867+
match opt_def {
868+
Some(DefVariant(enum_id, _, _)) => {
843869
if ty::enum_variants(cx.tcx, enum_id).len() != 1u {
844870
return true;
845871
}
846872
}
847-
Some(&DefStatic(..)) => return true,
873+
Some(DefStatic(..)) => return true,
848874
_ => ()
849875
}
850876

src/librustc/middle/const_eval.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,17 @@ pub fn join_all<It: Iterator<constness>>(mut cs: It) -> constness {
7878
}
7979

8080
pub fn lookup_const(tcx: ty::ctxt, e: &Expr) -> Option<@Expr> {
81-
match tcx.def_map.find(&e.id) {
82-
Some(&ast::DefStatic(def_id, false)) =>
83-
lookup_const_by_id(tcx, def_id),
84-
Some(&ast::DefVariant(enum_def, variant_def, _)) =>
85-
lookup_variant_by_id(tcx, enum_def, variant_def),
81+
let opt_def = {
82+
let def_map = tcx.def_map.borrow();
83+
def_map.get().find_copy(&e.id)
84+
};
85+
match opt_def {
86+
Some(ast::DefStatic(def_id, false)) => {
87+
lookup_const_by_id(tcx, def_id)
88+
}
89+
Some(ast::DefVariant(enum_def, variant_def, _)) => {
90+
lookup_variant_by_id(tcx, enum_def, variant_def)
91+
}
8692
_ => None
8793
}
8894
}

src/librustc/middle/dataflow.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,8 @@ impl<'a, O:DataFlowOperator> PropagationContext<'a, O> {
874874
}
875875

876876
Some(_) => {
877-
match self.tcx().def_map.find(&expr.id) {
877+
let def_map = self.tcx().def_map.borrow();
878+
match def_map.get().find(&expr.id) {
878879
Some(&ast::DefLabel(loop_id)) => {
879880
match loop_scopes.iter().position(|l| l.loop_id == loop_id) {
880881
Some(i) => i,

src/librustc/middle/dead.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ impl MarkSymbolVisitor {
6363
}
6464

6565
fn lookup_and_handle_definition(&mut self, id: &ast::NodeId) {
66-
let def = match self.tcx.def_map.find(id) {
66+
let def_map = self.tcx.def_map.borrow();
67+
let def = match def_map.get().find(id) {
6768
Some(&def) => def,
6869
None => return
6970
};

src/librustc/middle/freevars.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ impl Visitor<int> for CollectFreevarsVisitor {
5252
}
5353
ast::ExprPath(..) | ast::ExprSelf => {
5454
let mut i = 0;
55-
match self.def_map.find(&expr.id) {
55+
let def_map = self.def_map.borrow();
56+
match def_map.get().find(&expr.id) {
5657
None => fail!("path not found"),
5758
Some(&df) => {
5859
let mut def = df;

src/librustc/middle/kind.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ fn check_struct_safe_for_destructor(cx: &mut Context,
116116
}
117117

118118
fn check_impl_of_trait(cx: &mut Context, it: @item, trait_ref: &trait_ref, self_type: &Ty) {
119-
let ast_trait_def = cx.tcx.def_map.find(&trait_ref.ref_id)
120-
.expect("trait ref not in def map!");
119+
let def_map = cx.tcx.def_map.borrow();
120+
let ast_trait_def = def_map.get()
121+
.find(&trait_ref.ref_id)
122+
.expect("trait ref not in def map!");
121123
let trait_def_id = ast_util::def_id_of_def(*ast_trait_def);
122124
let trait_def;
123125
{
@@ -144,7 +146,7 @@ fn check_impl_of_trait(cx: &mut Context, it: @item, trait_ref: &trait_ref, self_
144146
match self_type.node {
145147
ty_path(_, ref bounds, path_node_id) => {
146148
assert!(bounds.is_none());
147-
let struct_def = cx.tcx.def_map.get_copy(&path_node_id);
149+
let struct_def = def_map.get().get_copy(&path_node_id);
148150
let struct_did = ast_util::def_id_of_def(struct_def);
149151
check_struct_safe_for_destructor(cx, self_type.span, struct_did);
150152
}
@@ -272,9 +274,11 @@ pub fn check_expr(cx: &mut Context, e: @Expr) {
272274
let node_type_substs = cx.tcx.node_type_substs.borrow();
273275
let r = node_type_substs.get().find(&type_parameter_id);
274276
for ts in r.iter() {
277+
let def_map = cx.tcx.def_map.borrow();
275278
let type_param_defs = match e.node {
276279
ExprPath(_) => {
277-
let did = ast_util::def_id_of_def(cx.tcx.def_map.get_copy(&e.id));
280+
let did = ast_util::def_id_of_def(def_map.get()
281+
.get_copy(&e.id));
278282
ty::lookup_item_type(cx.tcx, did).generics.type_param_defs
279283
}
280284
_ => {
@@ -335,7 +339,8 @@ fn check_ty(cx: &mut Context, aty: &Ty) {
335339
let node_type_substs = cx.tcx.node_type_substs.borrow();
336340
let r = node_type_substs.get().find(&id);
337341
for ts in r.iter() {
338-
let did = ast_util::def_id_of_def(cx.tcx.def_map.get_copy(&id));
342+
let def_map = cx.tcx.def_map.borrow();
343+
let did = ast_util::def_id_of_def(def_map.get().get_copy(&id));
339344
let type_param_defs =
340345
ty::lookup_item_type(cx.tcx, did).generics.type_param_defs;
341346
for (&ty, type_param_def) in ts.iter().zip(type_param_defs.iter()) {

src/librustc/middle/lint.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ fn check_item_ctypes(cx: &Context, it: &ast::item) {
712712
fn check_ty(cx: &Context, ty: &ast::Ty) {
713713
match ty.node {
714714
ast::ty_path(_, _, id) => {
715-
match cx.tcx.def_map.get_copy(&id) {
715+
let def_map = cx.tcx.def_map.borrow();
716+
match def_map.get().get_copy(&id) {
716717
ast::DefPrimTy(ast::ty_int(ast::ty_i)) => {
717718
cx.span_lint(ctypes, ty.span,
718719
"found rust type `int` in foreign module, while \
@@ -983,7 +984,8 @@ fn check_item_non_uppercase_statics(cx: &Context, it: &ast::item) {
983984

984985
fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
985986
// Lint for constants that look like binding identifiers (#7526)
986-
match (&p.node, cx.tcx.def_map.find(&p.id)) {
987+
let def_map = cx.tcx.def_map.borrow();
988+
match (&p.node, def_map.get().find(&p.id)) {
987989
(&ast::PatIdent(_, ref path, _), Some(&ast::DefStatic(_, false))) => {
988990
// last identifier alone is right choice for this lint.
989991
let ident = path.segments.last().identifier;
@@ -1197,7 +1199,8 @@ fn check_missing_doc_variant(cx: &Context, v: &ast::variant) {
11971199
fn check_stability(cx: &Context, e: &ast::Expr) {
11981200
let id = match e.node {
11991201
ast::ExprPath(..) | ast::ExprStruct(..) => {
1200-
match cx.tcx.def_map.find(&e.id) {
1202+
let def_map = cx.tcx.def_map.borrow();
1203+
match def_map.get().find(&e.id) {
12011204
Some(&def) => ast_util::def_id_of_def(def),
12021205
None => return
12031206
}

src/librustc/middle/liveness.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,8 @@ fn visit_expr(v: &mut LivenessVisitor, expr: @Expr, this: @IrMaps) {
486486
match expr.node {
487487
// live nodes required for uses or definitions of variables:
488488
ExprPath(_) | ExprSelf => {
489-
let def = this.tcx.def_map.get_copy(&expr.id);
489+
let def_map = this.tcx.def_map.borrow();
490+
let def = def_map.get().get_copy(&expr.id);
490491
debug!("expr {}: path that leads to {:?}", expr.id, def);
491492
if moves::moved_variable_node_id_from_def(def).is_some() {
492493
this.add_live_node_for_node(expr.id, ExprNode(expr.span));
@@ -755,13 +756,16 @@ impl Liveness {
755756
sp: Span)
756757
-> NodeId {
757758
match opt_label {
758-
Some(_) => // Refers to a labeled loop. Use the results of resolve
759-
// to find with one
760-
match self.tcx.def_map.find(&id) {
759+
Some(_) => {
760+
// Refers to a labeled loop. Use the results of resolve
761+
// to find with one
762+
let def_map = self.tcx.def_map.borrow();
763+
match def_map.get().find(&id) {
761764
Some(&DefLabel(loop_id)) => loop_id,
762765
_ => self.tcx.sess.span_bug(sp, "Label on break/loop \
763766
doesn't refer to a loop")
764-
},
767+
}
768+
}
765769
None => {
766770
// Vanilla 'break' or 'loop', so use the enclosing
767771
// loop scope
@@ -1344,7 +1348,8 @@ impl Liveness {
13441348

13451349
pub fn access_path(&self, expr: &Expr, succ: LiveNode, acc: uint)
13461350
-> LiveNode {
1347-
let def = self.tcx.def_map.get_copy(&expr.id);
1351+
let def_map = self.tcx.def_map.borrow();
1352+
let def = def_map.get().get_copy(&expr.id);
13481353
match moves::moved_variable_node_id_from_def(def) {
13491354
Some(nid) => {
13501355
let ln = self.live_node(expr.id, expr.span);
@@ -1572,7 +1577,8 @@ impl Liveness {
15721577
pub fn check_lvalue(&mut self, expr: @Expr) {
15731578
match expr.node {
15741579
ExprPath(_) => {
1575-
match self.tcx.def_map.get_copy(&expr.id) {
1580+
let def_map = self.tcx.def_map.borrow();
1581+
match def_map.get().get_copy(&expr.id) {
15761582
DefLocal(nid, _) => {
15771583
// Assignment to an immutable variable or argument: only legal
15781584
// if there is no later assignment. If this local is actually

0 commit comments

Comments
 (0)