Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: resolve intra-doc links to builtin macros #61916

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,23 @@ impl Attributes {

self.links.iter().filter_map(|&(ref s, did, ref fragment)| {
match did {
Some(DefId { krate: CrateNum::BuiltinMacros, .. }) => {
// builtin macros need to be handled separately, they'll always link to std
// though
let cache = cache();
let url = match cache.extern_locations.get(krate) {
Some(&(_, ref src, ExternalLocation::Local)) =>
src.to_str().expect("invalid file path"),
Some(&(_, _, ExternalLocation::Remote(ref s))) => s,
Some(&(_, _, ExternalLocation::Unknown)) | None =>
"https://doc.rust-lang.org/nightly",
};
Some((s.clone(),
format!("{}{}std/macro.{}.html",
url,
if !url.ends_with('/') { "/" } else { "" },
s)))
}
Some(did) => {
if let Some((mut href, ..)) = href(did) {
if let Some(ref fragment) = *fragment {
Expand Down
18 changes: 12 additions & 6 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use errors::Applicability;
use rustc::hir::def::{Res, DefKind, Namespace::{self, *}, PerNS};
use rustc::hir::def_id::DefId;
use rustc::hir::def_id::{DefId, CrateNum};
use rustc::hir;
use rustc::lint as lint;
use rustc::ty;
Expand Down Expand Up @@ -388,11 +388,17 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
}
};

if let Res::PrimTy(_) = res {
item.attrs.links.push((ori_link, None, fragment));
} else {
let id = register_res(cx, res);
item.attrs.links.push((ori_link, Some(id), fragment));
match res {
Res::PrimTy(_) => {
item.attrs.links.push((ori_link, None, fragment));
}
Res::Def(DefKind::Macro(_), did) if did.krate == CrateNum::BuiltinMacros => {
item.attrs.links.push((ori_link, Some(did), fragment));
}
res => {
let id = register_res(cx, res);
item.attrs.links.push((ori_link, Some(id), fragment));
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/rustdoc/intra-link-builtin-macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @has intra_link_builtin_macros/index.html
// @has - '//a/@href' 'https://doc.rust-lang.org/nightly/std/macro.cfg.html'
//! [cfg]