Skip to content

Commit 82b3b07

Browse files
committed
Support intra-doc links on trait and module re-exports
Trait implementations are treated the same as modules for the purposes of intra-doc links.
1 parent e63e5cd commit 82b3b07

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ enum ErrorKind {
5050

5151
struct LinkCollector<'a, 'tcx> {
5252
cx: &'a DocContext<'tcx>,
53-
mod_ids: Vec<hir::HirId>,
53+
// NOTE: this may not necessarily be a module in the current crate
54+
mod_ids: Vec<DefId>,
5455
}
5556

5657
impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
@@ -445,17 +446,6 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
445446
fn fold_item(&mut self, mut item: Item) -> Option<Item> {
446447
use rustc_middle::ty::DefIdTree;
447448

448-
let item_hir_id = if item.is_mod() {
449-
if let Some(def_id) = item.def_id.as_local() {
450-
Some(self.cx.tcx.hir().as_local_hir_id(def_id))
451-
} else {
452-
debug!("attempting to fold on a non-local item: {:?}", item);
453-
return self.fold_item_recur(item);
454-
}
455-
} else {
456-
None
457-
};
458-
459449
let parent_node = if item.is_fake() {
460450
// FIXME: is this correct?
461451
None
@@ -482,13 +472,9 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
482472
let current_item = match item.inner {
483473
ModuleItem(..) => {
484474
if item.attrs.inner_docs {
485-
if item_hir_id.unwrap() != hir::CRATE_HIR_ID { item.name.clone() } else { None }
475+
if item.def_id.is_top_level_module() { item.name.clone() } else { None }
486476
} else {
487-
match parent_node.or(self
488-
.mod_ids
489-
.last()
490-
.map(|&local| self.cx.tcx.hir().local_def_id(local).to_def_id()))
491-
{
477+
match parent_node.or(self.mod_ids.last().copied()) {
492478
Some(parent) if !parent.is_top_level_module() => {
493479
// FIXME: can we pull the parent module's name from elsewhere?
494480
Some(self.cx.tcx.item_name(parent).to_string())
@@ -508,7 +494,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
508494
};
509495

510496
if item.is_mod() && item.attrs.inner_docs {
511-
self.mod_ids.push(item_hir_id.unwrap());
497+
self.mod_ids.push(item.def_id);
512498
}
513499

514500
let cx = self.cx;
@@ -655,7 +641,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
655641
// for outer comments we explicitly try and resolve against the
656642
// parent_node first.
657643
let base_node = if item.is_mod() && item.attrs.inner_docs {
658-
self.mod_ids.last().map(|&id| self.cx.tcx.hir().local_def_id(id).to_def_id())
644+
self.mod_ids.last().copied()
659645
} else {
660646
parent_node
661647
};
@@ -842,7 +828,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
842828
}
843829

844830
if item.is_mod() && !item.attrs.inner_docs {
845-
self.mod_ids.push(item_hir_id.unwrap());
831+
self.mod_ids.push(item.def_id);
846832
}
847833

848834
if item.is_mod() {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![crate_name = "module_inner"]
2+
#![deny(intra_doc_link_resolution_failure)]
3+
/// [SomeType] links to [bar]
4+
pub struct SomeType;
5+
pub trait SomeTrait {}
6+
/// [bar] links to [SomeTrait] and also [SomeType]
7+
pub mod bar {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![crate_name = "inner"]
2+
/// this is a trait
3+
pub trait SomeTrait {
4+
/// this is a method for [SomeTrait]
5+
fn foo();
6+
}
7+
8+
pub mod bar {
9+
use super::SomeTrait;
10+
11+
pub struct BarStruct;
12+
13+
impl SomeTrait for BarStruct {
14+
fn foo() {}
15+
}
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// outer.rs
2+
// aux-build: module.rs
3+
// build-aux-docs
4+
#![deny(intra_doc_link_resolution_failure)]
5+
extern crate module_inner;
6+
// @has 'module/bar/index.html' '//a[@href="../../module_inner/trait.SomeTrait.html"]' 'SomeTrait'
7+
// @has 'module/bar/index.html' '//a[@href="../../module_inner/struct.SomeType.html"]' 'SomeType'
8+
pub use module_inner::bar;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// aux-build:traits.rs
2+
// build-aux-docs
3+
// ignore-tidy-line-length
4+
#![deny(intra_doc_link_resolution_failure)]
5+
6+
extern crate inner;
7+
use inner::SomeTrait;
8+
9+
pub struct SomeStruct;
10+
11+
// @has 'traits/struct.SomeStruct.html' '//a[@href="../inner/trait.SomeTrait.html"]' 'SomeTrait'
12+
impl SomeTrait for SomeStruct {
13+
// @has 'traits/struct.SomeStruct.html' '//a[@href="../inner/trait.SomeTrait.html"]' 'SomeTrait'
14+
fn foo() {}
15+
}

0 commit comments

Comments
 (0)