Skip to content

Commit e63e5cd

Browse files
committed
Support intra-doc links on macro re-exports
This includes both `macro_rules!` and proc-macros.
1 parent 5f49f55 commit e63e5cd

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
124124
}
125125

126126
/// Resolves a string as a macro.
127-
fn macro_resolve(&self, path_str: &str, parent_id: Option<hir::HirId>) -> Option<Res> {
127+
fn macro_resolve(&self, path_str: &str, parent_id: Option<DefId>) -> Option<Res> {
128128
let cx = self.cx;
129129
let path = ast::Path::from_ident(Ident::from_str(path_str));
130130
cx.enter_resolver(|resolver| {
@@ -142,8 +142,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
142142
if let Some(res) = resolver.all_macros().get(&Symbol::intern(path_str)) {
143143
return Some(res.map_id(|_| panic!("unexpected id")));
144144
}
145-
if let Some(module_id) = parent_id.or(self.mod_ids.last().cloned()) {
146-
let module_id = cx.tcx.hir().local_def_id(module_id);
145+
if let Some(module_id) = parent_id {
147146
if let Ok((_, res)) =
148147
resolver.resolve_str_path_error(DUMMY_SP, path_str, MacroNS, module_id)
149148
{
@@ -167,17 +166,13 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
167166
disambiguator: Option<&str>,
168167
ns: Namespace,
169168
current_item: &Option<String>,
170-
mut parent_id: Option<DefId>,
169+
parent_id: Option<DefId>,
171170
extra_fragment: &Option<String>,
172171
item_opt: Option<&Item>,
173172
) -> Result<(Res, Option<String>), ErrorKind> {
174173
let cx = self.cx;
175174

176175
// In case we're in a module, try to resolve the relative path.
177-
if parent_id.is_none() {
178-
let id = self.mod_ids.last().cloned();
179-
parent_id = id.map(|id| cx.tcx.hir().local_def_id(id).to_def_id());
180-
}
181176
if let Some(module_id) = parent_id {
182177
let result = cx.enter_resolver(|resolver| {
183178
resolver.resolve_str_path_error(DUMMY_SP, &path_str, ns, module_id)
@@ -659,8 +654,11 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
659654
// we've already pushed this node onto the resolution stack but
660655
// for outer comments we explicitly try and resolve against the
661656
// parent_node first.
662-
let base_node =
663-
if item.is_mod() && item.attrs.inner_docs { None } else { parent_node };
657+
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())
659+
} else {
660+
parent_node
661+
};
664662

665663
// replace `Self` with suitable item's parent name
666664
if path_str.starts_with("Self::") {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![crate_name = "macro_inner"]
2+
#![deny(intra_doc_resolution_failure)]
3+
4+
pub struct Foo;
5+
6+
/// See also [`Foo`]
7+
#[macro_export]
8+
macro_rules! my_macro {
9+
() => {}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
// compile-flags: --crate-type proc-macro
4+
#![crate_type="proc-macro"]
5+
#![crate_name="proc_macro_inner"]
6+
7+
extern crate proc_macro;
8+
9+
use proc_macro::TokenStream;
10+
11+
/// Links to [`OtherDerive`]
12+
#[proc_macro_derive(DeriveA)]
13+
pub fn a_derive(input: TokenStream) -> TokenStream {
14+
input
15+
}
16+
17+
#[proc_macro_derive(OtherDerive)]
18+
pub fn other_derive(input: TokenStream) -> TokenStream {
19+
input
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ignore-tidy-linelength
2+
// aux-build:macro_inner.rs
3+
// aux-build:proc_macro.rs
4+
// build-aux-docs
5+
#![deny(intra_doc_resolution_failure)]
6+
extern crate macro_inner;
7+
extern crate proc_macro_inner;
8+
9+
// @has 'macro/macro.my_macro.html' '//a[@href="../macro_inner/struct.Foo.html"]' 'Foo'
10+
pub use macro_inner::my_macro;
11+
// @has 'macro/derive.DeriveA.html' '//a[@href="../proc_macro_inner/derive.OtherDerive.html"]' 'OtherDerive'
12+
pub use proc_macro_inner::DeriveA;

0 commit comments

Comments
 (0)