1
Fork 0

Correctly handle "pub use" reexports

This commit is contained in:
Guillaume Gomez 2020-09-28 00:18:09 +02:00
parent 7c0d576c59
commit 31d275e587
6 changed files with 32 additions and 13 deletions

View file

@ -514,6 +514,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
}, },
did: None, did: None,
}, },
false,
)), )),
}); });
} else if let Some(i) = } else if let Some(i) =

View file

@ -2269,12 +2269,12 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
visibility: self.vis.clean(cx), visibility: self.vis.clean(cx),
stability: None, stability: None,
deprecation: None, deprecation: None,
inner: ImportItem(Import::Glob(resolve_use_source(cx, path))), inner: ImportItem(Import::Glob(resolve_use_source(cx, path), false)),
}); });
return items; return items;
} }
} }
Import::Glob(resolve_use_source(cx, path)) Import::Glob(resolve_use_source(cx, path), true)
} else { } else {
let name = self.name; let name = self.name;
if !please_inline { if !please_inline {
@ -2297,6 +2297,9 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
Some(self.attrs), Some(self.attrs),
&mut visited, &mut visited,
) { ) {
// In case this is a macro, we don't want to show the reexport, only the macro
// itself.
let is_macro = matches!(path.res, Res::Def(DefKind::Macro(_), _));
items.push(Item { items.push(Item {
name: None, name: None,
attrs: self.attrs.clean(cx), attrs: self.attrs.clean(cx),
@ -2308,12 +2311,13 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
inner: ImportItem(Import::Simple( inner: ImportItem(Import::Simple(
self.name.clean(cx), self.name.clean(cx),
resolve_use_source(cx, path), resolve_use_source(cx, path),
is_macro,
)), )),
}); });
return items; return items;
} }
} }
Import::Simple(name.clean(cx), resolve_use_source(cx, path)) Import::Simple(name.clean(cx), resolve_use_source(cx, path), false)
}; };
vec![Item { vec![Item {

View file

@ -1655,9 +1655,20 @@ pub struct Impl {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Import { pub enum Import {
// use source as str; // use source as str;
Simple(String, ImportSource), // The bool indicates wether it imports a macro or not.
Simple(String, ImportSource, bool),
// use source::*; // use source::*;
Glob(ImportSource), // The bool indicates wether this is from an import.
Glob(ImportSource, bool),
}
impl Import {
pub fn should_be_displayed(&self) -> bool {
match *self {
Self::Simple(_, _, is_macro) => !is_macro,
Self::Glob(_, is_from_import) => is_from_import,
}
}
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]

View file

@ -245,6 +245,7 @@ pub struct ExternCrate<'hir> {
pub span: Span, pub span: Span,
} }
#[derive(Debug)]
pub struct Import<'hir> { pub struct Import<'hir> {
pub name: Symbol, pub name: Symbol,
pub id: hir::HirId, pub id: hir::HirId,

View file

@ -1150,14 +1150,14 @@ impl PrintWithSpace for hir::Mutability {
impl clean::Import { impl clean::Import {
crate fn print(&self) -> impl fmt::Display + '_ { crate fn print(&self) -> impl fmt::Display + '_ {
display_fn(move |f| match *self { display_fn(move |f| match *self {
clean::Import::Simple(ref name, ref src) => { clean::Import::Simple(ref name, ref src, _) => {
if *name == src.path.last_name() { if *name == src.path.last_name() {
write!(f, "use {};", src.print()) write!(f, "use {};", src.print())
} else { } else {
write!(f, "use {} as {};", src.print(), *name) write!(f, "use {} as {};", src.print(), *name)
} }
} }
clean::Import::Glob(ref src) => { clean::Import::Glob(ref src, _) => {
if src.path.segments.is_empty() { if src.path.segments.is_empty() {
write!(f, "use *;") write!(f, "use *;")
} else { } else {

View file

@ -2074,12 +2074,14 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean:
} }
clean::ImportItem(ref import) => { clean::ImportItem(ref import) => {
write!( if import.should_be_displayed() {
w, write!(
"<tr><td><code>{}{}</code></td></tr>", w,
myitem.visibility.print_with_space(), "<tr><td><code>{}{}</code></td></tr>",
import.print() myitem.visibility.print_with_space(),
); import.print()
);
}
} }
_ => { _ => {