Skip to content

Commit 675edd0

Browse files
committed
Remove RefCell around module_trait_cache
1 parent b3c4e25 commit 675edd0

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

src/librustdoc/core.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ crate struct DocContext<'tcx> {
7676
///
7777
/// See `collect_intra_doc_links::traits_implemented_by` for more details.
7878
/// `map<module, set<trait>>`
79-
crate module_trait_cache: RefCell<FxHashMap<DefId, FxHashSet<DefId>>>,
79+
crate module_trait_cache: FxHashMap<DefId, FxHashSet<DefId>>,
8080
/// This same cache is used throughout rustdoc, including in [`crate::html::render`].
8181
crate cache: Cache,
8282
/// Used by [`clean::inline`] to tell if an item has already been inlined.
@@ -450,7 +450,7 @@ crate fn run_global_ctxt(
450450
.cloned()
451451
.filter(|trait_def_id| tcx.trait_is_auto(*trait_def_id))
452452
.collect(),
453-
module_trait_cache: RefCell::new(FxHashMap::default()),
453+
module_trait_cache: FxHashMap::default(),
454454
cache: Cache::new(access_levels, render_options.document_private),
455455
inlined: FxHashSet::default(),
456456
output_format,

src/librustdoc/passes/collect_intra_doc_links.rs

+24-23
Original file line numberDiff line numberDiff line change
@@ -484,21 +484,23 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
484484
/// Resolves a string as a path within a particular namespace. Returns an
485485
/// optional URL fragment in the case of variants and methods.
486486
fn resolve<'path>(
487-
&self,
487+
&mut self,
488488
path_str: &'path str,
489489
ns: Namespace,
490490
module_id: DefId,
491491
extra_fragment: &Option<String>,
492492
) -> Result<(Res, Option<String>), ErrorKind<'path>> {
493-
let cx = &self.cx;
493+
let tcx = self.cx.tcx;
494494

