Skip to content

Commit 6bea76f

Browse files
Simplify included import items handling
1 parent bfdfc66 commit 6bea76f

File tree

7 files changed

+55
-41
lines changed

7 files changed

+55
-41
lines changed

src/librustdoc/clean/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
498498
visibility: clean::Public,
499499
stability: None,
500500
deprecation: None,
501-
inner: clean::ImportItem(clean::Import::Simple(
501+
inner: clean::ImportItem(clean::Import::new_simple(
502502
item.ident.to_string(),
503503
clean::ImportSource {
504504
path: clean::Path {
@@ -514,7 +514,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
514514
},
515515
did: None,
516516
},
517-
false,
517+
true,
518518
)),
519519
});
520520
} else if let Some(i) =

src/librustdoc/clean/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -2269,12 +2269,12 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22692269
visibility: self.vis.clean(cx),
22702270
stability: None,
22712271
deprecation: None,
2272-
inner: ImportItem(Import::Glob(resolve_use_source(cx, path), false)),
2272+
inner: ImportItem(Import::new_glob(resolve_use_source(cx, path), false)),
22732273
});
22742274
return items;
22752275
}
22762276
}
2277-
Import::Glob(resolve_use_source(cx, path), true)
2277+
Import::new_glob(resolve_use_source(cx, path), true)
22782278
} else {
22792279
let name = self.name;
22802280
if !please_inline {
@@ -2297,9 +2297,6 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22972297
Some(self.attrs),
22982298
&mut visited,
22992299
) {
2300-
// In case this is a macro, we don't want to show the reexport, only the macro
2301-
// itself.
2302-
let is_macro = matches!(path.res, Res::Def(DefKind::Macro(_), _));
23032300
items.push(Item {
23042301
name: None,
23052302
attrs: self.attrs.clean(cx),
@@ -2308,16 +2305,16 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
23082305
visibility: self.vis.clean(cx),
23092306
stability: None,
23102307
deprecation: None,
2311-
inner: ImportItem(Import::Simple(
2308+
inner: ImportItem(Import::new_simple(
23122309
self.name.clean(cx),
23132310
resolve_use_source(cx, path),
2314-
is_macro,
2311+
false,
23152312
)),
23162313
});
23172314
return items;
23182315
}
23192316
}
2320-
Import::Simple(name.clean(cx), resolve_use_source(cx, path), false)
2317+
Import::new_simple(name.clean(cx), resolve_use_source(cx, path), true)
23212318
};
23222319

23232320
vec![Item {

src/librustdoc/clean/types.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ impl Item {
177177
pub fn is_stripped(&self) -> bool {
178178
match self.inner {
179179
StrippedItem(..) => true,
180+
ImportItem(ref i) => !i.should_be_displayed,
180181
_ => false,
181182
}
182183
}
@@ -1653,24 +1654,30 @@ pub struct Impl {
16531654
}
16541655

16551656
#[derive(Clone, Debug)]
1656-
pub enum Import {
1657-
// use source as str;
1658-
// The bool indicates whether it imports a macro or not.
1659-
Simple(String, ImportSource, bool),
1660-
// use source::*;
1661-
// The bool indicates whether this is from an import.
1662-
Glob(ImportSource, bool),
1657+
pub struct Import {
1658+
pub kind: ImportKind,
1659+
pub source: ImportSource,
1660+
pub should_be_displayed: bool,
16631661
}
16641662

16651663
impl Import {
1666-
pub fn should_be_displayed(&self) -> bool {
1667-
match *self {
1668-
Self::Simple(_, _, is_macro) => !is_macro,
1669-
Self::Glob(_, is_from_import) => is_from_import,
1670-
}
1664+
pub fn new_simple(name: String, source: ImportSource, should_be_displayed: bool) -> Self {
1665+
Self { kind: ImportKind::Simple(name), source, should_be_displayed }
1666+
}
1667+
1668+
pub fn new_glob(source: ImportSource, should_be_displayed: bool) -> Self {
1669+
Self { kind: ImportKind::Glob, source, should_be_displayed }
16711670
}
16721671
}
16731672

1673+
#[derive(Clone, Debug)]
1674+
pub enum ImportKind {
1675+
// use source as str;
1676+
Simple(String),
1677+
// use source::*;
1678+
Glob,
1679+
}
1680+
16741681
#[derive(Clone, Debug)]
16751682
pub struct ImportSource {
16761683
pub path: Path,

src/librustdoc/html/format.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1149,19 +1149,19 @@ impl PrintWithSpace for hir::Mutability {
11491149

11501150
impl clean::Import {
11511151
crate fn print(&self) -> impl fmt::Display + '_ {
1152-
display_fn(move |f| match *self {
1153-
clean::Import::Simple(ref name, ref src, _) => {
1154-
if *name == src.path.last_name() {
1155-
write!(f, "use {};", src.print())
1152+
display_fn(move |f| match self.kind {
1153+
clean::ImportKind::Simple(ref name) => {
1154+
if *name == self.source.path.last_name() {
1155+
write!(f, "use {};", self.source.print())
11561156
} else {
1157-
write!(f, "use {} as {};", src.print(), *name)
1157+
write!(f, "use {} as {};", self.source.print(), *name)
11581158
}
11591159
}
1160-
clean::Import::Glob(ref src, _) => {
1161-
if src.path.segments.is_empty() {
1160+
clean::ImportKind::Glob => {
1161+
if self.source.path.segments.is_empty() {
11621162
write!(f, "use *;")
11631163
} else {
1164-
write!(f, "use {}::*;", src.print())
1164+
write!(f, "use {}::*;", self.source.print())
11651165
}
11661166
}
11671167
})

src/librustdoc/html/render/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -2074,14 +2074,12 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean:
20742074
}
20752075

20762076
clean::ImportItem(ref import) => {
2077-
if import.should_be_displayed() {
2078-
write!(
2079-
w,
2080-
"<tr><td><code>{}{}</code></td></tr>",
2081-
myitem.visibility.print_with_space(),
2082-
import.print()
2083-
);
2084-
}
2077+
write!(
2078+
w,
2079+
"<tr><td><code>{}{}</code></td></tr>",
2080+
myitem.visibility.print_with_space(),
2081+
import.print()
2082+
);
20852083
}
20862084

20872085
_ => {
@@ -4440,8 +4438,9 @@ fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) {
44404438
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
44414439
let mut sidebar = String::new();
44424440

4443-
if items.iter().any(|it| it.type_() == ItemType::ExternCrate || it.type_() == ItemType::Import)
4444-
{
4441+
if items.iter().any(|it| {
4442+
it.type_() == ItemType::ExternCrate || (it.type_() == ItemType::Import && !it.is_stripped())
4443+
}) {
44454444
sidebar.push_str(&format!(
44464445
"<li><a href=\"#{id}\">{name}</a></li>",
44474446
id = "reexports",

src/librustdoc/passes/collect_intra_doc_links.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
758758
debug!("ignoring extern crate item {:?}", item.def_id);
759759
return self.fold_item_recur(item);
760760
}
761-
ImportItem(Import::Simple(ref name, ..)) => Some(name.clone()),
761+
ImportItem(Import { kind: ImportKind::Simple(ref name, ..), .. }) => Some(name.clone()),
762762
MacroItem(..) => None,
763763
_ => item.name.clone(),
764764
};

src/test/rustdoc/reexport-check.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![crate_name = "foo"]
2+
3+
// @!has 'foo/index.html' '//code' 'pub use self::i32;'
4+
// @has 'foo/index.html' '//tr[@class="module-item"]' 'i32'
5+
// @has 'foo/i32/index.html'
6+
pub use std::i32;
7+
// @!has 'foo/index.html' '//code' 'pub use self::string::String;'
8+
// @has 'foo/index.html' '//tr[@class="module-item"]' 'String'
9+
pub use std::string::String;
10+
// @!has 'foo/index.html' '//code' 'pub use self::string::*;'
11+
pub use std::string::*;

0 commit comments

Comments
 (0)