Skip to content

Commit e4ac499

Browse files
committed
Remove many RefCells from DocContext
I left some of them so this change doesn't balloon in size and because removing the RefCell in `DocContext.resolver` would require compiler changes. Thanks to `@jyn514` for making this a lot easier with rust-lang#82020!
1 parent 15598a8 commit e4ac499

12 files changed

+56
-60
lines changed

src/librustdoc/clean/auto_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
4141
) -> Option<Item> {
4242
let tcx = self.cx.tcx;
4343
let trait_ref = ty::TraitRef { def_id: trait_def_id, substs: tcx.mk_substs_trait(ty, &[]) };
44-
if !self.cx.generated_synthetics.borrow_mut().insert((ty, trait_def_id)) {
44+
if !self.cx.generated_synthetics.insert((ty, trait_def_id)) {
4545
debug!("get_auto_trait_impl_for({:?}): already generated, aborting", trait_ref);
4646
return None;
4747
}

src/librustdoc/clean/blanket_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
2222
debug!("get_blanket_impls({:?})", ty);
2323
let mut impls = Vec::new();
2424
for &trait_def_id in self.cx.tcx.all_traits(LOCAL_CRATE).iter() {
25-
if !self.cx.renderinfo.borrow().access_levels.is_public(trait_def_id)
26-
|| self.cx.generated_synthetics.borrow_mut().get(&(ty, trait_def_id)).is_some()
25+
if !self.cx.renderinfo.access_levels.is_public(trait_def_id)
26+
|| self.cx.generated_synthetics.get(&(ty, trait_def_id)).is_some()
2727
{
2828
continue;
2929
}
@@ -94,7 +94,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
9494
return;
9595
}
9696

97-
self.cx.generated_synthetics.borrow_mut().insert((ty, trait_def_id));
97+
self.cx.generated_synthetics.insert((ty, trait_def_id));
9898
let provided_trait_methods = self
9999
.cx
100100
.tcx

src/librustdoc/clean/inline.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ crate fn try_inline(
122122
let target_attrs = load_attrs(cx, did);
123123
let attrs = box merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);
124124

125-
cx.renderinfo.borrow_mut().inlined.insert(did);
125+
cx.renderinfo.inlined.insert(did);
126126
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name), kind, cx);
127127
ret.push(clean::Item { attrs, ..what_rustc_thinks });
128128
Some(ret)
@@ -156,7 +156,7 @@ crate fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> Attrs<'hir> {
156156
///
157157
/// These names are used later on by HTML rendering to generate things like
158158
/// source links back to the original item.
159-
crate fn record_extern_fqn(cx: &DocContext<'_>, did: DefId, kind: clean::TypeKind) {
159+
crate fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: clean::TypeKind) {
160160
let crate_name = cx.tcx.crate_name(did.krate).to_string();
161161

162162
let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
@@ -181,9 +181,9 @@ crate fn record_extern_fqn(cx: &DocContext<'_>, did: DefId, kind: clean::TypeKin
181181
};
182182

183183
if did.is_local() {
184-
cx.renderinfo.borrow_mut().exact_paths.insert(did, fqn);
184+
cx.renderinfo.exact_paths.insert(did, fqn);
185185
} else {
186-
cx.renderinfo.borrow_mut().external_paths.insert(did, (fqn, kind));
186+
cx.renderinfo.external_paths.insert(did, (fqn, kind));
187187
}
188188
}
189189

