Skip to content

Commit 38d911d

Browse files
committed
Auto merge of #77276 - GuillaumeGomez:reexported-item-lints, r=jyn514,ollie27
Warn on broken intra-doc links added to cross-crate re-exports This emits `broken_intra_doc_links` for docs applied to pub use statements that point to external items and are inlined. Does not address #77200 - any existing broken links from the original crate will not show warnings. r? `@jyn514`
2 parents 5ddef54 + 7e218bb commit 38d911d

File tree

12 files changed

+95
-19
lines changed

12 files changed

+95
-19
lines changed

src/librustdoc/clean/inline.rs

+2-1
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,6 +514,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
514514
},
515515
did: None,
516516
},
517+
true,
517518
)),
518519
});
519520
} else if let Some(i) =

src/librustdoc/clean/mod.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -2258,8 +2258,7 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22582258
return items;
22592259
}
22602260
}
2261-
2262-
Import::Glob(resolve_use_source(cx, path))
2261+
Import::new_glob(resolve_use_source(cx, path), true)
22632262
} else {
22642263
let name = self.name;
22652264
if !please_inline {
@@ -2273,18 +2272,33 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22732272
}
22742273
if !denied {
22752274
let mut visited = FxHashSet::default();
2276-
if let Some(items) = inline::try_inline(
2275+
2276+
if let Some(mut items) = inline::try_inline(
22772277
cx,
22782278
cx.tcx.parent_module(self.id).to_def_id(),
22792279
path.res,
22802280
name,
22812281
Some(self.attrs),
22822282
&mut visited,
22832283
) {
2284+
items.push(Item {
2285+
name: None,
2286+
attrs: self.attrs.clean(cx),
2287+
source: self.span.clean(cx),
2288+
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2289+
visibility: self.vis.clean(cx),
2290+
stability: None,
2291+
deprecation: None,
2292+
inner: ImportItem(Import::new_simple(
2293+
self.name.clean(cx),
2294+
resolve_use_source(cx, path),
2295+
false,
2296+
)),
2297+
});
22842298
return items;
22852299
}
22862300
}
2287-
Import::Simple(name.clean(cx), resolve_use_source(cx, path))
2301+
Import::new_simple(name.clean(cx), resolve_use_source(cx, path), true)
22882302
};
22892303

22902304
vec![Item {

src/librustdoc/clean/types.rs

+21-3
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,11 +1654,28 @@ pub struct Impl {
16531654
}
16541655

16551656
#[derive(Clone, Debug)]
1656-
pub enum Import {
1657+
pub struct Import {
1658+
pub kind: ImportKind,
1659+
pub source: ImportSource,
1660+
pub should_be_displayed: bool,
1661+
}
1662+
1663+
impl Import {
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 }
1670+
}
1671+
}
1672+
1673+
#[derive(Clone, Debug)]
1674+
pub enum ImportKind {
16571675
// use source as str;
1658-
Simple(String, ImportSource),
1676+
Simple(String),
16591677
// use source::*;
1660-
Glob(ImportSource),
1678+
Glob,
16611679
}
16621680

16631681
#[derive(Clone, Debug)]

src/librustdoc/doctree.rs

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ pub struct ExternCrate<'hir> {
245245
pub span: Span,
246246
}
247247

248+
#[derive(Debug)]
248249
pub struct Import<'hir> {
249250
pub name: Symbol,
250251
pub id: hir::HirId,

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -4438,8 +4438,9 @@ fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) {
44384438
fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
44394439
let mut sidebar = String::new();
44404440

4441-
if items.iter().any(|it| it.type_() == ItemType::ExternCrate || it.type_() == ItemType::Import)
4442-
{
4441+
if items.iter().any(|it| {
4442+
it.type_() == ItemType::ExternCrate || (it.type_() == ItemType::Import && !it.is_stripped())
4443+
}) {
44434444
sidebar.push_str(&format!(
44444445
"<li><a href=\"#{id}\">{name}</a></li>",
44454446
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
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![crate_name = "intra_doc_broken"]
2+
3+
/// [not_found]
4+
pub fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// aux-build:intra-doc-broken.rs
2+
// check-pass
3+
4+
#![deny(broken_intra_doc_links)]
5+
6+
extern crate intra_doc_broken;
7+
8+
pub use intra_doc_broken::foo;
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![deny(broken_intra_doc_links)]
2+
3+
/// [aloha]
4+
//~^ ERROR unresolved link to `aloha`
5+
pub use std::task::RawWakerVTable;
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: unresolved link to `aloha`
2+
--> $DIR/pub-export-lint.rs:3:6
3+
|
4+
LL | /// [aloha]
5+
| ^^^^^ no item named `aloha` in scope
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/pub-export-lint.rs:1:9
9+
|
10+
LL | #![deny(broken_intra_doc_links)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
13+
14+
error: aborting due to previous error
15+

src/test/rustdoc/reexport-check.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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;

0 commit comments

Comments
 (0)