495495
if let Some(res) = self.resolve_path(path_str, ns, module_id) {
496496
match res {
497497
// FIXME(#76467): make this fallthrough to lookup the associated
498498
// item a separate function.
499499
Res::Def(DefKind::AssocFn | DefKind::AssocConst, _) => assert_eq!(ns, ValueNS),
500500
Res::Def(DefKind::AssocTy, _) => assert_eq!(ns, TypeNS),
501-
Res::Def(DefKind::Variant, _) => return handle_variant(cx, res, extra_fragment),
501+
Res::Def(DefKind::Variant, _) => {
502+
return handle_variant(self.cx, res, extra_fragment);
503+
}
502504
// Not a trait item; just return what we found.
503505
Res::Primitive(ty) => {
504506
if extra_fragment.is_some() {
@@ -565,13 +567,12 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
565567
) => {
566568
debug!("looking for associated item named {} for item {:?}", item_name, did);
567569
// Checks if item_name belongs to `impl SomeItem`
568-
let assoc_item = cx
569-
.tcx
570+
let assoc_item = tcx
570571
.inherent_impls(did)
571572
.iter()
572573
.flat_map(|&imp| {
573-
cx.tcx.associated_items(imp).find_by_name_and_namespace(
574-
cx.tcx,
574+
tcx.associated_items(imp).find_by_name_and_namespace(
575+
tcx,
575576
Ident::with_dummy_span(item_name),
576577
ns,
577578
imp,
@@ -587,7 +588,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
587588
// something like [`ambi_fn`](<SomeStruct as SomeTrait>::ambi_fn)
588589
.or_else(|| {
589590
let kind =
590-
resolve_associated_trait_item(did, module_id, item_name, ns, &self.cx);
591+
resolve_associated_trait_item(did, module_id, item_name, ns, self.cx);
591592
debug!("got associated item kind {:?}", kind);
592593
kind
593594
});
@@ -611,7 +612,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
611612
debug!("looking for variants or fields named {} for {:?}", item_name, did);
612613
// FIXME(jynelson): why is this different from
613614
// `variant_field`?
614-
match cx.tcx.type_of(did).kind() {
615+
match tcx.type_of(did).kind() {
615616
ty::Adt(def, _) => {
616617
let field = if def.is_enum() {
617618
def.all_fields().find(|item| item.ident.name == item_name)
@@ -652,10 +653,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
652653
None
653654
}
654655
}
655-
Res::Def(DefKind::Trait, did) => cx
656-
.tcx
656+
Res::Def(DefKind::Trait, did) => tcx
657657
.associated_items(did)
658-
.find_by_name_and_namespace(cx.tcx, Ident::with_dummy_span(item_name), ns, did)
658+
.find_by_name_and_namespace(tcx, Ident::with_dummy_span(item_name), ns, did)
659659
.map(|item| {
660660
let kind = match item.kind {
661661
ty::AssocKind::Const => "associatedconstant",
@@ -699,7 +699,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
699699
/// This returns the `Res` even if it was erroneous for some reason
700700
/// (such as having invalid URL fragments or being in the wrong namespace).
701701
fn check_full_res(
702-
&self,
702+
&mut self,
703703
ns: Namespace,
704704
path_str: &str,
705705
module_id: DefId,
@@ -733,7 +733,7 @@ fn resolve_associated_trait_item(
733733
module: DefId,
734734
item_name: Symbol,
735735
ns: Namespace,
736-
cx: &DocContext<'_>,
736+
cx: &mut DocContext<'_>,
737737
) -> Option<(ty::AssocKind, DefId)> {
738738
// FIXME: this should also consider blanket impls (`impl<T> X for T`). Unfortunately
739739
// `get_auto_trait_and_blanket_impls` is broken because the caching behavior is wrong. In the
@@ -758,10 +758,10 @@ fn resolve_associated_trait_item(
758758
///
759759
/// NOTE: this cannot be a query because more traits could be available when more crates are compiled!
760760
/// So it is not stable to serialize cross-crate.
761-
fn traits_implemented_by(cx: &DocContext<'_>, type_: DefId, module: DefId) -> FxHashSet<DefId> {
762-
let mut cache = cx.module_trait_cache.borrow_mut();
763-
let in_scope_traits = cache.entry(module).or_insert_with(|| {
764-
cx.enter_resolver(|resolver| {
761+
fn traits_implemented_by(cx: &mut DocContext<'_>, type_: DefId, module: DefId) -> FxHashSet<DefId> {
762+
let mut resolver = cx.resolver.borrow_mut();
763+
let in_scope_traits = cx.module_trait_cache.entry(module).or_insert_with(|| {
764+
resolver.access(|resolver| {
765765
let parent_scope = &ParentScope::module(resolver.get_module(module), resolver);
766766
resolver
767767
.traits_in_scope(None, parent_scope, SyntaxContext::root(), None)
@@ -771,13 +771,14 @@ fn traits_implemented_by(cx: &DocContext<'_>, type_: DefId, module: DefId) -> Fx
771771
})
772772
});
773773

774-
let ty = cx.tcx.type_of(type_);
774+
let tcx = cx.tcx;
775+
let ty = tcx.type_of(type_);
775776
let iter = in_scope_traits.iter().flat_map(|&trait_| {
776777
trace!("considering explicit impl for trait {:?}", trait_);
777778

778779
// Look at each trait implementation to see if it's an impl for `did`
779-
cx.tcx.find_map_relevant_impl(trait_, ty, |impl_| {
780-
let trait_ref = cx.tcx.impl_trait_ref(impl_).expect("this is not an inherent impl");
780+
tcx.find_map_relevant_impl(trait_, ty, |impl_| {
781+
let trait_ref = tcx.impl_trait_ref(impl_).expect("this is not an inherent impl");
781782
// Check if these are the same type.
782783
let impl_type = trait_ref.self_ty();
783784
trace!(
@@ -1308,7 +1309,7 @@ impl LinkCollector<'_, '_> {
13081309
/// After parsing the disambiguator, resolve the main part of the link.
13091310
// FIXME(jynelson): wow this is just so much
13101311
fn resolve_with_disambiguator(
1311-
&self,
1312+
&mut self,
13121313
key: &ResolutionInfo,
13131314
diag: DiagnosticInfo<'_>,
13141315
) -> Option<(Res, Option<String>)> {
@@ -1732,7 +1733,7 @@ fn report_diagnostic(
17321733
/// handled earlier. For example, if passed `Item::Crate(std)` and `path_str`
17331734
/// `std::io::Error::x`, this will resolve `std::io::Error`.
17341735
fn resolution_failure(
1735-
collector: &LinkCollector<'_, '_>,
1736+
collector: &mut LinkCollector<'_, '_>,
17361737
item: &Item,
17371738
path_str: &str,
17381739
disambiguator: Option<Disambiguator>,

src/librustdoc/passes/html_tags.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
178178
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
179179
if !dox.is_empty() {
180180
let report_diag = |msg: &str, range: &Range<usize>| {
181-
let sp = match super::source_span_for_markdown_range(tcx, &dox, range, &item.attrs) {
181+
let sp = match super::source_span_for_markdown_range(tcx, &dox, range, &item.attrs)
182+
{
182183
Some(sp) => sp,
183184
None => span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
184185
};

0 commit comments

Comments
 (0)