Skip to content

Commit 2fc8571

Browse files
author
Ariel Ben-Yehuda
committed
Fix deadlocks with RUST_LOG=rustc::middle::ty
These are RefCell deadlocks that cause the rustc task to die with the stderr lock held, causing a real deadlock. Fixes #26717.
1 parent 40db46c commit 2fc8571

File tree

6 files changed

+68
-56
lines changed

6 files changed

+68
-56
lines changed

src/librustc/middle/astencode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,7 @@ fn decode_side_tables(dcx: &DecodeContext,
16561656
c::tag_table_tcache => {
16571657
let type_scheme = val_dsr.read_type_scheme(dcx);
16581658
let lid = ast::DefId { krate: ast::LOCAL_CRATE, node: id };
1659-
dcx.tcx.tcache.borrow_mut().insert(lid, type_scheme);
1659+
dcx.tcx.register_item_type(lid, type_scheme);
16601660
}
16611661
c::tag_table_param_defs => {
16621662
let bounds = val_dsr.read_type_param_def(dcx);

src/librustc/middle/ty.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3198,10 +3198,10 @@ pub trait ClosureTyper<'tcx> {
31983198

31993199
impl<'tcx> CommonTypes<'tcx> {
32003200
fn new(arena: &'tcx TypedArena<TyS<'tcx>>,
3201-
interner: &mut FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>)
3201+
interner: &RefCell<FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>>)
32023202
-> CommonTypes<'tcx>
32033203
{
3204-
let mut mk = |sty| ctxt::intern_ty(arena, interner, sty);
3204+
let mk = |sty| ctxt::intern_ty(arena, interner, sty);
32053205
CommonTypes {
32063206
bool: mk(TyBool),
32073207
char: mk(TyChar),
@@ -3430,12 +3430,12 @@ impl<'tcx> ctxt<'tcx> {
34303430
f: F) -> (Session, R)
34313431
where F: FnOnce(&ctxt<'tcx>) -> R
34323432
{
3433-
let mut interner = FnvHashMap();
3434-
let common_types = CommonTypes::new(&arenas.type_, &mut interner);
3433+
let interner = RefCell::new(FnvHashMap());
3434+
let common_types = CommonTypes::new(&arenas.type_, &interner);
34353435

34363436
tls::enter(ctxt {
34373437
arenas: arenas,
3438-
interner: RefCell::new(interner),
3438+
interner: interner,
34393439
substs_interner: RefCell::new(FnvHashMap()),
34403440
bare_fn_interner: RefCell::new(FnvHashMap()),
34413441
region_interner: RefCell::new(FnvHashMap()),
@@ -3563,35 +3563,37 @@ impl<'tcx> ctxt<'tcx> {
35633563
}
35643564

35653565
fn intern_ty(type_arena: &'tcx TypedArena<TyS<'tcx>>,
3566-
interner: &mut FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>,
3566+
interner: &RefCell<FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>>,
35673567
st: TypeVariants<'tcx>)
35683568
-> Ty<'tcx> {
3569-
match interner.get(&st) {
3570-
Some(ty) => return *ty,
3571-
_ => ()
3572-
}
3569+
let ty: Ty /* don't be &mut TyS */ = {
3570+
let mut interner = interner.borrow_mut();
3571+
match interner.get(&st) {
3572+
Some(ty) => return *ty,
3573+
_ => ()
3574+
}
35733575

3574-
let flags = FlagComputation::for_sty(&st);
3576+
let flags = FlagComputation::for_sty(&st);
3577+
3578+
let ty = match () {
3579+
() => type_arena.alloc(TyS { sty: st,
3580+
flags: Cell::new(flags.flags),
3581+
region_depth: flags.depth, }),
3582+
};
35753583

3576-
let ty = match () {
3577-
() => type_arena.alloc(TyS { sty: st,
3578-
flags: Cell::new(flags.flags),
3579-
region_depth: flags.depth, }),
3584+
interner.insert(InternedTy { ty: ty }, ty);
3585+
ty
35803586
};
35813587

35823588
debug!("Interned type: {:?} Pointer: {:?}",
35833589
ty, ty as *const TyS);
3584-
3585-
interner.insert(InternedTy { ty: ty }, ty);
3586-
35873590
ty
35883591
}
35893592

35903593
// Interns a type/name combination, stores the resulting box in cx.interner,
35913594
// and returns the box as cast to an unsafe ptr (see comments for Ty above).
35923595
pub fn mk_ty(&self, st: TypeVariants<'tcx>) -> Ty<'tcx> {
3593-
let mut interner = self.interner.borrow_mut();
3594-
ctxt::intern_ty(&self.arenas.type_, &mut *interner, st)
3596+
ctxt::intern_ty(&self.arenas.type_, &self.interner, st)
35953597
}
35963598

35973599
pub fn mk_mach_int(&self, tm: ast::IntTy) -> Ty<'tcx> {
@@ -5930,6 +5932,10 @@ impl<'tcx> ctxt<'tcx> {
59305932
.clone()
59315933
}
59325934

5935+
// Register a given item type
5936+
pub fn register_item_type(&self, did: ast::DefId, ty: TypeScheme<'tcx>) {
5937+
self.tcache.borrow_mut().insert(did, ty);
5938+
}
59335939

59345940
// If the given item is in an external crate, looks up its type and adds it to
59355941
// the type cache. Returns the type parameters and type.
@@ -6006,8 +6012,8 @@ impl<'tcx> ctxt<'tcx> {
60066012
if id.krate == ast::LOCAL_CRATE {
60076013
self.node_id_to_type(id.node)
60086014
} else {
6009-
let mut tcache = self.tcache.borrow_mut();
6010-
tcache.entry(id).or_insert_with(|| csearch::get_field_type(self, struct_id, id)).ty
6015+
memoized(&self.tcache, id,
6016+
|id| csearch::get_field_type(self, struct_id, id)).ty
60116017
}
60126018
}
60136019

src/librustc/util/ppaux.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,15 @@ impl<'tcx> fmt::Display for ty::TypeVariants<'tcx> {
680680
TyError => write!(f, "[type error]"),
681681
TyParam(ref param_ty) => write!(f, "{}", param_ty),
682682
TyEnum(did, substs) | TyStruct(did, substs) => {
683-
parameterized(f, substs, did, &[],
684-
|tcx| tcx.lookup_item_type(did).generics)
683+
ty::tls::with(|tcx| {
684+
if did.krate == ast::LOCAL_CRATE &&
685+
!tcx.tcache.borrow().contains_key(&did) {
686+
write!(f, "{}<..>", tcx.item_path_str(did))
687+
} else {
688+
parameterized(f, substs, did, &[],
689+
|tcx| tcx.lookup_item_type(did).generics)
690+
}
691+
})
685692
}
686693
TyTrait(ref data) => write!(f, "{}", data),
687694
ty::TyProjection(ref data) => write!(f, "{}", data),

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4007,7 +4007,7 @@ fn check_const<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
40074007
let inh = static_inherited_fields(ccx, &tables);
40084008
let rty = ccx.tcx.node_id_to_type(id);
40094009
let fcx = blank_fn_ctxt(ccx, &inh, ty::FnConverging(rty), e.id);
4010-
let declty = fcx.ccx.tcx.tcache.borrow().get(&local_def(id)).unwrap().ty;
4010+
let declty = fcx.ccx.tcx.lookup_item_type(local_def(id)).ty;
40114011
check_const_with_ty(&fcx, sp, e, declty);
40124012
}
40134013

src/librustc_typeck/coherence/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
214214
};
215215
debug!("new_polytype={:?}", new_polytype);
216216

217-
tcx.tcache.borrow_mut().insert(new_did, new_polytype);
217+
tcx.register_item_type(new_did, new_polytype);
218218
tcx.predicates.borrow_mut().insert(new_did, new_method_ty.predicates.clone());
219219
tcx.impl_or_trait_items
220220
.borrow_mut()

src/librustc_typeck/collect.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ fn get_enum_variant_types<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
596596
ty: result_ty
597597
};
598598

599-
tcx.tcache.borrow_mut().insert(variant_def_id, variant_scheme.clone());
599+
tcx.register_item_type(variant_def_id, variant_scheme.clone());
600600
tcx.predicates.borrow_mut().insert(variant_def_id, enum_predicates.clone());
601601
write_ty_to_tcx(tcx, variant.node.id, result_ty);
602602
}
@@ -635,7 +635,7 @@ fn convert_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
635635
ccx.tcx.mk_bare_fn(ty_method.fty.clone()));
636636
debug!("method {} (id {}) has type {:?}",
637637
ident, id, fty);
638-
ccx.tcx.tcache.borrow_mut().insert(def_id,TypeScheme {
638+
ccx.tcx.register_item_type(def_id, TypeScheme {
639639
generics: ty_method.generics.clone(),
640640
ty: fty
641641
});
@@ -661,11 +661,11 @@ fn convert_field<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
661661
write_ty_to_tcx(ccx.tcx, v.node.id, tt);
662662

663663
/* add the field to the tcache */
664-
ccx.tcx.tcache.borrow_mut().insert(local_def(v.node.id),
665-
ty::TypeScheme {
666-
generics: struct_generics.clone(),
667-
ty: tt
668-
});
664+
ccx.tcx.register_item_type(local_def(v.node.id),
665+
ty::TypeScheme {
666+
generics: struct_generics.clone(),
667+
ty: tt
668+
});
669669
ccx.tcx.predicates.borrow_mut().insert(local_def(v.node.id),
670670
struct_predicates.clone());
671671

@@ -841,9 +841,9 @@ fn convert_item(ccx: &CrateCtxt, it: &ast::Item) {
841841
let selfty = ccx.icx(&ty_predicates).to_ty(&ExplicitRscope, &**selfty);
842842
write_ty_to_tcx(tcx, it.id, selfty);
843843

844-
tcx.tcache.borrow_mut().insert(local_def(it.id),
845-
TypeScheme { generics: ty_generics.clone(),
846-
ty: selfty });
844+
tcx.register_item_type(local_def(it.id),
845+
TypeScheme { generics: ty_generics.clone(),
846+
ty: selfty });
847847
tcx.predicates.borrow_mut().insert(local_def(it.id),
848848
ty_predicates.clone());
849849

@@ -863,11 +863,11 @@ fn convert_item(ccx: &CrateCtxt, it: &ast::Item) {
863863
if let ast::ConstImplItem(ref ty, ref expr) = impl_item.node {
864864
let ty = ccx.icx(&ty_predicates)
865865
.to_ty(&ExplicitRscope, &*ty);
866-
tcx.tcache.borrow_mut().insert(local_def(impl_item.id),
867-
TypeScheme {
868-
generics: ty_generics.clone(),
869-
ty: ty,
870-
});
866+
tcx.register_item_type(local_def(impl_item.id),
867+
TypeScheme {
868+
generics: ty_generics.clone(),
869+
ty: ty,
870+
});
871871
convert_associated_const(ccx, ImplContainer(local_def(it.id)),
872872
impl_item.ident, impl_item.id,
873873
impl_item.vis.inherit_from(parent_visibility),
@@ -954,11 +954,11 @@ fn convert_item(ccx: &CrateCtxt, it: &ast::Item) {
954954
ast::ConstTraitItem(ref ty, ref default) => {
955955
let ty = ccx.icx(&trait_predicates)
956956
.to_ty(&ExplicitRscope, ty);
957-
tcx.tcache.borrow_mut().insert(local_def(trait_item.id),
958-
TypeScheme {
959-
generics: trait_def.generics.clone(),
960-
ty: ty,
961-
});
957+
tcx.register_item_type(local_def(trait_item.id),
958+
TypeScheme {
959+
generics: trait_def.generics.clone(),
960+
ty: ty,
961+
});
962962
convert_associated_const(ccx, TraitContainer(local_def(it.id)),
963963
trait_item.ident, trait_item.id,
964964
ast::Public, ty, default.as_ref().map(|d| &**d));
@@ -1099,26 +1099,25 @@ fn convert_struct<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
10991099
// Enum-like.
11001100
write_ty_to_tcx(tcx, ctor_id, selfty);
11011101

1102-
tcx.tcache.borrow_mut().insert(local_def(ctor_id), scheme);
1102+
tcx.register_item_type(local_def(ctor_id), scheme);
11031103
tcx.predicates.borrow_mut().insert(local_def(ctor_id), predicates);
11041104
} else if struct_def.fields[0].node.kind.is_unnamed() {
11051105
// Tuple-like.
11061106
let inputs: Vec<_> =
11071107
struct_def.fields
11081108
.iter()
1109-
.map(|field| tcx.tcache.borrow().get(&local_def(field.node.id))
1110-
.unwrap()
1111-
.ty)
1109+
.map(|field| tcx.lookup_item_type(
1110+
local_def(field.node.id)).ty)
11121111
.collect();
11131112
let ctor_fn_ty = tcx.mk_ctor_fn(local_def(ctor_id),
11141113
&inputs[..],
11151114
selfty);
11161115
write_ty_to_tcx(tcx, ctor_id, ctor_fn_ty);
1117-
tcx.tcache.borrow_mut().insert(local_def(ctor_id),
1118-
TypeScheme {
1119-
generics: scheme.generics,
1120-
ty: ctor_fn_ty
1121-
});
1116+
tcx.register_item_type(local_def(ctor_id),
1117+
TypeScheme {
1118+
generics: scheme.generics,
1119+
ty: ctor_fn_ty
1120+
});
11221121
tcx.predicates.borrow_mut().insert(local_def(ctor_id), predicates);
11231122
}
11241123
}

0 commit comments

Comments
 (0)