Skip to content

Commit c8a4c2e

Browse files
Rollup merge of rust-lang#156020 - GuillaumeGomez:cleanup-code, r=urgau
Improve source code for `librustdoc/visit_ast.rs` While working on this part of rustdoc, got annoyed at the tuples and decided to transform them into types. Code is now much easier to follow. :3 r? @Urgau
2 parents f2d111e + f19e850 commit c8a4c2e

2 files changed

Lines changed: 73 additions & 54 deletions

File tree

src/librustdoc/clean/mod.rs

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ pub(crate) use self::types::*;
6262
pub(crate) use self::utils::{krate, register_res, synthesize_auto_trait_and_blanket_impls};
6363
use crate::core::DocContext;
6464
use crate::formats::item_type::ItemType;
65-
use crate::visit_ast::Module as DocModule;
65+
use crate::visit_ast;
6666

67-
pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
67+
pub(crate) fn clean_doc_module<'tcx>(
68+
doc: &visit_ast::Module<'tcx>,
69+
cx: &mut DocContext<'tcx>,
70+
) -> Item {
6871
let mut items: Vec<Item> = vec![];
6972
let mut inserted = FxHashSet::default();
70-
items.extend(doc.foreigns.iter().map(|(item, renamed, import_id)| {
73+
items.extend(doc.foreigns.iter().map(|visit_ast::Foreign { item, renamed, import_id }| {
7174
let item = clean_maybe_renamed_foreign_item(cx, item, *renamed, *import_id);
7275
if let Some(name) = item.name
7376
&& (cx.document_hidden() || !item.is_doc_hidden())
@@ -95,52 +98,56 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
9598
// This covers the case where somebody does an import which should pull in an item,
9699
// but there's already an item with the same namespace and same name. Rust gives
97100
// priority to the not-imported one, so we should, too.
98-
items.extend(doc.items.values().flat_map(|entry| {
99-
// First, lower everything other than glob imports.
100-
let item = entry.item;
101-
if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
102-
return Vec::new();
103-
}
104-
let v = clean_maybe_renamed_item(cx, item, entry.renamed, &entry.import_ids);
105-
for item in &v {
106-
if let Some(name) = item.name
107-
&& (cx.document_hidden() || !item.is_doc_hidden())
108-
{
109-
inserted.insert((item.type_(), name));
101+
items.extend(doc.items.values().flat_map(
102+
|visit_ast::ItemEntry { item, renamed, import_ids }| {
103+
// First, lower everything other than glob imports.
104+
if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
105+
return Vec::new();
110106
}
111-
}
112-
v
113-
}));
114-
items.extend(doc.inlined_foreigns.iter().flat_map(|((_, renamed), (res, local_import_id))| {
115-
let Some(def_id) = res.opt_def_id() else { return Vec::new() };
116-
let name = renamed.unwrap_or_else(|| cx.tcx.item_name(def_id));
117-
let import = cx.tcx.hir_expect_item(*local_import_id);
118-
match import.kind {
119-
hir::ItemKind::Use(path, kind) => {
120-
let hir::UsePath { segments, span, .. } = *path;
121-
let path = hir::Path { segments, res: *res, span };
122-
clean_use_statement_inner(
123-
import,
124-
Some(name),
125-
&path,
126-
kind,
127-
cx,
128-
&mut Default::default(),
129-
)
107+
let v = clean_maybe_renamed_item(cx, item, *renamed, import_ids);
108+
for item in &v {
109+
if let Some(name) = item.name
110+
&& (cx.document_hidden() || !item.is_doc_hidden())
111+
{
112+
inserted.insert((item.type_(), name));
113+
}
130114
}
131-
_ => unreachable!(),
132-
}
133-
}));
134-
items.extend(doc.items.values().flat_map(|entry| {
135-
// Now we actually lower the imports, skipping everything else.
136-
let item = entry.item;
137-
if let hir::ItemKind::Use(path, hir::UseKind::Glob) = item.kind {
138-
clean_use_statement(item, entry.renamed, path, hir::UseKind::Glob, cx, &mut inserted)
139-
} else {
140-
// skip everything else
141-
Vec::new()
142-
}
143-
}));
115+
v
116+
},
117+
));
118+
items.extend(doc.inlined_foreigns.iter().flat_map(
119+
|((_, renamed), visit_ast::InlinedForeign { res, import_id })| {
120+
let Some(def_id) = res.opt_def_id() else { return Vec::new() };
121+
let name = renamed.unwrap_or_else(|| cx.tcx.item_name(def_id));
122+
let import = cx.tcx.hir_expect_item(*import_id);
123+
match import.kind {
124+
hir::ItemKind::Use(path, kind) => {
125+
let hir::UsePath { segments, span, .. } = *path;
126+
let path = hir::Path { segments, res: *res, span };
127+
clean_use_statement_inner(
128+
import,
129+
Some(name),
130+
&path,
131+
kind,
132+
cx,
133+
&mut Default::default(),
134+
)
135+
}
136+
_ => unreachable!(),
137+
}
138+
},
139+
));
140+
items.extend(doc.items.values().flat_map(
141+
|visit_ast::ItemEntry { item, renamed, import_ids: _ }| {
142+
// Now we actually lower the imports, skipping everything else.
143+
if let hir::ItemKind::Use(path, hir::UseKind::Glob) = item.kind {
144+
clean_use_statement(item, *renamed, path, hir::UseKind::Glob, cx, &mut inserted)
145+
} else {
146+
// skip everything else
147+
Vec::new()
148+
}
149+
},
150+
));
144151

145152
// determine if we should display the inner contents or
146153
// the outer `mod` item for the source code.

src/librustdoc/visit_ast.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ pub(crate) struct Module<'hir> {
3232
pub(crate) def_id: LocalDefId,
3333
pub(crate) renamed: Option<Symbol>,
3434
pub(crate) import_id: Option<LocalDefId>,
35-
/// The key is the item `ItemId`.
36-
/// We use `FxIndexMap` to keep the insert order.
35+
/// The key is the item `ItemId`. We use `FxIndexMap` to keep the insert order.
3736
///
3837
/// `import_id` needs to be a `Vec` because we live in a dark world where you can have code
3938
/// like:
@@ -54,17 +53,17 @@ pub(crate) struct Module<'hir> {
5453
/// shadowed or not.
5554
pub(crate) items: FxIndexMap<(LocalDefId, Option<Symbol>), ItemEntry<'hir>>,
5655

57-
/// (def_id, renamed) -> (res, local_import_id)
56+
/// The key is `(def_id, renamed)`.
5857
///
5958
/// `inlined_foreigns` only contains `extern` items
6059
/// that are cross-crate inlined.
6160
///
6261
/// Locally inlined `extern` items are
6362
/// stored in `foreigns` with the `import_id` set,
6463
/// analogous to how `items` is.
65-
pub(crate) inlined_foreigns: FxIndexMap<(DefId, Option<Symbol>), (Res, LocalDefId)>,
64+
pub(crate) inlined_foreigns: FxIndexMap<(DefId, Option<Symbol>), InlinedForeign>,
6665
/// (item, renamed, import_id)
67-
pub(crate) foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>, Option<LocalDefId>)>,
66+
pub(crate) foreigns: Vec<Foreign<'hir>>,
6867
}
6968

7069
#[derive(Debug)]
@@ -74,6 +73,19 @@ pub(crate) struct ItemEntry<'hir> {
7473
pub(crate) import_ids: Vec<LocalDefId>,
7574
}
7675

76+
#[derive(Debug)]
77+
pub(crate) struct InlinedForeign {
78+
pub(crate) res: Res,
79+
pub(crate) import_id: LocalDefId,
80+
}
81+
82+
#[derive(Debug)]
83+
pub(crate) struct Foreign<'hir> {
84+
pub(crate) item: &'hir hir::ForeignItem<'hir>,
85+
pub(crate) renamed: Option<Symbol>,
86+
pub(crate) import_id: Option<LocalDefId>,
87+
}
88+
7789
impl Module<'_> {
7890
pub(crate) fn new(
7991
name: Symbol,
@@ -283,7 +295,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
283295
.last_mut()
284296
.unwrap()
285297
.inlined_foreigns
286-
.insert((ori_res_did, renamed), (res, def_id));
298+
.insert((ori_res_did, renamed), InlinedForeign { res, import_id: def_id });
287299
return true;
288300
};
289301

@@ -575,7 +587,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
575587
) {
576588
// If inlining we only want to include public functions.
577589
if !self.inlining || self.cx.tcx.visibility(item.owner_id).is_public() {
578-
self.modules.last_mut().unwrap().foreigns.push((item, renamed, import_id));
590+
self.modules.last_mut().unwrap().foreigns.push(Foreign { item, renamed, import_id });
579591
}
580592
}
581593

0 commit comments

Comments
 (0)