Skip to content

Commit b1745c3

Browse files
author
Lukas Markeffsky
committed
de-rc external traits
Don't keep the `external_traits` as shared mutable data between the `DocContext` and `clean::Crate`. Instead, move the data over when necessary. This allows us to get rid of a borrowck hack in the `DocVisitor`.
1 parent 0399709 commit b1745c3

File tree

8 files changed

+19
-21
lines changed

8 files changed

+19
-21
lines changed

src/librustdoc/clean/inline.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -837,8 +837,7 @@ pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
837837
}
838838

839839
{
840-
if cx.external_traits.borrow().contains_key(&did) || cx.active_extern_traits.contains(&did)
841-
{
840+
if cx.external_traits.contains_key(&did) || cx.active_extern_traits.contains(&did) {
842841
return;
843842
}
844843
}
@@ -850,6 +849,6 @@ pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
850849
debug!("record_extern_trait: {did:?}");
851850
let trait_ = build_external_trait(cx, did);
852851

853-
cx.external_traits.borrow_mut().insert(did, trait_);
852+
cx.external_traits.insert(did, trait_);
854853
cx.active_extern_traits.remove(&did);
855854
}

src/librustdoc/clean/types.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::borrow::Cow;
2-
use std::cell::RefCell;
32
use std::hash::Hash;
43
use std::path::PathBuf;
5-
use std::rc::Rc;
64
use std::sync::{Arc, OnceLock as OnceCell};
75
use std::{fmt, iter};
86

@@ -115,7 +113,7 @@ impl From<DefId> for ItemId {
115113
pub(crate) struct Crate {
116114
pub(crate) module: Item,
117115
/// Only here so that they can be filtered through the rustdoc passes.
118-
pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, Trait>>>,
116+
pub(crate) external_traits: Box<FxHashMap<DefId, Trait>>,
119117
}
120118

121119
impl Crate {

src/librustdoc/clean/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
7474
}));
7575
}
7676

77-
Crate { module, external_traits: cx.external_traits.clone() }
77+
Crate { module, external_traits: Box::new(mem::take(&mut cx.external_traits)) }
7878
}
7979

8080
pub(crate) fn clean_middle_generic_args<'tcx>(

src/librustdoc/core.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::cell::RefCell;
2-
use std::rc::Rc;
31
use std::sync::atomic::AtomicBool;
42
use std::sync::{Arc, LazyLock};
53
use std::{io, mem};
@@ -41,7 +39,7 @@ pub(crate) struct DocContext<'tcx> {
4139
/// Most of this logic is copied from rustc_lint::late.
4240
pub(crate) param_env: ParamEnv<'tcx>,
4341
/// Later on moved through `clean::Crate` into `cache`
44-
pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
42+
pub(crate) external_traits: FxHashMap<DefId, clean::Trait>,
4543
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
4644
/// the same time.
4745
pub(crate) active_extern_traits: DefIdSet,
@@ -359,7 +357,7 @@ pub(crate) fn run_global_ctxt(
359357
// Note that in case of `#![no_core]`, the trait is not available.
360358
if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() {
361359
let sized_trait = build_external_trait(&mut ctxt, sized_trait_did);
362-
ctxt.external_traits.borrow_mut().insert(sized_trait_did, sized_trait);
360+
ctxt.external_traits.insert(sized_trait_did, sized_trait);
363361
}
364362

365363
debug!("crate: {:?}", tcx.hir().krate());

src/librustdoc/fold.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::mem;
2+
13
use crate::clean::*;
24

35
pub(crate) fn strip_item(mut item: Item) -> Item {
@@ -116,10 +118,11 @@ pub(crate) trait DocFolder: Sized {
116118
fn fold_crate(&mut self, mut c: Crate) -> Crate {
117119
c.module = self.fold_item(c.module).unwrap();
118120

119-
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
120-
for (k, mut v) in external_traits {
121-
v.items = v.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
122-
c.external_traits.borrow_mut().insert(k, v);
121+
for trait_ in c.external_traits.values_mut() {
122+
trait_.items = mem::take(&mut trait_.items)
123+
.into_iter()
124+
.filter_map(|i| self.fold_item(i))
125+
.collect();
123126
}
124127

125128
c

src/librustdoc/formats/cache.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ impl Cache {
153153

154154
// Crawl the crate to build various caches used for the output
155155
debug!(?cx.cache.crate_version);
156-
cx.cache.traits = krate.external_traits.take();
156+
assert!(cx.external_traits.is_empty());
157+
cx.cache.traits = mem::take(&mut krate.external_traits);
157158

158159
// Cache where all our extern crates are located
159160
// FIXME: this part is specific to HTML so it'd be nice to remove it from the common code

src/librustdoc/passes/collect_trait_impls.rs

+2
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
219219
panic!("collect-trait-impls can't run");
220220
};
221221

222+
krate.external_traits.extend(cx.external_traits.drain());
223+
222224
krate
223225
}
224226

src/librustdoc/visit.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ pub(crate) trait DocVisitor: Sized {
6161
fn visit_crate(&mut self, c: &Crate) {
6262
self.visit_item(&c.module);
6363

64-
// FIXME: make this a simple by-ref for loop once external_traits is cleaned up
65-
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
66-
for (k, v) in external_traits {
67-
v.items.iter().for_each(|i| self.visit_item(i));
68-
c.external_traits.borrow_mut().insert(k, v);
64+
for trait_ in c.external_traits.values() {
65+
trait_.items.iter().for_each(|i| self.visit_item(i));
6966
}
7067
}
7168
}

0 commit comments

Comments
 (0)