Skip to content

Commit 2c3f926

Browse files
committed
librustc: De-@mut the type cache in the type context
1 parent b976226 commit 2c3f926

File tree

5 files changed

+120
-59
lines changed

5 files changed

+120
-59
lines changed

src/librustc/middle/astencode.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,8 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
951951

952952
let lid = ast::DefId { crate: ast::LOCAL_CRATE, node: id };
953953
{
954-
let r = tcx.tcache.find(&lid);
954+
let tcache = tcx.tcache.borrow();
955+
let r = tcache.get().find(&lid);
955956
for &tpbt in r.iter() {
956957
ebml_w.tag(c::tag_table_tcache, |ebml_w| {
957958
ebml_w.id(id);
@@ -1247,7 +1248,8 @@ fn decode_side_tables(xcx: @ExtendedDecodeContext,
12471248
c::tag_table_tcache => {
12481249
let tpbt = val_dsr.read_ty_param_bounds_and_ty(xcx);
12491250
let lid = ast::DefId { crate: ast::LOCAL_CRATE, node: id };
1250-
dcx.tcx.tcache.insert(lid, tpbt);
1251+
let mut tcache = dcx.tcx.tcache.borrow_mut();
1252+
tcache.get().insert(lid, tpbt);
12511253
}
12521254
c::tag_table_param_defs => {
12531255
let bounds = val_dsr.read_type_param_def(xcx);

src/librustc/middle/ty.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ pub struct ty_param_substs_and_ty {
953953
ty: ty::t
954954
}
955955

956-
type type_cache = @mut HashMap<ast::DefId, ty_param_bounds_and_ty>;
956+
type type_cache = RefCell<HashMap<ast::DefId, ty_param_bounds_and_ty>>;
957957

958958
pub type node_type_table = @mut HashMap<uint,t>;
959959

@@ -986,7 +986,7 @@ pub fn mk_ctxt(s: session::Session,
986986
items: amap,
987987
intrinsic_defs: RefCell::new(HashMap::new()),
988988
freevars: freevars,
989-
tcache: @mut HashMap::new(),
989+
tcache: RefCell::new(HashMap::new()),
990990
rcache: mk_rcache(),
991991
short_names_cache: RefCell::new(HashMap::new()),
992992
needs_unwind_cleanup_cache: RefCell::new(HashMap::new()),
@@ -3975,8 +3975,9 @@ pub fn enum_variant_with_id(cx: ctxt,
39753975
pub fn lookup_item_type(cx: ctxt,
39763976
did: ast::DefId)
39773977
-> ty_param_bounds_and_ty {
3978+
let mut tcache = cx.tcache.borrow_mut();
39783979
lookup_locally_or_in_crate_store(
3979-
"tcache", did, cx.tcache,
3980+
"tcache", did, tcache.get(),
39803981
|| csearch::get_type(cx, did))
39813982
}
39823983

@@ -4071,15 +4072,17 @@ pub fn lookup_field_type(tcx: ctxt,
40714072
-> ty::t {
40724073
let t = if id.crate == ast::LOCAL_CRATE {
40734074
node_id_to_type(tcx, id.node)
4074-
}
4075-
else {
4076-
match tcx.tcache.find(&id) {
4077-
Some(&ty_param_bounds_and_ty {ty, ..}) => ty,
4078-
None => {
4079-
let tpt = csearch::get_field_type(tcx, struct_id, id);
4080-
tcx.tcache.insert(id, tpt);
4081-
tpt.ty
4082-
}
4075+
} else {
4076+
{
4077+
let mut tcache = tcx.tcache.borrow_mut();
4078+
match tcache.get().find(&id) {
4079+
Some(&ty_param_bounds_and_ty {ty, ..}) => ty,
4080+
None => {
4081+
let tpt = csearch::get_field_type(tcx, struct_id, id);
4082+
tcache.get().insert(id, tpt);
4083+
tpt.ty
4084+
}
4085+
}
40834086
}
40844087
};
40854088
subst(tcx, substs, t)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3446,7 +3446,10 @@ pub fn check_const(ccx: @mut CrateCtxt,
34463446
id: ast::NodeId) {
34473447
let rty = ty::node_id_to_type(ccx.tcx, id);
34483448
let fcx = blank_fn_ctxt(ccx, rty, e.id);
3449-
let declty = fcx.ccx.tcx.tcache.get(&local_def(id)).ty;
3449+
let declty = {
3450+
let tcache = fcx.ccx.tcx.tcache.borrow();
3451+
tcache.get().get(&local_def(id)).ty
3452+
};
34503453
check_const_with_ty(fcx, sp, e, declty);
34513454
}
34523455

src/librustc/middle/typeck/coherence.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,10 @@ impl CoherenceChecker {
366366
};
367367
debug!("new_polytype={}", new_polytype.repr(tcx));
368368

369-
tcx.tcache.insert(new_did, new_polytype);
369+
{
370+
let mut tcache = tcx.tcache.borrow_mut();
371+
tcache.get().insert(new_did, new_polytype);
372+
}
370373

371374
let mut methods = tcx.methods.borrow_mut();
372375
methods.get().insert(new_did, new_method_ty);
@@ -528,7 +531,8 @@ impl CoherenceChecker {
528531

529532
pub fn get_self_type_for_implementation(&self, implementation: @Impl)
530533
-> ty_param_bounds_and_ty {
531-
return self.crate_context.tcx.tcache.get_copy(&implementation.did);
534+
let tcache = self.crate_context.tcx.tcache.borrow();
535+
return tcache.get().get_copy(&implementation.did);
532536
}
533537

534538
// Privileged scope checking

src/librustc/middle/typeck/collect.rs

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,12 @@ pub fn get_enum_variant_types(ccx: &CrateCtxt,
173173
generics: ty_generics(ccx, generics, 0),
174174
ty: result_ty
175175
};
176-
tcx.tcache.insert(local_def(variant.node.id), tpt);
176+
177+
{
178+
let mut tcache = tcx.tcache.borrow_mut();
179+
tcache.get().insert(local_def(variant.node.id), tpt);
180+
}
181+
177182
write_ty_to_tcx(tcx, variant.node.id, result_ty);
178183
}
179184
}
@@ -350,7 +355,8 @@ pub fn ensure_trait_methods(ccx: &CrateCtxt,
350355
ty.repr(tcx),
351356
substs.repr(tcx));
352357

353-
tcx.tcache.insert(m.def_id,
358+
let mut tcache = tcx.tcache.borrow_mut();
359+
tcache.get().insert(m.def_id,
354360
ty_param_bounds_and_ty {
355361
generics: ty::Generics {
356362
type_param_defs: @new_type_param_defs,
@@ -440,7 +446,8 @@ pub fn convert_field(ccx: &CrateCtxt,
440446
let tt = ccx.to_ty(&ExplicitRscope, v.node.ty);
441447
write_ty_to_tcx(ccx.tcx, v.node.id, tt);
442448
/* add the field to the tcache */
443-
ccx.tcx.tcache.insert(local_def(v.node.id),
449+
let mut tcache = ccx.tcx.tcache.borrow_mut();
450+
tcache.get().insert(local_def(v.node.id),
444451
ty::ty_param_bounds_and_ty {
445452
generics: struct_generics.clone(),
446453
ty: tt
@@ -470,20 +477,25 @@ fn convert_methods(ccx: &CrateCtxt,
470477
m.ident.repr(ccx.tcx),
471478
m.id,
472479
fty.repr(ccx.tcx));
473-
tcx.tcache.insert(
474-
local_def(m.id),
480+
{
481+
let mut tcache = tcx.tcache.borrow_mut();
482+
tcache.get().insert(
483+
local_def(m.id),
484+
485+
// n.b.: the type of a method is parameterized by both
486+
// the parameters on the receiver and those on the method
487+
// itself
488+
ty_param_bounds_and_ty {
489+
generics: ty::Generics {
490+
type_param_defs: @vec::append(
491+
(*rcvr_ty_generics.type_param_defs).clone(),
492+
*m_ty_generics.type_param_defs),
493+
region_param_defs: rcvr_ty_generics.region_param_defs,
494+
},
495+
ty: fty
496+
});
497+
}
475498

476-
// n.b.: the type of a method is parameterized by both
477-
// the parameters on the receiver and those on the method itself
478-
ty_param_bounds_and_ty {
479-
generics: ty::Generics {
480-
type_param_defs: @vec::append(
481-
(*rcvr_ty_generics.type_param_defs).clone(),
482-
*m_ty_generics.type_param_defs),
483-
region_param_defs: rcvr_ty_generics.region_param_defs,
484-
},
485-
ty: fty
486-
});
487499
write_ty_to_tcx(tcx, m.id, fty);
488500

489501
let mut methods = tcx.methods.borrow_mut();
@@ -557,10 +569,14 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::item) {
557569
let i_ty_generics = ty_generics(ccx, generics, 0);
558570
let selfty = ccx.to_ty(&ExplicitRscope, selfty);
559571
write_ty_to_tcx(tcx, it.id, selfty);
560-
tcx.tcache.insert(local_def(it.id),
561-
ty_param_bounds_and_ty {
562-
generics: i_ty_generics,
563-
ty: selfty});
572+
573+
{
574+
let mut tcache = tcx.tcache.borrow_mut();
575+
tcache.get().insert(local_def(it.id),
576+
ty_param_bounds_and_ty {
577+
generics: i_ty_generics,
578+
ty: selfty});
579+
}
564580

565581
// If there is a trait reference, treat the methods as always public.
566582
// This is to work around some incorrect behavior in privacy checking:
@@ -618,7 +634,11 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::item) {
618634
// Write the class type
619635
let tpt = ty_of_item(ccx, it);
620636
write_ty_to_tcx(tcx, it.id, tpt.ty);
621-
tcx.tcache.insert(local_def(it.id), tpt);
637+
638+
{
639+
let mut tcache = tcx.tcache.borrow_mut();
640+
tcache.get().insert(local_def(it.id), tpt);
641+
}
622642

623643
convert_struct(ccx, struct_def, tpt, it.id);
624644
}
@@ -658,19 +678,29 @@ pub fn convert_struct(ccx: &CrateCtxt,
658678
if struct_def.fields.len() == 0 {
659679
// Enum-like.
660680
write_ty_to_tcx(tcx, ctor_id, selfty);
661-
tcx.tcache.insert(local_def(ctor_id), tpt);
681+
682+
{
683+
let mut tcache = tcx.tcache.borrow_mut();
684+
tcache.get().insert(local_def(ctor_id), tpt);
685+
}
662686
} else if struct_def.fields[0].node.kind == ast::unnamed_field {
663687
// Tuple-like.
664-
let inputs =
688+
let inputs = {
689+
let tcache = tcx.tcache.borrow();
665690
struct_def.fields.map(
666-
|field| ccx.tcx.tcache.get(
667-
&local_def(field.node.id)).ty);
691+
|field| tcache.get().get(
692+
&local_def(field.node.id)).ty)
693+
};
668694
let ctor_fn_ty = ty::mk_ctor_fn(tcx, ctor_id, inputs, selfty);
669695
write_ty_to_tcx(tcx, ctor_id, ctor_fn_ty);
670-
tcx.tcache.insert(local_def(ctor_id), ty_param_bounds_and_ty {
671-
generics: tpt.generics,
672-
ty: ctor_fn_ty
673-
});
696+
{
697+
let mut tcache = tcx.tcache.borrow_mut();
698+
tcache.get().insert(local_def(ctor_id),
699+
ty_param_bounds_and_ty {
700+
generics: tpt.generics,
701+
ty: ctor_fn_ty
702+
});
703+
}
674704
}
675705
}
676706
}
@@ -695,7 +725,9 @@ pub fn convert_foreign(ccx: &CrateCtxt, i: &ast::foreign_item) {
695725

696726
let tpt = ty_of_foreign_item(ccx, i, abis);
697727
write_ty_to_tcx(ccx.tcx, i.id, tpt.ty);
698-
ccx.tcx.tcache.insert(local_def(i.id), tpt);
728+
729+
let mut tcache = ccx.tcx.tcache.borrow_mut();
730+
tcache.get().insert(local_def(i.id), tpt);
699731
}
700732

701733
pub fn instantiate_trait_ref(ccx: &CrateCtxt,
@@ -781,15 +813,20 @@ pub fn ty_of_item(ccx: &CrateCtxt, it: &ast::item)
781813
-> ty::ty_param_bounds_and_ty {
782814
let def_id = local_def(it.id);
783815
let tcx = ccx.tcx;
784-
match tcx.tcache.find(&def_id) {
785-
Some(&tpt) => return tpt,
786-
_ => {}
816+
{
817+
let tcache = tcx.tcache.borrow();
818+
match tcache.get().find(&def_id) {
819+
Some(&tpt) => return tpt,
820+
_ => {}
821+
}
787822
}
788823
match it.node {
789824
ast::item_static(t, _, _) => {
790825
let typ = ccx.to_ty(&ExplicitRscope, t);
791826
let tpt = no_params(typ);
792-
tcx.tcache.insert(local_def(it.id), tpt);
827+
828+
let mut tcache = tcx.tcache.borrow_mut();
829+
tcache.get().insert(local_def(it.id), tpt);
793830
return tpt;
794831
}
795832
ast::item_fn(decl, purity, abi, ref generics, _) => {
@@ -810,13 +847,18 @@ pub fn ty_of_item(ccx: &CrateCtxt, it: &ast::item)
810847
tcx.sess.str_of(it.ident),
811848
it.id,
812849
ppaux::ty_to_str(tcx, tpt.ty));
813-
ccx.tcx.tcache.insert(local_def(it.id), tpt);
850+
851+
let mut tcache = ccx.tcx.tcache.borrow_mut();
852+
tcache.get().insert(local_def(it.id), tpt);
814853
return tpt;
815854
}
816855
ast::item_ty(t, ref generics) => {
817-
match tcx.tcache.find(&local_def(it.id)) {
818-
Some(&tpt) => return tpt,
819-
None => { }
856+
{
857+
let mut tcache = tcx.tcache.borrow_mut();
858+
match tcache.get().find(&local_def(it.id)) {
859+
Some(&tpt) => return tpt,
860+
None => { }
861+
}
820862
}
821863

822864
let tpt = {
@@ -827,7 +869,8 @@ pub fn ty_of_item(ccx: &CrateCtxt, it: &ast::item)
827869
}
828870
};
829871

830-
tcx.tcache.insert(local_def(it.id), tpt);
872+
let mut tcache = tcx.tcache.borrow_mut();
873+
tcache.get().insert(local_def(it.id), tpt);
831874
return tpt;
832875
}
833876
ast::item_enum(_, ref generics) => {
@@ -839,7 +882,9 @@ pub fn ty_of_item(ccx: &CrateCtxt, it: &ast::item)
839882
generics: ty_generics,
840883
ty: t
841884
};
842-
tcx.tcache.insert(local_def(it.id), tpt);
885+
886+
let mut tcache = tcx.tcache.borrow_mut();
887+
tcache.get().insert(local_def(it.id), tpt);
843888
return tpt;
844889
}
845890
ast::item_trait(..) => {
@@ -855,7 +900,9 @@ pub fn ty_of_item(ccx: &CrateCtxt, it: &ast::item)
855900
generics: ty_generics,
856901
ty: t
857902
};
858-
tcx.tcache.insert(local_def(it.id), tpt);
903+
904+
let mut tcache = tcx.tcache.borrow_mut();
905+
tcache.get().insert(local_def(it.id), tpt);
859906
return tpt;
860907
}
861908
ast::item_impl(..) | ast::item_mod(_) |
@@ -990,7 +1037,9 @@ pub fn ty_of_foreign_fn_decl(ccx: &CrateCtxt,
9901037
generics: ty_generics,
9911038
ty: t_fn
9921039
};
993-
ccx.tcx.tcache.insert(def_id, tpt);
1040+
1041+
let mut tcache = ccx.tcx.tcache.borrow_mut();
1042+
tcache.get().insert(def_id, tpt);
9941043
return tpt;
9951044
}
9961045

0 commit comments

Comments
 (0)