@@ -317,7 +317,7 @@ crate fn build_impl(
317317
attrs: Option<Attrs<'_>>,
318318
ret: &mut Vec<clean::Item>,
319319
) {
320-
if !cx.renderinfo.borrow_mut().inlined.insert(did) {
320+
if !cx.renderinfo.inlined.insert(did) {
321321
return;
322322
}
323323

@@ -329,7 +329,7 @@ crate fn build_impl(
329329
if !did.is_local() {
330330
if let Some(traitref) = associated_trait {
331331
let did = traitref.def_id;
332-
if !cx.renderinfo.borrow().access_levels.is_public(did) {
332+
if !cx.renderinfo.access_levels.is_public(did) {
333333
return;
334334
}
335335

@@ -361,7 +361,7 @@ crate fn build_impl(
361361
// reachable in rustdoc generated documentation
362362
if !did.is_local() {
363363
if let Some(did) = for_.def_id() {
364-
if !cx.renderinfo.borrow().access_levels.is_public(did) {
364+
if !cx.renderinfo.access_levels.is_public(did) {
365365
return;
366366
}
367367

@@ -613,20 +613,19 @@ crate fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
613613
}
614614

615615
{
616-
if cx.external_traits.borrow().contains_key(&did)
617-
|| cx.active_extern_traits.borrow().contains(&did)
616+
if cx.external_traits.borrow().contains_key(&did) || cx.active_extern_traits.contains(&did)
618617
{
619618
return;
620619
}
621620
}
622621

623622
{
624-
cx.active_extern_traits.borrow_mut().insert(did);
623+
cx.active_extern_traits.insert(did);
625624
}
626625

627626
debug!("record_extern_trait: {:?}", did);
628627
let trait_ = build_external_trait(cx, did);
629628

630629
cx.external_traits.borrow_mut().insert(did, trait_);
631-
cx.active_extern_traits.borrow_mut().remove(&did);
630+
cx.active_extern_traits.remove(&did);
632631
}

src/librustdoc/clean/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl Clean<Lifetime> for hir::Lifetime {
357357
| rl::Region::LateBound(_, node_id, _)
358358
| rl::Region::Free(_, node_id),
359359
) => {
360-
if let Some(lt) = cx.lt_substs.borrow().get(&node_id).cloned() {
360+
if let Some(lt) = cx.lt_substs.get(&node_id).cloned() {
361361
return lt;
362362
}
363363
}
@@ -644,7 +644,7 @@ impl Clean<Generics> for hir::Generics<'_> {
644644
match param.kind {
645645
GenericParamDefKind::Lifetime => unreachable!(),
646646
GenericParamDefKind::Type { did, ref bounds, .. } => {
647-
cx.impl_trait_bounds.borrow_mut().insert(did.into(), bounds.clone());
647+
cx.impl_trait_bounds.insert(did.into(), bounds.clone());
648648
}
649649
GenericParamDefKind::Const { .. } => unreachable!(),
650650
}
@@ -803,7 +803,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
803803
unreachable!();
804804
}
805805

806-
cx.impl_trait_bounds.borrow_mut().insert(param, bounds);
806+
cx.impl_trait_bounds.insert(param, bounds);
807807
}
808808

809809
// Now that `cx.impl_trait_bounds` is populated, we can process
@@ -1291,10 +1291,10 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
12911291
match qpath {
12921292
hir::QPath::Resolved(None, ref path) => {
12931293
if let Res::Def(DefKind::TyParam, did) = path.res {
1294-
if let Some(new_ty) = cx.ty_substs.borrow().get(&did).cloned() {
1294+
if let Some(new_ty) = cx.ty_substs.get(&did).cloned() {
12951295
return new_ty;
12961296
}
1297-
if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&did.into()) {
1297+
if let Some(bounds) = cx.impl_trait_bounds.remove(&did.into()) {
12981298
return ImplTrait(bounds);
12991299
}
13001300
}
@@ -1304,7 +1304,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
13041304
// Substitute private type aliases
13051305
if let Some(def_id) = def_id.as_local() {
13061306
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
1307-
if !cx.renderinfo.borrow().access_levels.is_exported(def_id.to_def_id()) {
1307+
if !cx.renderinfo.access_levels.is_exported(def_id.to_def_id()) {
13081308
alias = Some(&cx.tcx.hir().expect_item(hir_id).kind);
13091309
}
13101310
}
@@ -1651,7 +1651,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
16511651
ty::Projection(ref data) => data.clean(cx),
16521652

16531653
ty::Param(ref p) => {
1654-
if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&p.index.into()) {
1654+
if let Some(bounds) = cx.impl_trait_bounds.remove(&p.index.into()) {
16551655
ImplTrait(bounds)
16561656
} else {
16571657
Generic(p.name)

src/librustdoc/clean/utils.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
2323
let krate = cx.tcx.hir().krate();
2424
let module = crate::visit_ast::RustdocVisitor::new(&mut cx).visit(krate);
2525

26-
let mut r = cx.renderinfo.get_mut();
27-
r.deref_trait_did = cx.tcx.lang_items().deref_trait();
28-
r.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
29-
r.owned_box_did = cx.tcx.lang_items().owned_box();
26+
cx.renderinfo.deref_trait_did = cx.tcx.lang_items().deref_trait();
27+
cx.renderinfo.deref_mut_trait_did = cx.tcx.lang_items().deref_mut_trait();
28+
cx.renderinfo.owned_box_did = cx.tcx.lang_items().owned_box();
3029

3130
let mut externs = Vec::new();
3231
for &cnum in cx.tcx.crates().iter() {
@@ -494,10 +493,10 @@ crate fn enter_impl_trait<F, R>(cx: &mut DocContext<'_>, f: F) -> R
494493
where
495494
F: FnOnce(&mut DocContext<'_>) -> R,
496495
{
497-
let old_bounds = mem::take(&mut *cx.impl_trait_bounds.get_mut());
496+
let old_bounds = mem::take(&mut cx.impl_trait_bounds);
498497
let r = f(cx);
499-
assert!(cx.impl_trait_bounds.borrow().is_empty());
500-
*cx.impl_trait_bounds.get_mut() = old_bounds;
498+
assert!(cx.impl_trait_bounds.is_empty());
499+
cx.impl_trait_bounds = old_bounds;
501500
r
502501
}
503502

src/librustdoc/core.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,37 @@ crate type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
4242

4343
crate struct DocContext<'tcx> {
4444
crate tcx: TyCtxt<'tcx>,
45+
/// Name resolver. Used for intra-doc links.
46+
///
47+
/// The `Rc<RefCell<...>>` wrapping is needed because that is what's returned by
48+
/// [`Queries::expansion()`].
49+
// FIXME: see if we can get rid of this RefCell somehow
4550
crate resolver: Rc<RefCell<interface::BoxedResolver>>,
4651
/// Used for normalization.
4752
///
4853
/// Most of this logic is copied from rustc_lint::late.
4954
crate param_env: ParamEnv<'tcx>,
5055
/// Later on moved into `cache`
51-
crate renderinfo: RefCell<RenderInfo>,
56+
crate renderinfo: RenderInfo,
5257
/// Later on moved through `clean::Crate` into `cache`
5358
crate external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
5459
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
5560
/// the same time.
56-
crate active_extern_traits: RefCell<FxHashSet<DefId>>,
61+
crate active_extern_traits: FxHashSet<DefId>,
5762
// The current set of type and lifetime substitutions,
5863
// for expanding type aliases at the HIR level:
5964
/// Table `DefId` of type parameter -> substituted type
60-
crate ty_substs: RefCell<FxHashMap<DefId, clean::Type>>,
65+
crate ty_substs: FxHashMap<DefId, clean::Type>,
6166
/// Table `DefId` of lifetime parameter -> substituted lifetime
62-
crate lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
67+
crate lt_substs: FxHashMap<DefId, clean::Lifetime>,
6368
/// Table `DefId` of const parameter -> substituted const
64-
crate ct_substs: RefCell<FxHashMap<DefId, clean::Constant>>,
69+
crate ct_substs: FxHashMap<DefId, clean::Constant>,
6570
/// Table synthetic type parameter for `impl Trait` in argument position -> bounds
66-
crate impl_trait_bounds: RefCell<FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>>,
71+
crate impl_trait_bounds: FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>,
6772
crate fake_def_ids: FxHashMap<CrateNum, DefIndex>,
6873
/// Auto-trait or blanket impls processed so far, as `(self_ty, trait_def_id)`.
6974
// FIXME(eddyb) make this a `ty::TraitRef<'tcx>` set.
70-
crate generated_synthetics: RefCell<FxHashSet<(Ty<'tcx>, DefId)>>,
75+
crate generated_synthetics: FxHashSet<(Ty<'tcx>, DefId)>,
7176
crate auto_traits: Vec<DefId>,
7277
/// The options given to rustdoc that could be relevant to a pass.
7378
crate render_options: RenderOptions,
@@ -112,14 +117,14 @@ impl<'tcx> DocContext<'tcx> {
112117
F: FnOnce(&mut Self) -> R,
113118
{
114119
let (old_tys, old_lts, old_cts) = (
115-
mem::replace(&mut *self.ty_substs.get_mut(), ty_substs),
116-
mem::replace(&mut *self.lt_substs.get_mut(), lt_substs),
117-
mem::replace(&mut *self.ct_substs.get_mut(), ct_substs),
120+
mem::replace(&mut self.ty_substs, ty_substs),
121+
mem::replace(&mut self.lt_substs, lt_substs),
122+
mem::replace(&mut self.ct_substs, ct_substs),
118123
);
119124
let r = f(self);
120-
*self.ty_substs.get_mut() = old_tys;
121-
*self.lt_substs.get_mut() = old_lts;
122-
*self.ct_substs.get_mut() = old_cts;
125+
self.ty_substs = old_tys;
126+
self.lt_substs = old_lts;
127+
self.ct_substs = old_cts;
123128
r
124129
}
125130

@@ -509,7 +514,7 @@ crate fn run_global_ctxt(
509514
param_env: ParamEnv::empty(),
510515
external_traits: Default::default(),
511516
active_extern_traits: Default::default(),
512-
renderinfo: RefCell::new(renderinfo),
517+
renderinfo,
513518
ty_substs: Default::default(),
514519
lt_substs: Default::default(),
515520
ct_substs: Default::default(),
@@ -642,7 +647,7 @@ crate fn run_global_ctxt(
642647
// The main crate doc comments are always collapsed.
643648
krate.collapsed = true;
644649

645-
(krate, ctxt.renderinfo.into_inner(), ctxt.render_options)
650+
(krate, ctxt.renderinfo, ctxt.render_options)
646651
}
647652

648653
/// Due to <https://github.com/rust-lang/rust/pull/73566>,

src/librustdoc/passes/calculate_doc_coverage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<'a, 'b> CoverageCalculator<'a, 'b> {
127127
}
128128

129129
fn print_results(&self) {
130-
let output_format = self.ctx.renderinfo.borrow().output_format;
130+
let output_format = self.ctx.renderinfo.output_format;
131131
if output_format.is_json() {
132132
println!("{}", self.to_json());
133133
return;

src/librustdoc/passes/collect_trait_impls.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
4848
if !cx.tcx.get_attrs(def_id).lists(sym::doc).has_word(sym::hidden) {
4949
let self_ty = cx.tcx.type_of(def_id);
5050
let impls = get_auto_trait_and_blanket_impls(cx, self_ty, def_id);
51-
let mut renderinfo = cx.renderinfo.borrow_mut();
5251

53-
new_items.extend(impls.filter(|i| renderinfo.inlined.insert(i.def_id)));
52+
new_items.extend(impls.filter(|i| cx.renderinfo.inlined.insert(i.def_id)));
5453
}
55-
})
54+
});
5655
}
5756
}
5857

src/librustdoc/passes/doc_test_lints.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
9797
|lint| lint.build("missing code example in this documentation").emit(),
9898
);
9999
}
100-
} else if tests.found_tests > 0 && !cx.renderinfo.borrow().access_levels.is_public(item.def_id)
101-
{
100+
} else if tests.found_tests > 0 && !cx.renderinfo.access_levels.is_public(item.def_id) {
102101
cx.tcx.struct_span_lint_hir(
103102
lint::builtin::PRIVATE_DOC_TESTS,
104103
hir_id,

src/librustdoc/passes/strip_private.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ crate const STRIP_PRIVATE: Pass = Pass {
1717
crate fn strip_private(mut krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
1818
// This stripper collects all *retained* nodes.
1919
let mut retained = DefIdSet::default();
20-
let access_levels = cx.renderinfo.borrow().access_levels.clone();
20+
let access_levels = cx.renderinfo.access_levels.clone();
2121

2222
// strip all private items
2323
{

src/librustdoc/visit_ast.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
113113
assert_eq!(cur_mod_def_id, macro_parent_def_id);
114114
cur_mod.macros.push((def, None));
115115
}
116-
self.cx.renderinfo.get_mut().exact_paths = self.exact_paths;
116+
self.cx.renderinfo.exact_paths = self.exact_paths;
117117
top_level_module
118118
}
119119

@@ -199,12 +199,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
199199
} else {
200200
// All items need to be handled here in case someone wishes to link
201201
// to them with intra-doc links
202-
self.cx
203-
.renderinfo
204-
.get_mut()
205-
.access_levels
206-
.map
207-
.insert(did, AccessLevel::Public);
202+
self.cx.renderinfo.access_levels.map.insert(did, AccessLevel::Public);
208203
}
209204
}
210205
}
@@ -216,7 +211,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
216211
None => return false,
217212
};
218213

219-
let is_private = !self.cx.renderinfo.borrow().access_levels.is_public(res_did);
214+
let is_private = !self.cx.renderinfo.access_levels.is_public(res_did);
220215
let is_hidden = inherits_doc_hidden(self.cx, res_hir_id);
221216

222217
// Only inline if requested or if the item would otherwise be stripped.

src/librustdoc/visit_lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl<'a, 'tcx> LibEmbargoVisitor<'a, 'tcx> {
2525
crate fn new(cx: &'a mut crate::core::DocContext<'tcx>) -> LibEmbargoVisitor<'a, 'tcx> {
2626
LibEmbargoVisitor {
2727
tcx: cx.tcx,
28-
access_levels: &mut cx.renderinfo.get_mut().access_levels,
28+
access_levels: &mut cx.renderinfo.access_levels,
2929
prev_level: Some(AccessLevel::Public),
3030
visited_mods: FxHashSet::default(),
3131
}

0 commit comments

Comments
 (0)