diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index a6c754ab67f61..79ff7fc62d53e 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,6 +514,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet) }, did: None, }, + true, )), }); } else if let Some(i) = diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index ca9135cd11a16..501891da573a6 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2258,8 +2258,7 @@ impl Clean> for doctree::Import<'_> { return items; } } - - Import::Glob(resolve_use_source(cx, path)) + Import::new_glob(resolve_use_source(cx, path), true) } else { let name = self.name; if !please_inline { @@ -2273,7 +2272,8 @@ impl Clean> for doctree::Import<'_> { } if !denied { let mut visited = FxHashSet::default(); - if let Some(items) = inline::try_inline( + + if let Some(mut items) = inline::try_inline( cx, cx.tcx.parent_module(self.id).to_def_id(), path.res, @@ -2281,10 +2281,24 @@ impl Clean> for doctree::Import<'_> { Some(self.attrs), &mut visited, ) { + items.push(Item { + name: None, + attrs: self.attrs.clean(cx), + source: self.span.clean(cx), + def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(), + visibility: self.vis.clean(cx), + stability: None, + deprecation: None, + inner: ImportItem(Import::new_simple( + self.name.clean(cx), + resolve_use_source(cx, path), + false, + )), + }); return items; } } - Import::Simple(name.clean(cx), resolve_use_source(cx, path)) + 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 179cf248846a8..903f44a0f93df 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,11 +1654,28 @@ pub struct Impl { } #[derive(Clone, Debug)] -pub enum Import { +pub struct Import { + pub kind: ImportKind, + pub source: ImportSource, + pub should_be_displayed: bool, +} + +impl 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, ImportSource), + Simple(String), // use source::*; - Glob(ImportSource), + Glob, } #[derive(Clone, Debug)] diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index 6bb9b58bead65..ee217d99d2c77 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -245,6 +245,7 @@ pub struct ExternCrate<'hir> { pub span: Span, } +#[derive(Debug)] pub struct Import<'hir> { pub name: Symbol, pub id: hir::HirId, diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 2da9c68b1967c..d18282d6e675d 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 afd1dc596427f..76334f0213d15 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -4438,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 b9be3e2f92b46..5b5624298a41a 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-ui/auxiliary/intra-doc-broken.rs b/src/test/rustdoc-ui/auxiliary/intra-doc-broken.rs new file mode 100644 index 0000000000000..31a8310d47242 --- /dev/null +++ b/src/test/rustdoc-ui/auxiliary/intra-doc-broken.rs @@ -0,0 +1,4 @@ +#![crate_name = "intra_doc_broken"] + +/// [not_found] +pub fn foo() {} diff --git a/src/test/rustdoc-ui/intra-doc-broken-reexport.rs b/src/test/rustdoc-ui/intra-doc-broken-reexport.rs new file mode 100644 index 0000000000000..ef261359ebd9e --- /dev/null +++ b/src/test/rustdoc-ui/intra-doc-broken-reexport.rs @@ -0,0 +1,8 @@ +// aux-build:intra-doc-broken.rs +// check-pass + +#![deny(broken_intra_doc_links)] + +extern crate intra_doc_broken; + +pub use intra_doc_broken::foo; diff --git a/src/test/rustdoc-ui/pub-export-lint.rs b/src/test/rustdoc-ui/pub-export-lint.rs new file mode 100644 index 0000000000000..3fd3f77400978 --- /dev/null +++ b/src/test/rustdoc-ui/pub-export-lint.rs @@ -0,0 +1,5 @@ +#![deny(broken_intra_doc_links)] + +/// [aloha] +//~^ ERROR unresolved link to `aloha` +pub use std::task::RawWakerVTable; diff --git a/src/test/rustdoc-ui/pub-export-lint.stderr b/src/test/rustdoc-ui/pub-export-lint.stderr new file mode 100644 index 0000000000000..c345def794c08 --- /dev/null +++ b/src/test/rustdoc-ui/pub-export-lint.stderr @@ -0,0 +1,15 @@ +error: unresolved link to `aloha` + --> $DIR/pub-export-lint.rs:3:6 + | +LL | /// [aloha] + | ^^^^^ no item named `aloha` in scope + | +note: the lint level is defined here + --> $DIR/pub-export-lint.rs:1:9 + | +LL | #![deny(broken_intra_doc_links)] + | ^^^^^^^^^^^^^^^^^^^^^^ + = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]` + +error: aborting due to previous error + diff --git a/src/test/rustdoc/reexport-check.rs b/src/test/rustdoc/reexport-check.rs new file mode 100644 index 0000000000000..dea72b81a57c3 --- /dev/null +++ b/src/test/rustdoc/reexport-check.rs @@ -0,0 +1,9 @@ +#![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;