Skip to content

Commit 5ed55f7

Browse files
remove Clean trait implementation for ast::Module
1 parent 5af97e8 commit 5ed55f7

File tree

2 files changed

+62
-69
lines changed

2 files changed

+62
-69
lines changed

src/librustdoc/clean/mod.rs

+58-65
Original file line numberDiff line numberDiff line change
@@ -48,75 +48,68 @@ pub(crate) trait Clean<'tcx, T> {
4848
fn clean(&self, cx: &mut DocContext<'tcx>) -> T;
4949
}
5050

51-
impl<'tcx> Clean<'tcx, Item> for DocModule<'tcx> {
52-
fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
53-
let mut items: Vec<Item> = vec![];
54-
let mut inserted = FxHashSet::default();
55-
items.extend(self.foreigns.iter().map(|(item, renamed)| {
56-
let item = clean_maybe_renamed_foreign_item(cx, item, *renamed);
51+
pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
52+
let mut items: Vec<Item> = vec![];
53+
let mut inserted = FxHashSet::default();
54+
items.extend(doc.foreigns.iter().map(|(item, renamed)| {
55+
let item = clean_maybe_renamed_foreign_item(cx, item, *renamed);
56+
if let Some(name) = item.name {
57+
inserted.insert((item.type_(), name));
58+
}
59+
item
60+
}));
61+
items.extend(doc.mods.iter().map(|x| {
62+
inserted.insert((ItemType::Module, x.name));
63+
clean_doc_module(x, cx)
64+
}));
65+
66+
// Split up imports from all other items.
67+
//
68+
// This covers the case where somebody does an import which should pull in an item,
69+
// but there's already an item with the same namespace and same name. Rust gives
70+
// priority to the not-imported one, so we should, too.
71+
items.extend(doc.items.iter().flat_map(|(item, renamed)| {
72+
// First, lower everything other than imports.
73+
if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
74+
return Vec::new();
75+
}
76+
let v = clean_maybe_renamed_item(cx, item, *renamed);
77+
for item in &v {
5778
if let Some(name) = item.name {
5879
inserted.insert((item.type_(), name));
5980
}
60-
item
61-
}));
62-
items.extend(self.mods.iter().map(|x| {
63-
inserted.insert((ItemType::Module, x.name));
64-
x.clean(cx)
65-
}));
66-
67-
// Split up imports from all other items.
68-
//
69-
// This covers the case where somebody does an import which should pull in an item,
70-
// but there's already an item with the same namespace and same name. Rust gives
71-
// priority to the not-imported one, so we should, too.
72-
items.extend(self.items.iter().flat_map(|(item, renamed)| {
73-
// First, lower everything other than imports.
74-
if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
75-
return Vec::new();
76-
}
77-
let v = clean_maybe_renamed_item(cx, item, *renamed);
78-
for item in &v {
79-
if let Some(name) = item.name {
80-
inserted.insert((item.type_(), name));
81-
}
82-
}
83-
v
84-
}));
85-
items.extend(self.items.iter().flat_map(|(item, renamed)| {
86-
// Now we actually lower the imports, skipping everything else.
87-
if let hir::ItemKind::Use(path, hir::UseKind::Glob) = item.kind {
88-
let name = renamed.unwrap_or_else(|| cx.tcx.hir().name(item.hir_id()));
89-
clean_use_statement(item, name, path, hir::UseKind::Glob, cx, &mut inserted)
90-
} else {
91-
// skip everything else
92-
Vec::new()
93-
}
94-
}));
95-
96-
// determine if we should display the inner contents or
97-
// the outer `mod` item for the source code.
98-
99-
let span = Span::new({
100-
let where_outer = self.where_outer(cx.tcx);
101-
let sm = cx.sess().source_map();
102-
let outer = sm.lookup_char_pos(where_outer.lo());
103-
let inner = sm.lookup_char_pos(self.where_inner.lo());
104-
if outer.file.start_pos == inner.file.start_pos {
105-
// mod foo { ... }
106-
where_outer
107-
} else {
108-
// mod foo; (and a separate SourceFile for the contents)
109-
self.where_inner
110-
}
111-
});
81+
}
82+
v
83+
}));
84+
items.extend(doc.items.iter().flat_map(|(item, renamed)| {
85+
// Now we actually lower the imports, skipping everything else.
86+
if let hir::ItemKind::Use(path, hir::UseKind::Glob) = item.kind {
87+
let name = renamed.unwrap_or_else(|| cx.tcx.hir().name(item.hir_id()));
88+
clean_use_statement(item, name, path, hir::UseKind::Glob, cx, &mut inserted)
89+
} else {
90+
// skip everything else
91+
Vec::new()
92+
}
93+
}));
94+
95+
// determine if we should display the inner contents or
96+
// the outer `mod` item for the source code.
97+
98+
let span = Span::new({
99+
let where_outer = doc.where_outer(cx.tcx);
100+
let sm = cx.sess().source_map();
101+
let outer = sm.lookup_char_pos(where_outer.lo());
102+
let inner = sm.lookup_char_pos(doc.where_inner.lo());
103+
if outer.file.start_pos == inner.file.start_pos {
104+
// mod foo { ... }
105+
where_outer
106+
} else {
107+
// mod foo; (and a separate SourceFile for the contents)
108+
doc.where_inner
109+
}
110+
});
112111

113-
Item::from_hir_id_and_parts(
114-
self.id,
115-
Some(self.name),
116-
ModuleItem(Module { items, span }),
117-
cx,
118-
)
119-
}
112+
Item::from_hir_id_and_parts(doc.id, Some(doc.name), ModuleItem(Module { items, span }), cx)
120113
}
121114

122115
fn clean_generic_bound<'tcx>(

src/librustdoc/clean/utils.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::clean::auto_trait::AutoTraitFinder;
22
use crate::clean::blanket_impl::BlanketImplFinder;
33
use crate::clean::render_macro_matchers::render_macro_matcher;
44
use crate::clean::{
5-
clean_middle_const, clean_middle_region, clean_middle_ty, inline, Clean, Crate, ExternalCrate,
6-
Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path, PathSegment,
7-
Primitive, PrimitiveType, Type, TypeBinding, Visibility,
5+
clean_doc_module, clean_middle_const, clean_middle_region, clean_middle_ty, inline, Crate,
6+
ExternalCrate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path,
7+
PathSegment, Primitive, PrimitiveType, Type, TypeBinding, Visibility,
88
};
99
use crate::core::DocContext;
1010
use crate::formats::item_type::ItemType;
@@ -37,7 +37,7 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
3737

3838
// Clean the crate, translating the entire librustc_ast AST to one that is
3939
// understood by rustdoc.
40-
let mut module = module.clean(cx);
40+
let mut module = clean_doc_module(&module, cx);
4141

4242
match *module.kind {
4343
ItemKind::ModuleItem(ref module) => {

0 commit comments

Comments
 (0)