Skip to content

Commit 6880392

Browse files
committed
rustdoc: Use DefId(Map,Set) instead of FxHash(Map,Set)
Not all uses are converted, a few cases iterating through maps/sets and requiring nontrivial changes are kept.
1 parent 52372f9 commit 6880392

File tree

11 files changed

+55
-65
lines changed

11 files changed

+55
-65
lines changed

src/librustdoc/clean/inline.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_ast as ast;
99
use rustc_data_structures::fx::FxHashSet;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::{DefKind, Res};
12-
use rustc_hir::def_id::{DefId, LocalDefId};
12+
use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId};
1313
use rustc_hir::Mutability;
1414
use rustc_metadata::creader::{CStore, LoadedMacro};
1515
use rustc_middle::ty::{self, TyCtxt};
@@ -45,7 +45,7 @@ pub(crate) fn try_inline(
4545
res: Res,
4646
name: Symbol,
4747
attrs: Option<&[ast::Attribute]>,
48-
visited: &mut FxHashSet<DefId>,
48+
visited: &mut DefIdSet,
4949
) -> Option<Vec<clean::Item>> {
5050
let did = res.opt_def_id()?;
5151
if did.is_local() {
@@ -163,7 +163,7 @@ pub(crate) fn try_inline_glob(
163163
cx: &mut DocContext<'_>,
164164
res: Res,
165165
current_mod: LocalDefId,
166-
visited: &mut FxHashSet<DefId>,
166+
visited: &mut DefIdSet,
167167
inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
168168
) -> Option<Vec<clean::Item>> {
169169
let did = res.opt_def_id()?;
@@ -568,11 +568,7 @@ pub(crate) fn build_impl(
568568
));
569569
}
570570

571-
fn build_module(
572-
cx: &mut DocContext<'_>,
573-
did: DefId,
574-
visited: &mut FxHashSet<DefId>,
575-
) -> clean::Module {
571+
fn build_module(cx: &mut DocContext<'_>, did: DefId, visited: &mut DefIdSet) -> clean::Module {
576572
let items = build_module_items(cx, did, visited, &mut FxHashSet::default(), None);
577573

578574
let span = clean::Span::new(cx.tcx.def_span(did));
@@ -582,9 +578,9 @@ fn build_module(
582578
fn build_module_items(
583579
cx: &mut DocContext<'_>,
584580
did: DefId,
585-
visited: &mut FxHashSet<DefId>,
581+
visited: &mut DefIdSet,
586582
inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
587-
allowed_def_ids: Option<&FxHashSet<DefId>>,
583+
allowed_def_ids: Option<&DefIdSet>,
588584
) -> Vec<clean::Item> {
589585
let mut items = Vec::new();
590586

src/librustdoc/clean/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_attr as attr;
1515
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, IndexEntry};
1616
use rustc_hir as hir;
1717
use rustc_hir::def::{CtorKind, DefKind, Res};
18-
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
18+
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LOCAL_CRATE};
1919
use rustc_hir::PredicateOrigin;
2020
use rustc_hir_analysis::hir_ty_to_ty;
2121
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
@@ -1528,7 +1528,7 @@ fn maybe_expand_private_type_alias<'tcx>(
15281528
let hir::ItemKind::TyAlias(ty, generics) = alias else { return None };
15291529

15301530
let provided_params = &path.segments.last().expect("segments were empty");
1531-
let mut substs = FxHashMap::default();
1531+
let mut substs = DefIdMap::default();
15321532
let generic_args = provided_params.args();
15331533

15341534
let mut indices: hir::GenericParamCount = Default::default();
@@ -2321,7 +2321,7 @@ fn clean_extern_crate<'tcx>(
23212321

23222322
let krate_owner_def_id = krate.owner_id.to_def_id();
23232323
if please_inline {
2324-
let mut visited = FxHashSet::default();
2324+
let mut visited = DefIdSet::default();
23252325

23262326
let res = Res::Def(DefKind::Mod, crate_def_id);
23272327

@@ -2440,7 +2440,7 @@ fn clean_use_statement_inner<'tcx>(
24402440
let path = clean_path(path, cx);
24412441
let inner = if kind == hir::UseKind::Glob {
24422442
if !denied {
2443-
let mut visited = FxHashSet::default();
2443+
let mut visited = DefIdSet::default();
24442444
if let Some(items) =
24452445
inline::try_inline_glob(cx, path.res, current_mod, &mut visited, inlined_names)
24462446
{
@@ -2459,7 +2459,7 @@ fn clean_use_statement_inner<'tcx>(
24592459
}
24602460
}
24612461
if !denied {
2462-
let mut visited = FxHashSet::default();
2462+
let mut visited = DefIdSet::default();
24632463
let import_def_id = import.owner_id.to_def_id();
24642464

24652465
if let Some(mut items) = inline::try_inline(

src/librustdoc/core.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::emitter::{Emitter, EmitterWriter};
66
use rustc_errors::json::JsonEmitter;
77
use rustc_feature::UnstableFeatures;
88
use rustc_hir::def::{Namespace, Res};
9-
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
9+
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId};
1010
use rustc_hir::intravisit::{self, Visitor};
1111
use rustc_hir::{HirId, Path, TraitCandidate};
1212
use rustc_interface::interface;
@@ -60,11 +60,11 @@ pub(crate) struct DocContext<'tcx> {
6060
pub(crate) external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
6161
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
6262
/// the same time.
63-
pub(crate) active_extern_traits: FxHashSet<DefId>,
63+
pub(crate) active_extern_traits: DefIdSet,
6464
// The current set of parameter substitutions,
6565
// for expanding type aliases at the HIR level:
6666
/// Table `DefId` of type, lifetime, or const parameter -> substituted type, lifetime, or const
67-
pub(crate) substs: FxHashMap<DefId, clean::SubstParam>,
67+
pub(crate) substs: DefIdMap<clean::SubstParam>,
6868
/// Table synthetic type parameter for `impl Trait` in argument position -> bounds
6969
pub(crate) impl_trait_bounds: FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>,
7070
/// Auto-trait or blanket impls processed so far, as `(self_ty, trait_def_id)`.
@@ -108,11 +108,7 @@ impl<'tcx> DocContext<'tcx> {
108108

109109
/// Call the closure with the given parameters set as
110110
/// the substitutions for a type alias' RHS.
111-
pub(crate) fn enter_alias<F, R>(
112-
&mut self,
113-
substs: FxHashMap<DefId, clean::SubstParam>,
114-
f: F,
115-
) -> R
111+
pub(crate) fn enter_alias<F, R>(&mut self, substs: DefIdMap<clean::SubstParam>, f: F) -> R
116112
where
117113
F: FnOnce(&mut Self) -> R,
118114
{

src/librustdoc/formats/cache.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::mem;
22

33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4-
use rustc_hir::def_id::{CrateNum, DefId};
4+
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet};
55
use rustc_middle::ty::{self, TyCtxt};
66
use rustc_span::Symbol;
77

@@ -33,7 +33,7 @@ pub(crate) struct Cache {
3333
///
3434
/// The values of the map are a list of implementations and documentation
3535
/// found on that implementation.
36-
pub(crate) impls: FxHashMap<DefId, Vec<Impl>>,
36+
pub(crate) impls: DefIdMap<Vec<Impl>>,
3737

3838
/// Maintains a mapping of local crate `DefId`s to the fully qualified name
3939
/// and "short type description" of that node. This is used when generating
@@ -56,7 +56,7 @@ pub(crate) struct Cache {
5656
/// to the path used if the corresponding type is inlined. By
5757
/// doing this, we can detect duplicate impls on a trait page, and only display
5858
/// the impl for the inlined type.
59-
pub(crate) exact_paths: FxHashMap<DefId, Vec<Symbol>>,
59+
pub(crate) exact_paths: DefIdMap<Vec<Symbol>>,
6060

6161
/// This map contains information about all known traits of this crate.
6262
/// Implementations of a crate should inherit the documentation of the
@@ -127,7 +127,7 @@ pub(crate) struct Cache {
127127
struct CacheBuilder<'a, 'tcx> {
128128
cache: &'a mut Cache,
129129
/// This field is used to prevent duplicated impl blocks.
130-
impl_ids: FxHashMap<DefId, FxHashSet<DefId>>,
130+
impl_ids: DefIdMap<DefIdSet>,
131131
tcx: TyCtxt<'tcx>,
132132
}
133133

@@ -173,7 +173,7 @@ impl Cache {
173173

174174
let (krate, mut impl_ids) = {
175175
let mut cache_builder =
176-
CacheBuilder { tcx, cache: &mut cx.cache, impl_ids: FxHashMap::default() };
176+
CacheBuilder { tcx, cache: &mut cx.cache, impl_ids: Default::default() };
177177
krate = cache_builder.fold_crate(krate);
178178
(krate, cache_builder.impl_ids)
179179
};

src/librustdoc/html/render/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::rc::Rc;
66
use std::sync::mpsc::{channel, Receiver};
77

88
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
9-
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
9+
use rustc_hir::def_id::{DefIdMap, LOCAL_CRATE};
1010
use rustc_middle::ty::TyCtxt;
1111
use rustc_session::Session;
1212
use rustc_span::edition::Edition;
@@ -56,7 +56,7 @@ pub(crate) struct Context<'tcx> {
5656
pub(super) render_redirect_pages: bool,
5757
/// Tracks section IDs for `Deref` targets so they match in both the main
5858
/// body and the sidebar.
59-
pub(super) deref_id_map: FxHashMap<DefId, String>,
59+
pub(super) deref_id_map: DefIdMap<String>,
6060
/// The map used to ensure all generated 'id=' attributes are unique.
6161
pub(super) id_map: IdMap,
6262
/// Shared mutable state.
@@ -544,7 +544,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
544544
dst,
545545
render_redirect_pages: false,
546546
id_map,
547-
deref_id_map: FxHashMap::default(),
547+
deref_id_map: Default::default(),
548548
shared: Rc::new(scx),
549549
include_sources,
550550
types_with_notable_traits: FxHashSet::default(),
@@ -572,7 +572,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
572572
current: self.current.clone(),
573573
dst: self.dst.clone(),
574574
render_redirect_pages: self.render_redirect_pages,
575-
deref_id_map: FxHashMap::default(),
575+
deref_id_map: Default::default(),
576576
id_map: IdMap::new(),
577577
shared: Rc::clone(&self.shared),
578578
include_sources: self.include_sources,

src/librustdoc/html/render/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use rustc_ast_pretty::pprust;
5050
use rustc_attr::{ConstStability, Deprecation, StabilityLevel};
5151
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5252
use rustc_hir::def::CtorKind;
53-
use rustc_hir::def_id::DefId;
53+
use rustc_hir::def_id::{DefId, DefIdSet};
5454
use rustc_hir::Mutability;
5555
use rustc_middle::middle::stability;
5656
use rustc_middle::ty;
@@ -1115,7 +1115,7 @@ fn render_assoc_items(
11151115
it: DefId,
11161116
what: AssocItemRender<'_>,
11171117
) {
1118-
let mut derefs = FxHashSet::default();
1118+
let mut derefs = DefIdSet::default();
11191119
derefs.insert(it);
11201120
render_assoc_items_inner(w, cx, containing_item, it, what, &mut derefs)
11211121
}
@@ -1126,7 +1126,7 @@ fn render_assoc_items_inner(
11261126
containing_item: &clean::Item,
11271127
it: DefId,
11281128
what: AssocItemRender<'_>,
1129-
derefs: &mut FxHashSet<DefId>,
1129+
derefs: &mut DefIdSet,
11301130
) {
11311131
info!("Documenting associated items of {:?}", containing_item.name);
11321132
let shared = Rc::clone(&cx.shared);
@@ -1215,7 +1215,7 @@ fn render_deref_methods(
12151215
impl_: &Impl,
12161216
container_item: &clean::Item,
12171217
deref_mut: bool,
1218-
derefs: &mut FxHashSet<DefId>,
1218+
derefs: &mut DefIdSet,
12191219
) {
12201220
let cache = cx.cache();
12211221
let deref_type = impl_.inner_impl().trait_.as_ref().unwrap();
@@ -2175,7 +2175,7 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
21752175
if let Some(impl_) =
21762176
v.iter().find(|i| i.trait_did() == cx.tcx().lang_items().deref_trait())
21772177
{
2178-
let mut derefs = FxHashSet::default();
2178+
let mut derefs = DefIdSet::default();
21792179
derefs.insert(did);
21802180
sidebar_deref_methods(cx, out, impl_, v, &mut derefs, &mut used_links);
21812181
}
@@ -2195,7 +2195,7 @@ fn sidebar_deref_methods(
21952195
out: &mut Buffer,
21962196
impl_: &Impl,
21972197
v: &[Impl],
2198-
derefs: &mut FxHashSet<DefId>,
2198+
derefs: &mut DefIdSet,
21992199
used_links: &mut FxHashSet<String>,
22002200
) {
22012201
let c = cx.cache();

src/librustdoc/json/import_finder.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use rustc_data_structures::fx::FxHashSet;
2-
use rustc_hir::def_id::DefId;
1+
use rustc_hir::def_id::DefIdSet;
32

43
use crate::{
54
clean::{self, Import, ImportSource, Item},
@@ -14,14 +13,15 @@ use crate::{
1413
/// See [#100973](https://github.com/rust-lang/rust/issues/100973) and
1514
/// [#101103](https://github.com/rust-lang/rust/issues/101103) for times when
1615
/// this information is needed.
17-
pub(crate) fn get_imports(krate: clean::Crate) -> (clean::Crate, FxHashSet<DefId>) {
18-
let mut finder = ImportFinder { imported: FxHashSet::default() };
16+
pub(crate) fn get_imports(krate: clean::Crate) -> (clean::Crate, DefIdSet) {
17+
let mut finder = ImportFinder::default();
1918
let krate = finder.fold_crate(krate);
2019
(krate, finder.imported)
2120
}
2221

22+
#[derive(Default)]
2323
struct ImportFinder {
24-
imported: FxHashSet<DefId>,
24+
imported: DefIdSet,
2525
}
2626

2727
impl DocFolder for ImportFinder {

src/librustdoc/json/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::io::{BufWriter, Write};
1313
use std::path::PathBuf;
1414
use std::rc::Rc;
1515

16-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
17-
use rustc_hir::def_id::DefId;
16+
use rustc_data_structures::fx::FxHashMap;
17+
use rustc_hir::def_id::{DefId, DefIdSet};
1818
use rustc_middle::ty::TyCtxt;
1919
use rustc_session::Session;
2020
use rustc_span::def_id::LOCAL_CRATE;
@@ -40,7 +40,7 @@ pub(crate) struct JsonRenderer<'tcx> {
4040
/// The directory where the blob will be written to.
4141
out_path: PathBuf,
4242
cache: Rc<Cache>,
43-
imported_items: FxHashSet<DefId>,
43+
imported_items: DefIdSet,
4444
}
4545

4646
impl<'tcx> JsonRenderer<'tcx> {

src/librustdoc/passes/collect_trait_impls.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::core::DocContext;
77
use crate::formats::cache::Cache;
88
use crate::visit::DocVisitor;
99

10-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
11-
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
10+
use rustc_data_structures::fx::FxHashSet;
11+
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LOCAL_CRATE};
1212
use rustc_middle::ty::{self, DefIdTree};
1313
use rustc_span::symbol::sym;
1414

@@ -126,14 +126,14 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
126126
});
127127

128128
let mut cleaner = BadImplStripper { prims, items: crate_items, cache: &cx.cache };
129-
let mut type_did_to_deref_target: FxHashMap<DefId, &Type> = FxHashMap::default();
129+
let mut type_did_to_deref_target: DefIdMap<&Type> = DefIdMap::default();
130130

131131
// Follow all `Deref` targets of included items and recursively add them as valid
132132
fn add_deref_target(
133133
cx: &DocContext<'_>,
134-
map: &FxHashMap<DefId, &Type>,
134+
map: &DefIdMap<&Type>,
135135
cleaner: &mut BadImplStripper<'_>,
136-
targets: &mut FxHashSet<DefId>,
136+
targets: &mut DefIdSet,
137137
type_did: DefId,
138138
) {
139139
if let Some(target) = map.get(&type_did) {
@@ -177,7 +177,7 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
177177
// `Deref` target type and the impl for type positions, this map of types is keyed by
178178
// `DefId` and for convenience uses a special cleaner that accepts `DefId`s directly.
179179
if cleaner.keep_impl_with_def_id(for_did.into()) {
180-
let mut targets = FxHashSet::default();
180+
let mut targets = DefIdSet::default();
181181
targets.insert(for_did);
182182
add_deref_target(
183183
cx,

src/librustdoc/visit_ast.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
//! The Rust AST Visitor. Extracts useful information and massages it into a form
22
//! usable for `clean`.
33
4-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4+
use rustc_data_structures::fx::FxHashSet;
55
use rustc_hir as hir;
66
use rustc_hir::def::{DefKind, Res};
7-
use rustc_hir::def_id::DefId;
8-
use rustc_hir::Node;
9-
use rustc_hir::CRATE_HIR_ID;
7+
use rustc_hir::def_id::{DefId, DefIdMap};
8+
use rustc_hir::{HirIdSet, Node, CRATE_HIR_ID};
109
use rustc_middle::ty::TyCtxt;
1110
use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
1211
use rustc_span::symbol::{kw, sym, Symbol};
@@ -62,24 +61,24 @@ pub(crate) fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut node: hir::HirId) -> bool
6261

6362
pub(crate) struct RustdocVisitor<'a, 'tcx> {
6463
cx: &'a mut core::DocContext<'tcx>,
65-
view_item_stack: FxHashSet<hir::HirId>,
64+
view_item_stack: HirIdSet,
6665
inlining: bool,
6766
/// Are the current module and all of its parents public?
6867
inside_public_path: bool,
69-
exact_paths: FxHashMap<DefId, Vec<Symbol>>,
68+
exact_paths: DefIdMap<Vec<Symbol>>,
7069
}
7170

7271
impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7372
pub(crate) fn new(cx: &'a mut core::DocContext<'tcx>) -> RustdocVisitor<'a, 'tcx> {
7473
// If the root is re-exported, terminate all recursion.
75-
let mut stack = FxHashSet::default();
74+
let mut stack = HirIdSet::default();
7675
stack.insert(hir::CRATE_HIR_ID);
7776
RustdocVisitor {
7877
cx,
7978
view_item_stack: stack,
8079
inlining: false,
8180
inside_public_path: true,
82-
exact_paths: FxHashMap::default(),
81+
exact_paths: Default::default(),
8382
}
8483
}
8584

0 commit comments

Comments
 (0)