From 6bea76f175f1a565320a1a93a174fb80567dda7c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 29 Sep 2020 17:04:40 +0200 Subject: [PATCH] Simplify included import items handling --- src/librustdoc/clean/inline.rs | 4 +-- src/librustdoc/clean/mod.rs | 13 +++----- src/librustdoc/clean/types.rs | 31 ++++++++++++------- src/librustdoc/html/format.rs | 16 +++++----- src/librustdoc/html/render/mod.rs | 19 ++++++------ .../passes/collect_intra_doc_links.rs | 2 +- src/test/rustdoc/reexport-check.rs | 11 +++++++ 7 files changed, 55 insertions(+), 41 deletions(-) create mode 100644 src/test/rustdoc/reexport-check.rs diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 17f9411fca8..79ff7fc62d5 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -498,7 +498,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet) visibility: clean::Public, stability: None, deprecation: None, - inner: clean::ImportItem(clean::Import::Simple( + inner: clean::ImportItem(clean::Import::new_simple( item.ident.to_string(), clean::ImportSource { path: clean::Path { @@ -514,7 +514,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet) }, did: None, }, - false, + true, )), }); } else if let Some(i) = diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index daa9f1df649..721e3902cf5 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2269,12 +2269,12 @@ impl Clean> for doctree::Import<'_> { visibility: self.vis.clean(cx), stability: None, deprecation: None, - inner: ImportItem(Import::Glob(resolve_use_source(cx, path), false)), + inner: ImportItem(Import::new_glob(resolve_use_source(cx, path), false)), }); return items; } } - Import::Glob(resolve_use_source(cx, path), true) + Import::new_glob(resolve_use_source(cx, path), true) } else { let name = self.name; if !please_inline { @@ -2297,9 +2297,6 @@ impl Clean> for doctree::Import<'_> { Some(self.attrs), &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 { name: None, attrs: self.attrs.clean(cx), @@ -2308,16 +2305,16 @@ impl Clean> for doctree::Import<'_> { visibility: self.vis.clean(cx), stability: None, deprecation: None, - inner: ImportItem(Import::Simple( + inner: ImportItem(Import::new_simple( self.name.clean(cx), resolve_use_source(cx, path), - is_macro, + false, )), }); return items; } } - Import::Simple(name.clean(cx), resolve_use_source(cx, path), false) + Import::new_simple(name.clean(cx), resolve_use_source(cx, path), true) }; vec![Item { diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 6f35ab4b386..903f44a0f93 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -177,6 +177,7 @@ impl Item { pub fn is_stripped(&self) -> bool { match self.inner { StrippedItem(..) => true, + ImportItem(ref i) => !i.should_be_displayed, _ => false, } } @@ -1653,22 +1654,28 @@ pub struct Impl { } #[derive(Clone, Debug)] -pub enum Import { - // use source as str; - // The bool indicates whether it imports a macro or not. - Simple(String, ImportSource, bool), - // use source::*; - // The bool indicates whether this is from an import. - Glob(ImportSource, bool), +pub struct Import { + pub kind: ImportKind, + pub source: ImportSource, + pub should_be_displayed: 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, - } + pub fn new_simple(name: String, source: ImportSource, should_be_displayed: bool) -> Self { + Self { kind: ImportKind::Simple(name), source, should_be_displayed } } + + pub fn new_glob(source: ImportSource, should_be_displayed: bool) -> Self { + Self { kind: ImportKind::Glob, source, should_be_displayed } + } +} + +#[derive(Clone, Debug)] +pub enum ImportKind { + // use source as str; + Simple(String), + // use source::*; + Glob, } #[derive(Clone, Debug)] diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index ece0c247fb5..d18282d6e67 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1149,19 +1149,19 @@ impl PrintWithSpace for hir::Mutability { impl clean::Import { crate fn print(&self) -> impl fmt::Display + '_ { - display_fn(move |f| match *self { - clean::Import::Simple(ref name, ref src, _) => { - if *name == src.path.last_name() { - write!(f, "use {};", src.print()) + display_fn(move |f| match self.kind { + clean::ImportKind::Simple(ref name) => { + if *name == self.source.path.last_name() { + write!(f, "use {};", self.source.print()) } else { - write!(f, "use {} as {};", src.print(), *name) + write!(f, "use {} as {};", self.source.print(), *name) } } - clean::Import::Glob(ref src, _) => { - if src.path.segments.is_empty() { + clean::ImportKind::Glob => { + if self.source.path.segments.is_empty() { write!(f, "use *;") } else { - write!(f, "use {}::*;", src.print()) + write!(f, "use {}::*;", self.source.print()) } } }) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index a74e768f980..76334f0213d 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2074,14 +2074,12 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean: } clean::ImportItem(ref import) => { - if import.should_be_displayed() { - write!( - w, - "{}{}", - myitem.visibility.print_with_space(), - import.print() - ); - } + write!( + w, + "{}{}", + myitem.visibility.print_with_space(), + import.print() + ); } _ => { @@ -4440,8 +4438,9 @@ fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) { fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) { let mut sidebar = String::new(); - if items.iter().any(|it| it.type_() == ItemType::ExternCrate || it.type_() == ItemType::Import) - { + if items.iter().any(|it| { + it.type_() == ItemType::ExternCrate || (it.type_() == ItemType::Import && !it.is_stripped()) + }) { sidebar.push_str(&format!( "
  • {name}
  • ", id = "reexports", diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index b9be3e2f92b..5b5624298a4 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -758,7 +758,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { debug!("ignoring extern crate item {:?}", item.def_id); return self.fold_item_recur(item); } - ImportItem(Import::Simple(ref name, ..)) => Some(name.clone()), + ImportItem(Import { kind: ImportKind::Simple(ref name, ..), .. }) => Some(name.clone()), MacroItem(..) => None, _ => item.name.clone(), }; diff --git a/src/test/rustdoc/reexport-check.rs b/src/test/rustdoc/reexport-check.rs new file mode 100644 index 00000000000..9ecf1abbea9 --- /dev/null +++ b/src/test/rustdoc/reexport-check.rs @@ -0,0 +1,11 @@ +#![crate_name = "foo"] + +// @!has 'foo/index.html' '//code' 'pub use self::i32;' +// @has 'foo/index.html' '//tr[@class="module-item"]' 'i32' +// @has 'foo/i32/index.html' +pub use std::i32; +// @!has 'foo/index.html' '//code' 'pub use self::string::String;' +// @has 'foo/index.html' '//tr[@class="module-item"]' 'String' +pub use std::string::String; +// @!has 'foo/index.html' '//code' 'pub use self::string::*;' +pub use std::string::*;