diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 559dcaf5a8d3b..b2c59aa73113e 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -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 { diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 7fbfc3e1fc0f4..cd8d0a5fe7dca 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -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; @@ -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)); + } } } diff --git a/src/test/rustdoc/intra-link-builtin-macros.rs b/src/test/rustdoc/intra-link-builtin-macros.rs new file mode 100644 index 0000000000000..ad6450da316fd --- /dev/null +++ b/src/test/rustdoc/intra-link-builtin-macros.rs @@ -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]