Skip to content

Commit bba0ea2

Browse files
committed
rustdoc: support methods on primitives in intra-doc links
1 parent 1005f3b commit bba0ea2

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/librustdoc/clean/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -974,11 +974,13 @@ impl Attributes {
974974
"https://doc.rust-lang.org/nightly",
975975
};
976976
// This is a primitive so the url is done "by hand".
977+
let tail = fragment.find('#').unwrap_or_else(|| fragment.len());
977978
Some((s.clone(),
978-
format!("{}{}std/primitive.{}.html",
979+
format!("{}{}std/primitive.{}.html{}",
979980
url,
980981
if !url.ends_with('/') { "/" } else { "" },
981-
fragment)))
982+
&fragment[..tail],
983+
&fragment[tail..])))
982984
} else {
983985
panic!("This isn't a primitive?!");
984986
}

src/librustdoc/passes/collect_intra_doc_links.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc::lint as lint;
22
use rustc::hir;
33
use rustc::hir::def::Def;
4+
use rustc::hir::def_id::DefId;
45
use rustc::ty;
56
use syntax;
67
use syntax::ast::{self, Ident, NodeId};
@@ -126,6 +127,17 @@ impl<'a, 'tcx, 'rcx> LinkCollector<'a, 'tcx, 'rcx> {
126127
path = name.clone();
127128
}
128129
}
130+
if let Some(prim) = is_primitive(&path, false) {
131+
let did = primitive_impl(cx, &path).ok_or(())?;
132+
return cx.tcx.associated_items(did)
133+
.find(|item| item.ident.name == item_name)
134+
.and_then(|item| match item.kind {
135+
ty::AssociatedKind::Method => Some("method"),
136+
_ => None,
137+
})
138+
.map(|out| (prim, Some(format!("{}#{}.{}", path, out, item_name))))
139+
.ok_or(());
140+
}
129141

130142
// FIXME: `with_scope` requires the `NodeId` of a module.
131143
let ty = cx.resolver.borrow_mut()
@@ -603,3 +615,26 @@ fn is_primitive(path_str: &str, is_val: bool) -> Option<Def> {
603615
PRIMITIVES.iter().find(|x| x.0 == path_str).map(|x| x.1)
604616
}
605617
}
618+
619+
fn primitive_impl(cx: &DocContext, path_str: &str) -> Option<DefId> {
620+
let tcx = cx.tcx;
621+
match path_str {
622+
"u8" => tcx.lang_items().u8_impl(),
623+
"u16" => tcx.lang_items().u16_impl(),
624+
"u32" => tcx.lang_items().u32_impl(),
625+
"u64" => tcx.lang_items().u64_impl(),
626+
"u128" => tcx.lang_items().u128_impl(),
627+
"usize" => tcx.lang_items().usize_impl(),
628+
"i8" => tcx.lang_items().i8_impl(),
629+
"i16" => tcx.lang_items().i16_impl(),
630+
"i32" => tcx.lang_items().i32_impl(),
631+
"i64" => tcx.lang_items().i64_impl(),
632+
"i128" => tcx.lang_items().i128_impl(),
633+
"isize" => tcx.lang_items().isize_impl(),
634+
"f32" => tcx.lang_items().f32_impl(),
635+
"f64" => tcx.lang_items().f64_impl(),
636+
"str" => tcx.lang_items().str_impl(),
637+
"char" => tcx.lang_items().char_impl(),
638+
_ => None,
639+
}
640+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![deny(intra_doc_link_resolution_failure)]
2+
3+
//! A [`char`] and its [`char::len_utf8`].

0 commit comments

Comments
 (0)